Merge pull request #2175 from Budibase/feature/sso-sync-picture
Sync profile picture when using SSO
This commit is contained in:
commit
5a83b177d2
|
@ -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 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,
|
||||||
|
|
|
@ -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()}>
|
||||||
|
|
|
@ -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()}>
|
||||||
|
|
Loading…
Reference in New Issue