gpt4 book ai didi

Unable to create RBAC using GoogleProvider(无法使用GoogleProvider创建RBAC)

转载 作者:bug小助手 更新时间:2023-10-26 20:35:15 26 4
gpt4 key购买 nike



I am creating an app in NextJs and I am using NextAuth. I want to use GoogleProvider.
I want to have two roles in my application, suppose these are 'A' and 'B". When a user signs up, he/she will be able to select the role when signing up.
I have searched a lot but could not find anything about this.

我正在用NextJs创建一个应用程序,我正在使用NextAuth。我想使用GoogleProvider。我想在我的应用程序中有两个角色,假设这两个角色是‘A’和‘B’。当用户注册时,他/她可以在注册时选择角色。我搜索了很多次,但没有找到任何关于这方面的信息。


Flow I tried --

我试过了--



  • The user lands on the signup page

  • Enters email and role

  • A user is added in data with the same creds

  • Login with Google Flow is triggered

  • But when the user signs in using the same email he registers earlier.

  • NextAuth throws an error something like - Please use the account you used previously.


Here are my NextAuthOptions

以下是我的NextAuthtions


export const authOptions: NextAuthOptions = {
pages: {
newUser: '/register',
},
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
},
}),
signIn: async ({ user, profile }) => {
console.log(profile)

if (!user) {
return Promise.reject(new Error('Invalid user provided by google.'))
}

const existingUser = await prisma.user.findFirst({ where: { email: user.email } })
if (!existingUser) {
return false
}

return true
},
},
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
authorization: {
params: {
prompt: 'consent',
access_type: 'offline',
response_type: 'code',
},
},
}),
],
}

Can anyone please suggest how can I achieve this? Or if there is any application already created for this would be very helpful

有谁能建议我怎样才能做到这一点?或者如果已经为此创建了任何应用程序,这将非常有帮助


更多回答
优秀答案推荐

NextAuth, and the Google Provider, is mostly about authentication, while your use case is more about authorization.

NextAuth和Google提供商主要是关于身份验证,而您的用例更多地是关于授权。


To achieve this user role selection and the further task of validating permissions, you need to either:

要完成此用户角色选择和进一步的权限验证任务,您需要:



更多回答

My concern is, how will I signup a user with specific role if I am using GoogleProvider.

我担心的是,如果我使用GoogleProvider,我将如何注册具有特定角色的用户。

You can use NextAuth callbacks to call the API you wish to assign role with next-auth.js.org/configuration/callbacks

您可以使用NextAuth回调来使用Next-auth.js.org/configuration/回调来调用您希望分配角色的API

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com