next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

What is the correct way to overwrite the function signIn in Callbacks in v5?

Open nasatome opened this issue 1 year ago • 3 comments

What is the improvement or update you wish to see?

in version 4, you could edit the signIn function inside callbacks, but in version 5 I don't see where to edit this option.

Will this option still be valid in the future?

Is there any context that might help us understand?

examples:

    callbacks: {
        async signIn({user, account, profile}) {
            if (user.email.endsWith('@domain.com')) {
                return true;
            } else {
                return false;
            }
        },
    }
callbacks: {
  async signIn({ user, account, profile, email, credentials }) {
    const isAllowedToSignIn = true
    if (isAllowedToSignIn) {
      return true
    } else {
      // Return false to display a default error message
      return false
      // Or you can return a URL to redirect to:
      // return '/unauthorized'
    }
  }
}

Does the docs page already exist? Please link to it.

https://next-auth.js.org/configuration/callbacks#sign-in-callback

nasatome avatar Jun 14 '24 21:06 nasatome

I have implemented custom sessions with callbacks the following way:

import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import {
  SignInWithEmailAndPassword,
  oauthSignIn,
} from "@/actions/user/user.actions";

const providers = [
  Google,
  GitHub,
  CredentialsProvider({
    name: "Credentials",
    credentials: {
      email: { label: "Email", type: "email" },
      password: { label: "Password", type: "password" },
    },
    async authorize(credentials) {
      const user = await SignInWithEmailAndPassword(credentials); // User verification logic
      if (user.status === 400) {
        return null;
      }
      return user.data;
    },
  }),
];

export const providerMap = providers.map((provider) => {
  if (typeof provider === "function") {
    const providerData = provider();
    return { id: providerData.id, name: providerData.name };
  } else {
    return { id: provider.id, name: provider.name };
  }
});

export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
  providers,
  pages: {
    signIn: "/auth/sign-in",
  },
  callbacks: {
    async signIn({ user, account }) {
      if (account.provider === "google" || account.provider === "github") {
        const { name, email, image } = user;
        const payload = {
          name,
          email,
          avatar: image,
          authType: "Oauth",
        };

        const res = await oauthSignIn(payload);
        user.id = res.data._id.toString();
        user.isVerified = res.data.isVerified;
        user.image = res.data.avatar;

        return user;
      }
      // Default to allow sign-in
      return user;
    },
    async jwt({ trigger, token, user }) {
      // Add user information to the token during sign-in
      if (trigger === "update") {
        token.isVerified = session.user.isVerified;
      }
      if (user) {
        console.log(user);
        const id = user._id?.toString() || user.id;
        token.id = id;
        token.email = user.email;
        token.name = user.name;
        token.isVerified = user.isVerified;
        token.picture = user.avatar || user.image;
      }
      return token;
    },
    async session({ session, token }) {
      session.user.id = token.id;
      session.user.email = token.email;
      session.user.name = token.name;
      session.user.image = token.picture;
      session.user.isVerified = token.isVerified;
      return session;
    },
  },
});```
If this answers your question please feel free to ask how it works..

Ali-Raza764 avatar Jun 15 '24 04:06 Ali-Raza764

I only problem I have faces is with the trigger to update the session when I verify user email and I think it is some of my personal error. For detailed implementation check this repository.

Ali-Raza764 avatar Jun 15 '24 04:06 Ali-Raza764

Actually the docs are in migration currently so some features are explained in the old documentation.

Ali-Raza764 avatar Jun 15 '24 04:06 Ali-Raza764

@nasatome the signIn callback should work as before. :thinking: If you experience some bug, please open a bug report.

@Ali-Raza764 note that returning data in the signIn method was never supported. Just move that logic into the jwt callback. signIn should be only used to decide if the user is allowed to sign in or not.

balazsorban44 avatar Aug 08 '24 22:08 balazsorban44