flow oops
This commit is contained in:
parent
3f3653b180
commit
16b5fa45ee
|
@ -37,7 +37,7 @@ export function getVerifiedEmail({ emails }: T.ProviderProfile): string | false
|
||||||
) ? emails[0].value : 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) {
|
if (!profile.emails || profile.emails.length === 0) {
|
||||||
return { email: undefined, verified: false };
|
return { email: undefined, verified: false };
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {
|
||||||
import {
|
import {
|
||||||
createNewUser,
|
createNewUser,
|
||||||
getVerifiedEmail,
|
getVerifiedEmail,
|
||||||
|
getEmailFromGoogleProfile,
|
||||||
} from "../lib/user-utils";
|
} from "../lib/user-utils";
|
||||||
import env from "../lib/env-vars";
|
import env from "../lib/env-vars";
|
||||||
import * as T from "../../../website/src/types/account-types";
|
import * as T from "../../../website/src/types/account-types";
|
||||||
|
@ -62,16 +63,6 @@ function setupPassport(passport: PassportStatic) {
|
||||||
if (otherAccountWSameGoogle) {
|
if (otherAccountWSameGoogle) {
|
||||||
return done(null, 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 });
|
const u = await updateLingdocsUser(req.user.userId, { google: gProfile });
|
||||||
if (!u.email) {
|
if (!u.email) {
|
||||||
// if the user is adding a google account and doesn't have a previous email, add the google 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);
|
return done(null, u);
|
||||||
}
|
}
|
||||||
|
// if there's a google account matching, log them in
|
||||||
const user = await getLingdocsUser("googleId", profile.id);
|
const user = await getLingdocsUser("googleId", profile.id);
|
||||||
if (user) return done (null, user);
|
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 });
|
const u = await createNewUser({ strategy: "google", profile: gProfile });
|
||||||
return done(null, u);
|
return done(null, u);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
Loading…
Reference in New Issue