Next.js Discord

Discord Forum

next auth not set session token cookie

Answered
Blue horntail woodwasp posted this in #help-forum
Open in Discord
Blue horntail woodwaspOP
I have a problem with Next-auth using Credential Providers.
After login, there is no "next-auth.session-token" found.
I try to console on my middleware.ts :
  let cookie = request.cookies.get("next-auth.session-token");
  let token = request.cookies.get("token");

  console.log({ token, cookie });

fyi : I set extra token cookie for my backend token

This works fine on local, but not in production(vercel) .

Here is how I set my token cookie on login :
 async authorize(credentials, req) {
        try {
          const res = await axios.post(`${process.env.API_URL}/login`, {
            email: credentials?.email,
            password: credentials?.password,
          });

          const { user, token } = await res.data;

          if (token) {
            console.log("token is : ", token);
            cookies().set({
              name: "token",
              value: token,
              maxAge: 30 * 24 * 60 * 60,
            });
          }
          return user;
        } catch (error) {
          return null;
        }
      },
Answered by Blue horntail woodwasp
I found the answer here :
https://reacthustle.com/blog/how-to-get-session-in-nextjs-middleware-with-nextauthjs?expand_article=1

use getToken from next-auth/jwt is a good option.
View full answer

2 Replies

Blue horntail woodwaspOP
I found it, in my cookie storage there is new prefix for cookie. is there any solution to always get the correct token?
Blue horntail woodwaspOP
I found the answer here :
https://reacthustle.com/blog/how-to-get-session-in-nextjs-middleware-with-nextauthjs?expand_article=1

use getToken from next-auth/jwt is a good option.
Answer