From 9abbefaa6b7ba57d01b7b9e4020162d7acf04a8b Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 6 Sep 2021 10:10:28 +0200 Subject: [PATCH] feat: github connect uses user id instead of username --- migrateDB.ts | 3 +++ src/database/users/users.schema.ts | 3 +++ src/database/users/users.types.ts | 4 +++- src/routes/connection.ts | 5 +++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/migrateDB.ts b/migrateDB.ts index 454de7d..f35533a 100644 --- a/migrateDB.ts +++ b/migrateDB.ts @@ -68,6 +68,9 @@ async function connect(db) { accessTokens: { github: r.accessToken, }, + externalIDs: { + github: r.profile.id, + }, username: r.username, emails: r.profile.emails.map((email) => { return { email: email.value, default: false }; diff --git a/src/database/users/users.schema.ts b/src/database/users/users.schema.ts index d81986c..33a7c7a 100644 --- a/src/database/users/users.schema.ts +++ b/src/database/users/users.schema.ts @@ -5,6 +5,9 @@ const UserSchema = new Schema({ accessTokens: { github: { type: String }, }, + externalIDs: { + github: { type: String }, + }, username: { type: String, index: { unique: true }, diff --git a/src/database/users/users.types.ts b/src/database/users/users.types.ts index 165e0db..a45cbd3 100644 --- a/src/database/users/users.types.ts +++ b/src/database/users/users.types.ts @@ -4,7 +4,9 @@ export interface IUser { accessTokens: { github: string; }; - + externalIDs: { + github: string; + }; username: string; emails: { email: string; diff --git a/src/routes/connection.ts b/src/routes/connection.ts index 1e7c019..fe64f95 100644 --- a/src/routes/connection.ts +++ b/src/routes/connection.ts @@ -8,6 +8,7 @@ import * as express from "express"; import config from "../../config"; import UserModel from "../database/users/users.model"; +import { IUserDocument } from "../database/users/users.types"; const RedisStore = connectRedis(session); @@ -28,9 +29,9 @@ const verify = async ( profile: Profile, done: OAuth2Strategy.VerifyCallback ): Promise => { - let user; + let user: IUserDocument; try { - user = await UserModel.findOne({ username: profile.username }); + user = await UserModel.findOne({ "externalIDs.github": profile.id }); const email = profile.emails ? profile.emails[0]?.value : null; const photo = profile.photos ? profile.photos[0]?.value : null; if (user) {