Session Callback Not Being Triggered Using Credential Auth
Answered
Sloth bear posted this in #help-forum
Sloth bearOP
Code: https://pastebin.com/n3LUQHcc
Console Output After
I have seen notes about adding in
Console Output After
signIn() call:web:dev: - wait compiling /api/auth/[...nextauth]/route (client and server)...
web:dev: - event compiled successfully in 112 ms (525 modules)
web:dev: ddddd
web:dev: {
web:dev: id: 'cljhnt4pl0006vbvrpk088bgh',
web:dev: name: null,
web:dev: email: 'njkadgnujk@asgoiansg.com',
web:dev: emailVerified: null,
web:dev: image: null,
web:dev: createdAt: '2023-06-29T21:30:25.689Z',
web:dev: updatedAt: '2023-06-29T21:30:25.689Z'
web:dev: }
web:dev:
web:dev: - ┌ POST /api/auth/callback/credentials 200 in 312ms
web:dev: │
web:dev: └──── POST http://localhost:3001/auth/authorize 201 in 265ms (cache: MISS)
web:dev:
web:dev: [next-auth][warn][EXPERIMENTAL_API]
web:dev: `getServerSession` is used in a React Server Component.
web:dev: https://next-auth.js.org/configuration/nextjs#getServerSession}
web:dev: https://next-auth.js.org/warnings#EXPERIMENTAL_API
web:dev: {
web:dev: user: { name: null, email: 'njkadgnujk@asgoiansg.com', image: null }
web:dev: }I have seen notes about adding in
session: {strategy: 'jwt' } and did not make a difference, plus I got a TS error as an incompatible type.Answered by Sloth bear
Fixed it -- all i needed to do was in the JWT callback was check to make sure the user object was defined before adding it to the token. JWT callback gets called on EVERY access to the token, so when you call
Pretty nifty -- glad i ran into this issue and helped me better understand the library! Thanks for the help all!
getServerSession() , the call back will execute and fail bc User isnt defined, causing session callback to not execute either. JWT callback is called every access to the session to resign the token. Pretty nifty -- glad i ran into this issue and helped me better understand the library! Thanks for the help all!
45 Replies
Sloth bearOP
Proof of TS error:
@Sloth bear options.callbacks.session is called when you get a session in app codes like this
it is designed to tailor a session information from JWT(DB), then pass to app codes(API, Server/Client components), so it won't be invoked unless you explicitly grab a session.
export async function GET() {
const session = await getServerSession(options)it is designed to tailor a session information from JWT(DB), then pass to app codes(API, Server/Client components), so it won't be invoked unless you explicitly grab a session.
besides that, there is a bug in ur code. you set token.user.userId = user.id, so token.userId in session is undefined
Sloth bearOP
The bug was something I’ve fixed. It was introduced during debugging.
So when I call getServerSession(), it should trigger that function? I am currently calling that in an RSC and the session function is not running and assigning an id.
So when I call getServerSession(), it should trigger that function? I am currently calling that in an RSC and the session function is not running and assigning an id.
Sloth bearOP
https://codevoweb.com/nextjs-use-custom-login-and-signup-pages-for-nextauth-js/
blog post I have been following.
https://pastebin.com/6FucprUi
updated code to more closely follow the blog.
blog post I have been following.
https://pastebin.com/6FucprUi
updated code to more closely follow the blog.
Sloth bearOP
can do!
Sloth bearOP
When i dont pass in options
the log says undefined
u do not have a session cookie in ur browser or open it in incognite mode
Sloth bearOP
yes when i pass in the authoptions
i do
name and image is expected to be null
also session callback isnt running either.
can u rewrite like this?
console.log('Login', user)
i cannot locate it
console.log('Login', user)
i cannot locate it
Sloth bearOP
is this what you were asking for?
run this
export default async function Home() {
const session = await getServerSession(options)
const user = session?.user
console.log('Login', user) // show me this output ONLYSloth bearOP
undefined
now you got it
Sloth bearOP
but the session callback isnt executing? I should be expecting a console log from that?
session: ({ session, token }) => {
console.log("asdfasdf");
console.log("token ", token);
return {
...session,
user: {
...session.user,
id: token.id,
},
};
},go to Chrome Dev Tool > App > cookies
make sure you have session cookies in place
make sure you have session cookies in place
Sloth bearOP
cookies
weird. do u use PrismaAdapter?
maybe you want to clean options.ts to use either of Google or mail for now.
then nail down the issue. if you use PrismaAdapter, strategy is database by default.
maybe you want to clean options.ts to use either of Google or mail for now.
then nail down the issue. if you use PrismaAdapter, strategy is database by default.
make it simple to get familiar with Auth.js
Sloth bearOP
I’m determined to get this working 😂I am using prisma adapter, and I am using google along side credentials. Earlier I tried isolating all of them and I have the same issues.
Do you know why I am able to get a user back when I am not passing the auth options in to getServerSession()?
Do you know why I am able to get a user back when I am not passing the auth options in to getServerSession()?
Also im gonna create a blank project and see if i can replicate it there
if i were u, i started with a simple hello world with mail. then Google, then DB. that is how i understand new technology.
just my two cents
Sloth bearOP
I built up to this point. Google and DB work fine
i initialized credential auth and then hooked it up to my existing back end.
do u mean it used to work, session is called back correctly.
but when adding Google and DB, it stop working?
but when adding Google and DB, it stop working?
Sloth bearOP
correct.
Sloth bearOP
appreciate the help. thanks for the gut check. will keep hammering away and update if i find the solution.
yup. hope so. just keep in mind
if u use PrismaAdapter, strategy is database, session is stored in db side unless you Explicitly define strategy : jwt. bit complicated
if u use PrismaAdapter, strategy is database, session is stored in db side unless you Explicitly define strategy : jwt. bit complicated
Sloth bearOP
yeah i tried adding that in as well -- saw it in a github question. didnt seem to help.
my hunch is introducing DB messed up ur settings and cookies
Sloth bearOP
quite possible
here is my sample project, which uses Prisma without jwt. this helps
https://github.com/tfutada/next-13-auth-prisma/blob/main/app/options.ts
https://github.com/tfutada/next-13-auth-prisma/blob/main/app/options.ts
Sloth bearOP
Fixed it -- all i needed to do was in the JWT callback was check to make sure the user object was defined before adding it to the token. JWT callback gets called on EVERY access to the token, so when you call
Pretty nifty -- glad i ran into this issue and helped me better understand the library! Thanks for the help all!
getServerSession() , the call back will execute and fail bc User isnt defined, causing session callback to not execute either. JWT callback is called every access to the session to resign the token. Pretty nifty -- glad i ran into this issue and helped me better understand the library! Thanks for the help all!
Answer