Utilizing Auth.js credentials provider for user sign-ins in a Next.js application has proven successful. The auth()
function now returns a non-null object upon signing in, as opposed to returning null
previously.
// when not signed in
const session = await auth();
console.log(session)
// null
// when signed in
const session = await auth();
console.log(session)
// { user: {}, expires: '2024-07-28T14:03:13.208Z' }
I'm curious about the fact that the user
object is empty {}
and wonder what it should actually contain. It's possible that my expectations are off due to not fully grasping the underlying patterns at play here.
Access to the project's database is indirect for me, as I need to interact with a specific API endpoint. The returned data from the API is somewhat obscured for the sake of brevity.
{
result: { status_code: 0, status: 'Ok', message: 'Sign in' },
payload: {
data: {
auth: [Object],
refresh_token: [Object],
user: [Object],
project: [Object]
}
}
}
The auth
and refresh_token
properties hold bearer and refresh tokens respectively. Meanwhile, the user
and project
properties contain the user-related information I seek.
How are these properties linked to the session? What is the expected relationship between them? Isn't the 'user' essentially the 'session'? My assumption is that the user
property from the result of auth()
should encompass all user information along with references to bearer and refresh tokens for use in subsequent API calls.
Alternatively, could it be that I'm misinterpreting the patterns and concepts at play here?
Provided below is the auth.ts
file:
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut
} = NextAuth({
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
async authorize(credentials){
try {
const login = await fetch(`https://some_project/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(credentials),
});
const user = await login.json();
console.log(user);
return user;
} catch (error) {
throw new Error("")
}
}
})
]
})
In essence, how do the user objects returned in the above code snippet and the user object within the session correlate?