NextAuth CredentialProvider doesn't return user object
Unanswered
Chinese perch posted this in #help-forum
Chinese perchOP
Hi, I have this auth.ts file to handle signin for my app:
This connects to an api I have that handles the authentication. This works and on my app at /api/auth/signin I have a form to log in. And the console.log() messages in the cmd do indeed show that the authentication is successful:
However, when I am then redirected, and I try to get the session data, I get an empty user object:
Here is how i try to get the session data:
Any Idea why the session's user object becomes empty, even though I return it?
import NextAuth from 'next-auth';
import CredentialsProvider from "next-auth/providers/credentials"
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
// ...
},
async authorize(credentials, req) {
const res = await fetch("my_api_url", {
method: 'POST',
body: JSON.stringify({ ... }),
headers: { "Content-Type": "application/json" }
})
const user = await res.json()
console.log(res.ok);
if (res.ok && user) {
console.log("we're returning the user object:");
console.log(user);
return user
}
console.log("we're returning null:");
return null
}
})
]
})This connects to an api I have that handles the authentication. This works and on my app at /api/auth/signin I have a form to log in. And the console.log() messages in the cmd do indeed show that the authentication is successful:
true // -> from console.log(res.ok)
we're returning the user object:
{ // -> from console.log(user)
token: 'theJWTtoken'
}However, when I am then redirected, and I try to get the session data, I get an empty user object:
{ user: {}, expires: '2024-04-05T17:17:57.992Z' }Here is how i try to get the session data:
import { auth, signIn, signOut } from '@/auth';
type Props = {}
const Header = async (props: Props) => {
const session = await auth();
console.log(session);
return (
// ...
);
};
export default Header;Any Idea why the session's user object becomes empty, even though I return it?