diff --git a/account/src/lib/user-utils.ts b/account/src/lib/user-utils.ts index 83e11ac..8a8e1ac 100644 --- a/account/src/lib/user-utils.ts +++ b/account/src/lib/user-utils.ts @@ -37,7 +37,7 @@ export function getVerifiedEmail({ emails }: T.ProviderProfile): string | false ) ? emails[0].value : false; } -function getEmailFromGoogleProfile(profile: T.GoogleProfile): { email: string | undefined, verified: boolean } { +export function getEmailFromGoogleProfile(profile: T.GoogleProfile): { email: string | undefined, verified: boolean } { if (!profile.emails || profile.emails.length === 0) { return { email: undefined, verified: false }; } diff --git a/account/src/middleware/setup-passport.ts b/account/src/middleware/setup-passport.ts index 3ac4931..915df0e 100644 --- a/account/src/middleware/setup-passport.ts +++ b/account/src/middleware/setup-passport.ts @@ -13,6 +13,7 @@ import { import { createNewUser, getVerifiedEmail, + getEmailFromGoogleProfile, } from "../lib/user-utils"; import env from "../lib/env-vars"; import * as T from "../../../website/src/types/account-types"; @@ -62,16 +63,6 @@ function setupPassport(passport: PassportStatic) { if (otherAccountWSameGoogle) { return done(null, otherAccountWSameGoogle); } - // if the person used their google email for a plain signup, add the google provider to it and sign in - const googleMail = profile.emails && profile.emails[0].value; - if (googleMail) { - const otherAccountWSameEmail = await getLingdocsUser("email", googleMail); - console.log("found user with same gmail email"); - if (otherAccountWSameEmail) { - await updateLingdocsUser(otherAccountWSameEmail.userId, { google: gProfile }); - return done(null, otherAccountWSameEmail); - } - } const u = await updateLingdocsUser(req.user.userId, { google: gProfile }); if (!u.email) { // if the user is adding a google account and doesn't have a previous email, add the google email @@ -83,8 +74,21 @@ function setupPassport(passport: PassportStatic) { } return done(null, u); } + // if there's a google account matching, log them in const user = await getLingdocsUser("googleId", profile.id); if (user) return done (null, user); + // if the person used their google email for a plain signup, add the google provider to it and sign in + const googleMail = getEmailFromGoogleProfile(gProfile); + console.log("google mail is", googleMail.email) + if (googleMail.email) { + const otherAccountWSameEmail = await getLingdocsUser("email", googleMail.email); + console.log("user with same gmail email:", otherAccountWSameEmail?.name); + if (otherAccountWSameEmail) { + await updateLingdocsUser(otherAccountWSameEmail.userId, { google: gProfile }); + return done(null, otherAccountWSameEmail); + } + } + // otherwise create a brand new user const u = await createNewUser({ strategy: "google", profile: gProfile }); return done(null, u); } catch (e) {