我试图控制台日志session.user.accessToken和clientsecret,结果是未定义的,所以我前往NextAuth.js查看是否有问题,似乎account和user在jwt回调函数中未定义
而console.log(account)
是未定义的,我错了吗?
这是我的[...Nextauth].js文件
export default NextAuth({
// Configure one or more authentication providers
providers: [
SpotifyProvider({
clientId: process.env.NEXT_PUBLIC_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_CLIENT_SECRET,
authorization: LOGIN_URL
})
// ...add more providers here
],
secret: process.env.JWT_SECRET,
pages: {
login: "/login"
},
callbacks: {
async jwt({ token, account, user, profile }) {
//initial user sign in
if (account && user) {
console.log({ token, user, account, profile });
return {
...token,
accessToken: account.access_Token,
refreshToken: account.refresh_token,
username: account.providerAccountId,
accessTokenExpires: account.expires_at * 1000 //handle expiry time in ms
};
}
//return the previous token if current token is expired
if (Date.now() < token.accessTokenExpires) {
console.log("Existing token is valid");
return token;
}
//refresh token
console.log("Existing token has expired");
return await refreshAccessToken(token);
},
async session({ session, token }) {
session.user.accessToken = token.accessToken;
session.user.refreshToken = token.refreshToken;
session.user.username = token.username;
return session;
}
}
});
1条答案
按热度按时间62o28rlo1#
您可以在控制台中进一步检查。例如,在[...nextauth].js中添加如下内容:
参考:https://next-auth.js.org/configuration/events