1
0
Fork 0
mirror of synced 2024-07-03 21:40:55 +12:00

Sync profile picture when using SSO

This commit is contained in:
Rory Powell 2021-07-29 14:43:23 +01:00
parent 7a42898ef8
commit 298cf4965c
3 changed files with 31 additions and 4 deletions

View file

@ -6,6 +6,7 @@ const { authError } = require("./utils")
const { newid } = require("../../hashing") const { newid } = require("../../hashing")
const { createASession } = require("../../security/sessions") const { createASession } = require("../../security/sessions")
const { getGlobalUserByEmail } = require("../../utils") const { getGlobalUserByEmail } = require("../../utils")
const { default: fetch } = require("node-fetch")
/** /**
* Common authentication logic for third parties. e.g. OAuth, OIDC. * Common authentication logic for third parties. e.g. OAuth, OIDC.
@ -65,7 +66,7 @@ exports.authenticateThirdParty = async function (
} }
} }
dbUser = syncUser(dbUser, thirdPartyUser) dbUser = await syncUser(dbUser, thirdPartyUser)
// create or sync the user // create or sync the user
const response = await db.post(dbUser) const response = await db.post(dbUser)
@ -86,10 +87,26 @@ exports.authenticateThirdParty = async function (
return done(null, dbUser) return done(null, dbUser)
} }
async function syncProfilePicture(user, thirdPartyUser) {
const pictureUrl = thirdPartyUser.profile._json.picture
if (pictureUrl) {
const response = await fetch(pictureUrl)
if (response.status === 200) {
const type = response.headers.get("content-type")
if (type.startsWith("image/")) {
user.pictureUrl = pictureUrl
}
}
}
return user
}
/** /**
* @returns a user that has been sync'd with third party information * @returns a user that has been sync'd with third party information
*/ */
function syncUser(user, thirdPartyUser) { async function syncUser(user, thirdPartyUser) {
// provider // provider
user.provider = thirdPartyUser.provider user.provider = thirdPartyUser.provider
user.providerType = thirdPartyUser.providerType user.providerType = thirdPartyUser.providerType
@ -112,6 +129,8 @@ function syncUser(user, thirdPartyUser) {
} }
} }
user = await syncProfilePicture(user, thirdPartyUser)
// profile // profile
user.thirdPartyProfile = { user.thirdPartyProfile = {
...profile._json, ...profile._json,

View file

@ -54,7 +54,11 @@
</Layout> </Layout>
<ActionMenu align="right"> <ActionMenu align="right">
<div slot="control" class="avatar"> <div slot="control" class="avatar">
<Avatar size="M" initials={$auth.initials} /> <Avatar
size="M"
initials={$auth.initials}
url={$auth.user.pictureUrl}
/>
<Icon size="XL" name="ChevronDown" /> <Icon size="XL" name="ChevronDown" />
</div> </div>
<MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}> <MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}>

View file

@ -100,7 +100,11 @@
<div /> <div />
<ActionMenu align="right"> <ActionMenu align="right">
<div slot="control" class="avatar"> <div slot="control" class="avatar">
<Avatar size="M" initials={$auth.initials} /> <Avatar
size="M"
initials={$auth.initials}
url={$auth.user.pictureUrl}
/>
<Icon size="XL" name="ChevronDown" /> <Icon size="XL" name="ChevronDown" />
</div> </div>
<MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}> <MenuItem icon="UserEdit" on:click={() => userInfoModal.show()}>