我有一个使用 Twitter Provider 的 Auth Provider,当我点击登录并将我重定向到隐藏登录按钮的登录页面时它运行正常(因为有一个会话)并且我的控制台显示会话数据和令牌是正确的,但不知何故它是再次运行(回调)并使我之前的会话数据无效
[...nextauth].js
import NextAuth from 'next-auth';
import TwitterProviders from 'next-auth/providers/twitter';
export default NextAuth({
providers: [
TwitterProviders({
clientId: process.env.TWITTER_CLIENT_ID,
clientSecret: process.env.TWITTER_CLIENT_SECRET
})
],
callbacks: {
async jwt({ token, user, account, profile }) {
if (account){
// Add access_token to the token right after signin, jwt callback runs twice but the second time account was undefined
if ( account.provider && !token[account.provider] ) {
token[account.provider] = {};
}
if ( account.oauth_token ) {
token[account.provider].oauth_token = account.oauth_token;
}
if ( account.oauth_token_secret ) {
token[account.provider].oauth_token_secret = account.oauth_token_secret;
}
token.account = account;
console.log('=================================================================')
console.log("Token :", token)
return token;
}
},
async session({ session, token }) {
console.log("Session :", session)
console.log('=================================================================')
return session;
}
},
secret: process.env.NEXTAUTH_SECRET
})
第一轮:
=================================================================
Token : {
name: '001 nlptest',
email: undefined,
picture: 'https://pbs.twimg.com/profile_images/1648862909584515073/S3KDvPUY.png',
sub: '1648862842563727360',
twitter: {
oauth_token: '1648862842563727360-CKNjKyBuccSsNmGMxCrfUX46ueyXLB',
oauth_token_secret: 'w15pRV9QujYSDFbrP9w4drw1daiebGYD9cBbaJuc2YI11'
},
account: {
provider: 'twitter',
type: 'oauth',
providerAccountId: '1648862842563727360',
oauth_token: '1648862842563727360-CKNjKyBuccSsNmGMxCrfUX46ueyXLB',
oauth_token_secret: 'w15pRV9QujYSDFbrP9w4drw1daiebGYD9cBbaJuc2YI11'
}
}
Session : {
user: {
name: '001 nlptest',
email: undefined,
image: 'https://pbs.twimg.com/profile_images/1648862909584515073/S3KDvPUY.png'
},
expires: '2023-05-21T04:11:31.703Z'
}
=================================================================
Session : {
user: {
name: '001 nlptest',
email: undefined,
image: 'https://pbs.twimg.com/profile_images/1648862909584515073/S3KDvPUY.png'
},
expires: '2023-05-21T04:11:33.758Z'
}
=================================================================
第二次:
Session : {
user: { name: undefined, email: undefined, image: undefined },
expires: '2023-05-21T04:18:14.709Z'
}
=================================================================
我的会话数据发生了什么,为什么它运行两次并使数据无效?
谢谢你的解释
我尝试使用推特登录,但后来会话数据无效,因为它运行了两次,第二次数据丢失了