Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass additional values to signIn() callback while using oauth in Authjs ( beta v5 ) #11387

Closed
inderjotx opened this issue Jul 14, 2024 · 3 comments
Labels
documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@inderjotx
Copy link

inderjotx commented Jul 14, 2024

What is the improvement or update you wish to see?

Using Auth.js ( next-auth v5 beta)
I am using Google Ouath , and building a role based sign-up , user can either sign-up as user or organization . I want to pass a additional param to the signIn("google") funation specifiing the role of the user. This will help me to create respective entity in the signIn callback

Is there any context that might help us understand?

I wasn't able to find any piece of information on Auth.js docs.

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

No response

@inderjotx inderjotx added documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jul 14, 2024
@Jay-Karia
Copy link
Contributor

I don't think that you can pass custom arguments to the signIn function.

You can extend the user session by adding the role field and even adding it to the database
Then, you can update the user role in the session callback from the token object.

// ...
callbacks: {
   async session({token, session}) {
     if (token.role && session.user) {
         session.user.role = token.role
     }
   }
  // ...
}

There's a dedicated video about next auth v5:
https://youtu.be/1MTyCvS05V4?si=wjam6facDE2pTZHJ
(3:17:00)

@inderjotx
Copy link
Author

@Jay-Karia I am adding that already . During the sign-up progress , I need to get that additional argument from the user to create a entity with specified role .

    callbacks: {
        async signIn({ profile, user, account, credentials, ...other }) {

            const email = profile?.email;
            const name = profile?.name;
            const image = profile?.picture
            const phoneNumber = profile?.phone_number;
            const address = profile?.address?.formatted + ", " + profile?.address?.locality + ", " + profile?.address?.region + ", " + profile?.address?.country;

            // i want to get this arguement from the user
            const is_institute = false  


            let response = await getUserFromEmail({ email: email ?? null });
            let userData;

            if (response) {
                userData = response;
            } else {
                userData = await createUser({
                    email: email ?? null,
                    name: name ?? null,
                    image: image ?? null,
                    phoneNumber: phoneNumber ?? null,
                    address: address ?? null,
                    is_institute: is_institute
                })
            }

            if (userData) {
                return true;
            }

            return false;

        },
        async jwt({ profile, token }) {

            const email = profile?.email;

            if (email) {
                const user = await getUserFromEmail({ email: email ?? null });


                if (user) {
                    token.id = user?.id;
                    token.name = user?.name;
                    token.email = user?.email;
                    token.is_institute = user?.is_institute;
                    token.image = user?.image;
                }



            }

            return token;



        },
        async session({ session, token }) {



            session.user.id = token?.id as string;
            session.user.name = token.name as string;
            session.user.email = token.email as string;
            session.user.is_institute = token.is_institute as boolean;
            session.user.image = token.image as string;
            return session;

        },


    }

@Jay-Karia
Copy link
Contributor

Okay,
When the register form is submitted, we'll call a function (a server action if you are using Next js) named register, we'll have to pass the values from the form as the function parameter.

We will get the role from the form input.

// actions/register.ts

export const register = async (values: z.infer<typeof registerSchema>) => {

    // validate data
    // ...

    // hash password
    // ...

    await db.user.create({
        data: {
            name: validatedValues.name.toLowerCase(),
            email: validatedValues.email,
            password: hashedPassword,
            role: validatedValues.role // getting the role from form input
        }
    })

    // ...

    return { msg: "User successfully registered, verify your email", status: "success" }

}

This function will create a user in database with the role specified in the form.

@nextauthjs nextauthjs locked and limited conversation to collaborators Aug 8, 2024
@balazsorban44 balazsorban44 converted this issue into discussion #11550 Aug 8, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants