fix: User records not written correctly on signin (#1119)

* Initial fix for #1116

* clarify logic
This commit is contained in:
Tom Moor
2019-12-22 20:14:06 -08:00
committed by GitHub
parent c20282de06
commit 98dda567c2
2 changed files with 41 additions and 17 deletions

View File

@@ -89,12 +89,15 @@ router.get('google.callback', auth({ required: false }), async ctx => {
serviceId: profile.data.id,
},
{
service: '',
service: { [Op.eq]: null },
email: profile.data.email,
},
],
teamId: team.id,
},
defaults: {
service: 'google',
serviceId: profile.data.id,
name: profile.data.name,
email: profile.data.email,
isAdmin: isFirstUser,
@@ -102,6 +105,25 @@ router.get('google.callback', auth({ required: false }), async ctx => {
},
});
// update the user with fresh details if they just accepted an invite
if (!user.serviceId || !user.service) {
await user.update({
service: 'google',
serviceId: profile.data.id,
avatarUrl: profile.data.picture,
});
}
// update email address if it's changed in Google
if (!isFirstSignin && profile.data.email !== user.email) {
await user.update({ email: profile.data.email });
}
if (isFirstUser) {
await team.provisionFirstCollection(user.id);
await team.provisionSubdomain(hostname);
}
if (isFirstSignin) {
await Event.create({
name: 'users.create',
@@ -116,16 +138,6 @@ router.get('google.callback', auth({ required: false }), async ctx => {
});
}
// update email address if it's changed in Google
if (!isFirstSignin && profile.data.email !== user.email) {
await user.update({ email: profile.data.email });
}
if (isFirstUser) {
await team.provisionFirstCollection(user.id);
await team.provisionSubdomain(hostname);
}
// set cookies on response and redirect to team subdomain
ctx.signIn(user, team, 'google', isFirstSignin);
} catch (err) {