fix: split email for username if preferred_username is unavailable
This commit is contained in:
parent
222ac7a279
commit
9a5952adcb
@ -1,5 +1,5 @@
|
|||||||
import * as client from 'openid-client';
|
import * as client from 'openid-client';
|
||||||
import { Configuration } from 'openid-client';
|
import { Configuration, IDToken, UserInfoResponse } from 'openid-client';
|
||||||
import log from '~/utils/log';
|
import log from '~/utils/log';
|
||||||
|
|
||||||
// We try our best to infer the callback URI of our Headplane instance
|
// We try our best to infer the callback URI of our Headplane instance
|
||||||
@ -99,11 +99,34 @@ export async function finishAuthFlow(
|
|||||||
subject: user.sub,
|
subject: user.sub,
|
||||||
name: getName(user, claims),
|
name: getName(user, claims),
|
||||||
email: user.email ?? claims.email?.toString(),
|
email: user.email ?? claims.email?.toString(),
|
||||||
username: user.preferred_username ?? claims.preferred_username?.toString(),
|
username: calculateUsername(claims, user),
|
||||||
picture: user.picture,
|
picture: user.picture,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function calculateUsername(claims: IDToken, user: UserInfoResponse) {
|
||||||
|
if (user.preferred_username) {
|
||||||
|
return user.preferred_username;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
claims.preferred_username &&
|
||||||
|
typeof claims.preferred_username === 'string'
|
||||||
|
) {
|
||||||
|
return claims.preferred_username;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.email) {
|
||||||
|
return user.email.split('@')[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (claims.email && typeof claims.email === 'string') {
|
||||||
|
return claims.email.split('@')[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
function getName(user: client.UserInfoResponse, claims: client.IDToken) {
|
function getName(user: client.UserInfoResponse, claims: client.IDToken) {
|
||||||
if (user.name) {
|
if (user.name) {
|
||||||
return user.name;
|
return user.name;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user