Skip to content

Commit

Permalink
fix(auth): req.auth 無法顯示完整資訊問題
Browse files Browse the repository at this point in the history
問題描述:
auth.js callback 中 token, session 的相關處理
應該可以影響到在 req.auth 應該顯示的資訊
但僅能顯示 user 的 name, email, image 三個欄位

解決方式:
將 callback 放在 auth.config.js 中,即可正常顯示完整資訊
nextauthjs/next-auth#9836 (comment)
  • Loading branch information
AndrewCK24 committed May 22, 2024
1 parent 67d66d3 commit d1ec906
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
18 changes: 18 additions & 0 deletions auth.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ const authConfig = {
});
},
},
callbacks: {
async jwt({ token, user }) {
if (user) {
const { password, ...rest } = user._doc;
token.user = { ...token.user, ...rest };
}
return token;
},
async session({ session, token }) {
session.user = token.user;
return session;
},
},
pages: {
signIn: "/auth/sign-in",
error: "/auth/error",
newUser: "/team/invitations",
},
};

export default authConfig;
18 changes: 0 additions & 18 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,5 @@ import authConfig from "@/auth.config";
export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: MongooseAdapter(connectToMongoDB),
session: { strategy: "jwt" },
callbacks: {
async jwt({ token, user }) {
if (user) {
const { password, ...rest } = user._doc;
token.user = { ...token.user, ...rest };
}
return token;
},
async session({ session, token }) {
session.user = token.user;
return session;
},
},
pages: {
signIn: "/auth/sign-in",
error: "/auth/error",
newUser: "/team/invitations",
},
...authConfig,
});

0 comments on commit d1ec906

Please sign in to comment.