Skip to content

Commit

Permalink
Refactor authOptions to update user role in token if ID is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyshnav001 committed Sep 28, 2024
1 parent c351f9a commit 73b60c5
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib/auth-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";
import prisma from "@/server/db";
import { UserRoleType } from "@/types";
import { JWT } from "next-auth/jwt";
import { getUserById } from "@/app/actions/get-user-by-id";

declare module "next-auth" {
interface Session {
Expand Down Expand Up @@ -39,6 +40,13 @@ export const authOptions: NextAuthOptions = {
role: user.role,
};
}
if (token.id) {
const updatedUser = await getUserById(token.id);
return {
...token,
role: updatedUser?.role,
};
}
return token;
},
async session({ session, token }: { session: NextAuthSession; token: JWT }): Promise<any> {
Expand Down

0 comments on commit 73b60c5

Please sign in to comment.