Search code examples
next.jsdiscordstrapinext-auth

Next-Auth Signin without user consent when app is already authorized


I have a Next application that let's the user signup using their discord account. The signup part is working. Now I am wondering how can I let the user sign-in using the discord account without going through the authorize part again, if they had already done the signup.

In the /pages/api/auth/[...nextauth].ts file

export default (req: NextApiRequest, res: NextApiResponse<any>) => {

    if (req.query.firstName && req.query.lastName) {
        firstName = req.query.firstName;
        lastName = req.query.lastName;
    }

    return NextAuth(req, res, {

        providers: [
            DiscordProvider({
                clientId: process.env.DISCORD_CLIENT_ID,
                clientSecret: process.env.DISCORD_CLIENT_SECRET,
                authorization: { params: { scope: DISCORD_SCOPES } },
            }),
        ],

        session: { strategy: "jwt" },

        callbacks: {
            async session({ session, token, user }) {
                session.jwt = token.jwt;
                session.id = token.id;
                return session;
            },

            async jwt({ token, user, account }) {
                
            }

        }

    });
}

Using above logic I am saving the user data to strapi after signup.

How to let the user sign-in with discord without getting a new access token and going through authorization


Solution

  • If the user already authorized your app to log in. You can use the following in your front-end client.

    import { signIn } from "next-auth/react";
    
    signIn("discord", { callbackUrl: '', redirect: true }, { prompt: "none" });
    

    References:

    https://next-auth.js.org/getting-started/client#additional-parameters