fix: cleanup username/email check

Signed-off-by: Tom Opdebeeck <tom.opdebeeck@studiohyperdrive.be>
This commit is contained in:
Tom Opdebeeck
2021-07-19 16:45:52 +02:00
parent cd5932bc66
commit aae88bc3fa
@@ -31,6 +31,7 @@ import {
AuthProviderFactory,
SignInResolver,
AuthHandler,
ProfileInfo,
} from '../types';
import {
OAuthAdapter,
@@ -63,25 +64,28 @@ export type GitlabAuthProviderOptions = OAuthProviderOptions & {
logger: Logger;
};
const extractUserId = (profile: ProfileInfo): string => {
return profile.username || (profile.email?.split('@')[0] as string);
};
function transformResult(result: OAuthResult): OAuthResult {
const { fullProfile, ...authResult } = result;
const profile = makeProfileInfo({
...fullProfile,
photos: [
...(fullProfile.photos ?? []),
...((fullProfile as FullProfile).avatarUrl
? [{ value: (fullProfile as FullProfile).avatarUrl as string }]
: []),
],
});
fullProfile.photos = [
...(fullProfile.photos ?? []),
...((fullProfile as FullProfile).avatarUrl
? [{ value: (fullProfile as FullProfile).avatarUrl as string }]
: []),
];
let id = fullProfile.id;
const profile = makeProfileInfo(fullProfile);
if (profile.email) {
id = profile.email.split('@')[0];
if (!profile.username && !profile.email) {
throw new Error('Profile contained no username or email');
}
fullProfile.id = extractUserId(profile);
return {
...authResult,
fullProfile,
@@ -94,17 +98,17 @@ export const gitlabDefaultSignInResolver: SignInResolver<OAuthResult> = async (
) => {
const { profile } = info;
if (!profile.email) {
throw new Error('Profile contained no email');
if (!profile.username && !profile.email) {
throw new Error('Profile contained no username or email');
}
const userId = profile.email.split('@')[0];
const id = extractUserId(profile);
const token = await ctx.tokenIssuer.issueToken({
claims: { sub: userId, ent: [`user:default/${userId}`] },
claims: { sub: id, ent: [`user:default/${id}`] },
});
return { id: userId, token };
return { id, token };
};
export class GitlabAuthProvider implements OAuthHandlers {