Next.js Discord

Discord Forum

CredentialsProvider, JWT token disappearing from user object?

Unanswered
Egyptian Mau posted this in #help-forum
Open in Discord
Egyptian MauOP
I am using the Credentialsprovider likes this:

providers: [
    GitHub,
    CredentialsProvider({
      name: "Credentials",
      // You can specify which fields should be submitted, by adding keys to the `credentials` object.
      // e.g. domain, username, password, 2FA token, etc.
      // You can pass any HTML attribute to the <input> tag through the object.
      credentials: {
        email: {
          label: "Email",
          type: "email",
          placeholder: "email@example.com",
        },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        const { email, password } = credentials as any;
        const res = await fetch(
          "URL",
          {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({
              email,
              password,
            }),
          }
        );
        const data = await res.json();

        if (res.ok && data) {
          console.log(data.user);
          return data.user;
        }
        //If login invalid
        return null;
      },
    }),
  ],


Now in my callbacks I first set the JWT token but then in session callback it is not defined, what am I doing wrong?

jwt: async ({ token, user }) => {
      //attach JWT token to user object
      if (user) {
        token.accessToken = user.token;
      }
      return token;
    },
    async session({ session, token }) {
      session.accessToken = token.accessToken;
      return session;
    },

0 Replies