Skip to content

Commit 56387d4

Browse files
committed
2025.02.22 next auth: change logic to assign githubId
1 parent 4d1e6c4 commit 56387d4

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

auth.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ import type { NextAuthConfig } from 'next-auth';
44
// https://authjs.dev/guides/edge-compatibility#middleware
55
export default {
66
providers: [GitHub],
7-
debug: true,
87
} satisfies NextAuthConfig;

auth.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@ import { MongoDBAdapter } from '@auth/mongodb-adapter';
22
import NextAuth from 'next-auth';
33
import authConfig from './auth.config';
44
import mongoClientPromise from './lib/mongo-client';
5+
import { ObjectId } from 'mongodb';
56

67
export 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

Comments
 (0)