session always comes null in next-auth v5 credential provider
Unanswered
Atlantic cod posted this in #help-forum
Atlantic codOP
I feel like dying at this point. Nothing I do fixes it, my session is always null, except If I dont make the api call(or even include it), and return email, id, then the session gets updated:
auth.ts
auth.ts
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import axios from 'axios';
import { getUserByEmail } from "./data/users";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
session: {
strategy: 'jwt',
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) {
return null;
}
try {
const users = await axios.post('https:external/users/login', {
"email": credentials.email,
"password": credentials.password
});
const user = users?.data;
if (user && user.id) {
return {
id: user.id,
email: user.email,
};
} else {
return null;
}
} catch (error) {
console.error("Authentication error:", error);
return null;
}
}
})
],
debug: true,
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
token.email = user.email;
token.name = user.name;
}
return token;
},
async session({ session, token }) {
if (token) {
session.user = {
id: token?.id,
email: token.email,
name: token.name,
};
}
return session;
},
}
});14 Replies
@B33fb0n3 the url for retrieving a user does not look that correct for me:
> 'https:external/users/login'
Can you clarify that?
Atlantic codOP
sorry I redacted that. basically, thats external link to admin.domain/api app. upon successful login, it returns:
{
{
{
id: ...,
email:.
token
}
id: ...,
email:.
token
}
likewise, If I do:
session works
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) {
return null;
}
try {
return { email: "bro@gmail.com", id: "4" }
} catch (error) {
console.error("Authentication error:", error);
return null;
}
}
})
],session works
but as soon as do the api req, session doesnt work
@Atlantic cod but as soon as do the api req, session doesnt work
why don't you call your auth provider directly from the client?
const users = await axios.post('https:external/users/login', {
"email": credentials.email,
"password": credentials.password
});@B33fb0n3 why don't you call your auth provider directly from the client?
tsx
const users = await axios.post('https:external/users/login', {
"email": credentials.email,
"password": credentials.password
});
Atlantic codOP
like with in the login page without using next-auth?
that might actually work. so, currently I only need credentialprovider. I login with external link within the page> after successful login > pass the response to the auth.ts> then use next-auth?
next-auth is an auth provider, so you can easily manage mostly everything auth related. But it looks like you already have a backend for auth stuff. So why using next-auth?
@B33fb0n3 next-auth is an auth provider, so you can easily manage mostly everything auth related. But it looks like you already have a backend for auth stuff. So why using next-auth?
Atlantic codOP
man i just realized that. I was just using next-auth religiously
cause I used it from the start
I think yeah I wont use next-auth for now, beta seems to be too troubling
@Atlantic cod I think yeah I wont use next-auth for now, beta seems to be too troubling
yea, next-auth beta is a big buggy. I am using next-auth latest as auth backend. You already have an auth backend, so you won't need next-auth
@Atlantic cod solved?