我正在开发一个类似于 Zappier 的集成工具。我想使用 Next-auth 连接到一个或多个应用程序并保存其访问令牌。但是,Next-auth 一次仅允许一个会话。如何使用 Next-auth 一次存储多个会话?
export const authOptions = {
// Configure one or more authentication providers
secret: 'tsfsdf',
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
authorization: {
params: {
scope: 'openid https://www.googleapis.com/auth/gmail.compose https://www.googleapis.com/auth/gmail.modify'
}
}
}),
{
clientId: process.env.QUICK_BOOKS_CLIENT_ID,
clientSecret: process.env.QUICK_BOOKS_CLIENT_SECRET,
id: 'quickbooks',
name: 'QuickBooks',
type: 'oauth',
wellKnown: 'https://developer.api.intuit.com/.well-known/openid_sandbox_configuration',
authorization: { params: { scope: 'com.intuit.quickbooks.accounting openid profile email phone address' } },
idToken: true,
checks: ['pkce', 'state'],
profile(profile) {
console.log(profile, 'profile')
return {
id: profile.sub,
name: profile.name,
email: profile.email,
image: profile.picture
}
}
}
// ...add more providers here
],
callbacks: {
async session({ session, token, user }) {
session.user.id = token.id
session.accessToken = token.accessToken
return session
},
async jwt({ token, user, account, profile, isNewUser }) {
if (user) {
token.id = user.id
}
if (account) {
token.accessToken = account.access_token
}
return token
}
}
}
这演示了如何使用多个会话。我摆脱了 __NEXTAUTH,使 SessionProvider 可指向每个会话,并添加了一个 SessionsProvider 将所有会话放入上下文中,并添加了一个 useSessions 挂钩。
https://next-auth-nextjs-git-multi-sessions-demo-gilesbs-projects.vercel.app/