我正在使用NextAuth处理一个项目,在创建用户后,我需要创建帐户并让用户使用Stripe Connect配置帐户,为此,我需要仅在创建用户后才将用户重定向到某个页面(正如Stripe文档中所述),我该如何实现这一点?我曾考虑在NextAuth选项中使用createUser回调,但这似乎不是正确的方法。
以下是我的NextAuth选项:
const options = {
secret: process.env.NEXTAUTH_SECRET,
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET
}),
TwitchProvider({
clientId: process.env.TWITCH_CLIENT_ID,
clientSecret: process.env.TWITCH_CLIENT_SECRET
})
],
pages: {
signIn: '/signin'
},
callbacks: {
async signIn({ user, account, profile, email, credentials }) {
console.log("USER SIGNED IN, ", user, account);
// const user = await prisma.user.findUnique({})
// const account = await stripe.accounts.retrieve();
return true;
},
async redirect({ url, baseUrl }) {
return baseUrl;
},
async session({ session, user, token }) {
return { ...session, ...user };
},
async jwt({ token, user, account, profile, isNewUser }) {
return token;
}
},
events: {
createUser: async ({ user }) => {
console.log("USER CREATED, ", user);
const customer = await stripe.customers.create({ email: user.email });
const account = await stripe.accounts.create({ type: "standard" });
const accountLink = await stripe.accountLinks.create({
account: account.id,
refresh_url: process.env.PUBLIC_URL,
return_url: process.env.PUBLIC_URL,
type: 'account_onboarding',
});
await prisma.user.update({
where: { id: user.id },
data: {
customerId: customer.id,
stripeAccountId: account.id
},
});
}
},
cookies: {
sessionToken: {
name: 'next-auth.session-token',
options: {
httpOnly: true,
sameSite: 'lax',
path: '/',
secure: process.env.NODE_ENV === 'production',
domain: hostName
},
},
}
}
1条答案
按热度按时间ikfrs5lh1#
您可以在登录方法www.example.com中使用callbackUrlhttps://next-auth.js.org/getting-started/client#specifying-a-callbackurl
用户在首次登录时将被重定向到那里
如果用户是第一次登录,它将首先创建用户,然后将新用户重定向到callbackUrl中指定的URL。