@@ -2,29 +2,28 @@ import { MongoDBAdapter } from '@auth/mongodb-adapter';
22import NextAuth from 'next-auth' ;
33import authConfig from './auth.config' ;
44import mongoClientPromise from './lib/mongo-client' ;
5+ import { ObjectId } from 'mongodb' ;
56
67export const { handlers, signIn, signOut, auth } = NextAuth ( {
78 adapter : MongoDBAdapter ( mongoClientPromise ) ,
89 session : { strategy : 'jwt' } ,
910 ...authConfig ,
10- callbacks : {
11- /**
12- * signIn is called whenever a user signs in (OAuth or credentials).
13- * Here we have access to the `profile`, which includes GitHub user info.
14- */
15- async signIn ( { account, profile, user } ) {
16- // Only do this if the provider is GitHub
17- if ( account ?. provider === 'github' ) {
11+ events : {
12+ signIn : async ( { account, user, profile, isNewUser } ) => {
13+ if ( isNewUser && account ?. provider === 'github' ) {
1814 const client = await mongoClientPromise ;
1915 const db = client . db ( 'auth' ) ;
2016
2117 await db
2218 . collection ( 'accounts' )
23- . updateOne ( { userId : user . id } , { $set : { githubId : profile ?. node_id , githubLogin : profile ?. login } } ) ;
19+ . updateOne (
20+ { userId : new ObjectId ( user . id ) } ,
21+ { $set : { githubId : profile ?. node_id , githubLogin : profile ?. login } } ,
22+ ) ;
2423 }
25- return true ;
2624 } ,
27-
25+ } ,
26+ callbacks : {
2827 async jwt ( { token, profile, account } ) {
2928 if ( profile && account ?. provider === 'github' ) {
3029 token . githubId = profile . node_id ;
0 commit comments