Skip to content

Commit

Permalink
auth callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
andlrutt committed Oct 17, 2024
1 parent 63ca2f5 commit 8cc1172
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/app/api/auth/[...nextauth]/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MongoDBAdapter } from '@next-auth/mongodb-adapter';
import { MongoClient } from 'mongodb';
import { NextAuthOptions } from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import GoogleProvider, { GoogleProfile } from 'next-auth/providers/google';
import dbConnect from '@/utils/db-connect';
import { getUserByEmail } from '@/server/actions/users';

const clientPromise = dbConnect().then((mon) => {
return mon.connection.getClient() as unknown as Promise<MongoClient>;
Expand All @@ -14,8 +15,35 @@ export const handler: NextAuthOptions = {
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
profile(profile: GoogleProfile) {
return {
id: profile.sub,
name: profile.name,
email: profile.email,
image: profile.picture,
isAdmin: false,
};
},
}),
],
callbacks: {
async session({ session, token }) {
if (token.data && session.user) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
session.user.isAdmin = (token.data as any).isAdmin;
}
return session;
},
async jwt({ token }) {
if (token.email) {
try {
const user = await getUserByEmail(token.email);
token.data = user;
} catch {}
}
return token;
},
},
session: {
strategy: 'jwt',
},
Expand Down

0 comments on commit 8cc1172

Please sign in to comment.