Skip to content

Commit 0d37fed

Browse files
committed
Fix bug with Google and GitHub passport login
1 parent fd009c0 commit 0d37fed

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

server/.env.example

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
NODE_ENV="dev"
2+
PORT=3001
3+
POSTGRES_USER="postgres"
4+
POSTGRES_DB="notangles-db"
5+
POSTGRES_PASSWORD="postgres"
6+
POSTGRES_PORT="5432"
7+
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${POSTGRES_PORT}/${POSTGRES_DB}?schema=public"
8+
SESSION_SECRET="i-love-notangles"
9+
OAUTH2_CLIENT_PROVIDER_OIDC_ISSUER="https://id.example.com.au"
10+
OAUTH2_CLIENT_REGISTRATION_LOGIN_CLIENT_ID="a-b-c-d-e-f-1234567890"
11+
OAUTH2_CLIENT_REGISTRATION_LOGIN_CLIENT_SECRET="aaazzz123"
12+
OAUTH2_CLIENT_REGISTRATION_LOGIN_SCOPE="openid"
13+
OAUTH2_CLIENT_REGISTRATION_LOGIN_REDIRECT_URI="http://localhost:3001/api/auth/callback/devsoc"
14+
OAUTH2_CLIENT_REGISTRATION_LOGIN_POST_LOGOUT_REDIRECT_URI="http://localhost:3001/api/auth/callback/devsoc"
15+
GITHUB_CLIENT_ID="aaaZZZ123"
16+
GITHUB_CLIENT_SECRET="abcdef1234567890"
17+
GITHUB_REDIRECT_URI="http://localhost:3001/api/auth/callback/github"
18+
GOOGLE_CLIENT_ID="12345-aaazzz123.apps.googleusercontent.com"
19+
GOOGLE_CLIENT_SECRET="AAAZZZ-aaaZZZ123-aaaZZZ"
20+
GOOGLE_REDIRECT_URI="http://localhost:3001/api/auth/callback/google"
21+
CLIENT_HOST_NAME="localhost"
22+
CLIENT_HOST_PORT="5173"

server/src/auth/auth.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const OidcStrategyFactory = {
2525
controllers: [AuthController],
2626
providers: [
2727
PrismaService,
28-
// OidcStrategyFactory,
28+
OidcStrategyFactory,
2929
GithubStrategy,
3030
GoogleStrategy,
3131
GuestStrategy,

server/src/auth/github.strategy.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ export class GithubStrategy extends PassportStrategy(Strategy, 'github') {
2121
req: Request,
2222
_accessToken: string,
2323
_refreshToken: string,
24-
profile: { id: number; name: string; email: string },
24+
profile: { id: number; displayName: string | null; email: string },
2525
) {
26+
const displayName = profile.displayName?.trim() ?? 'GitHub User';
27+
2628
const user = await this.prisma.user.upsert({
2729
where: {
2830
authProvider_authSubject: {
@@ -36,21 +38,16 @@ export class GithubStrategy extends PassportStrategy(Strategy, 'github') {
3638
create: {
3739
authProvider: 'GITHUB',
3840
authSubject: profile.id.toString(),
39-
firstName: profile.name?.split(' ')[0] || 'GitHub',
40-
lastName: profile.name?.split(' ').slice(1).join(' ') || 'User',
41+
firstName: displayName.split(' ')[0] || 'GitHub',
42+
lastName: displayName.split(' ').slice(1).join(' ') || 'User',
4143
isGuest: false,
44+
settings: {
45+
create: {},
46+
},
4247
},
4348
});
4449

45-
if (user.createdAt.getTime() === user.lastLogin.getTime()) {
46-
await this.prisma.settings.create({
47-
data: {
48-
userId: user.id,
49-
},
50-
});
51-
}
52-
53-
const login = promisify(req.login.bind(this));
50+
const login = promisify(req.login.bind(req));
5451
await login(user);
5552
return user;
5653
}

server/src/auth/google.strategy.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,13 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
4343
firstName: profile.name.givenName,
4444
lastName: profile.name.familyName,
4545
isGuest: false,
46+
settings: {
47+
create: {},
48+
},
4649
},
4750
});
48-
if (user.createdAt.getTime() === user.lastLogin.getTime()) {
49-
await this.prisma.settings.create({
50-
data: {
51-
userId: user.id,
52-
},
53-
});
54-
}
55-
const login = promisify(req.login.bind(this));
51+
52+
const login = promisify(req.login.bind(req));
5653
await login(user);
5754
return user;
5855
}

0 commit comments

Comments
 (0)