Role-based signup - Assigning role at signup for better UX #9352
Replies: 3 comments 4 replies
-
Did you solve it, I have tried all the ways like adding to profile object, yet it’s not working |
Beta Was this translation helpful? Give feedback.
-
In docs they say something like
But using Oauth providers we don't get any roles. Also, we can't add our own logic as well. I also want to implement something similar, the only way around is to use Credentials, and that also need some workaround to use with database. |
Beta Was this translation helpful? Give feedback.
-
best way i found is setting a cookie, and getting it during the user creation event. here's the auth.ts (v5 beta 21): import NextAuth, { DefaultSession } from "next-auth";
import Google from "next-auth/providers/google";
import Credentials from "next-auth/providers/credentials";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "@/lib/prisma";
import { UserRole } from "@prisma/client";
import { cookies } from "next/headers";
declare module "next-auth" {
interface Session {
user: {
firstName: string;
lastName: string;
role: UserRole;
} & DefaultSession["user"];
}
interface User {
firstName: string;
lastName: string;
role: UserRole;
}
}
export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [
Google({
async profile(profile) {
return {
role: profile.role,
id: profile.sub,
email: profile.email,
image: profile.picture,
firstName: profile.given_name,
lastName: profile.family_name,
};
},
}),
],
pages: {
signIn: "/auth",
},
events: {
async createUser({ user }) { // this is the important step
const cookie = cookies().get("role");
if (cookie) {
await prisma.user.update({
where: {
id: user.id,
},
data: {
role: cookie.value as UserRole,
},
});
cookies().delete("role");
}
},
},
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.role = (user.role as UserRole) || UserRole.CONSUMER;
session.user.firstName = user.firstName;
session.user.lastName = user.lastName;
}
return session;
},
},
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have been searching for a guide on how to assign a role at signup with next auth, in vain. It seems that everything talk about role-based control and access, but never about how to properly assign it at sign up.
Indeed, I have a use case which I think is pretty common, being a two sided web app. User can sign up as a recruiter, or a talent. To have a seamless signup and onboarding process, I would really like to have the user select is role when signing in.
Is there a robust way to do that with next-auth? Correct me if I am wrong, but it seems like I cannot custom values to the providers (Google & Email). So how should proceed to pass it from the front end at sign up?
Stack:
Thank you for the help!
Here's my component Signup.tsx in the front:
It seems like my only way would be a workaround with a custom signup endpoint that I would somehow redirect to the next-auth sign up workflow down the road. But I dont feel like this would be robust enough. Am I wrong? (still a junior developer here!)
Beta Was this translation helpful? Give feedback.
All reactions