Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import { Injectable, Logger } from '@nestjs/common'; import axios from 'axios'; import { object, string } from 'yup'; import { toError } from '@amalia/ext/typescript'; import { InjectAuthConfig, type AuthConfig } from '../config/auth.config'; const userInfoSchema = object({ picture: string().optional().default(''), }); export type Auth0UserInfo = { sub: string; name: string; given_name: string; family_name: string; middle_name: string; nickname: string; preferred_username: string; profile: string; picture: string; website: string; email: string; email_verified: boolean; gender: string; birthdate: string; zoneinfo: string; locale: string; phone_number: string; phone_number_verified: boolean; address: { country: string; }; updated_at: string; }; @Injectable() export class IdentityProviderService { private readonly logger = new Logger(IdentityProviderService.name); public constructor(@InjectAuthConfig() private readonly authConfig: Pick<AuthConfig, 'userInfoUrl'>) {} public async getUserInfo(accessToken: string) { const { userInfoUrl } = this.authConfig; const { data: infos } = await axios.get<Auth0UserInfo>(userInfoUrl, { headers: { Authorization: `Bearer ${accessToken}` }, }); try { // Only keep what you wanted to keep return await userInfoSchema.validate(infos, { // Collect all errors then Throw abortEarly: false, // Remove unspecified keys from objects stripUnknown: true, }); } catch (e) { const error = toError(e); this.logger.error({ message: 'Error while getting user infos from Auth0', error, infos, }); return { picture: undefined }; } } } |