fix: fix user connection

This commit is contained in:
tdurieux
2021-09-06 10:18:46 +02:00
parent 9abbefaa6b
commit d812560b99
2 changed files with 14 additions and 12 deletions
+2 -2
View File
@@ -72,7 +72,7 @@ async function connect(db) {
github: r.profile.id, github: r.profile.id,
}, },
username: r.username, username: r.username,
emails: r.profile.emails.map((email) => { emails: r.profile.emails?.map((email) => {
return { email: email.value, default: false }; return { email: email.value, default: false };
}), }),
photo: r.profile.photos[0]?.value, photo: r.profile.photos[0]?.value,
@@ -82,7 +82,7 @@ async function connect(db) {
options: r.default.options, options: r.default.options,
}, },
}); });
if (user.emails.length) user.emails[0].default = true; if (user.emails?.length) user.emails[0].default = true;
await user.save(); await user.save();
+12 -10
View File
@@ -32,21 +32,23 @@ const verify = async (
let user: IUserDocument; let user: IUserDocument;
try { try {
user = await UserModel.findOne({ "externalIDs.github": profile.id }); 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) { if (user) {
user.accessToken = accessToken; user.accessTokens.github = accessToken;
user.email = photo;
user.photo = photo;
await user.save();
} else { } else {
user = await new UserModel({ const photo = profile.photos ? profile.photos[0]?.value : null;
user = new UserModel({
username: profile.username, username: profile.username,
accessToken: accessToken, accessTokens: {
email, github: accessToken,
},
emails: profile.emails?.map((email) => {
return { email: email.value, default: false };
}),
photo, photo,
}).save(); });
if (user.emails?.length) user.emails[0].default = true;
} }
await user.save();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} finally { } finally {