UNPKG

@quinck/aws-cognito-client

Version:

Provides a user attributes generic cognito client.

105 lines (104 loc) 4.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BasicCognitoService = void 0; const client_cognito_identity_provider_1 = require("@aws-sdk/client-cognito-identity-provider"); require("@quinck/collections"); const errors_1 = require("../utils/errors"); class BasicCognitoService { constructor(config) { const { userPoolId, userStructure, cognitoIdentityProviderClientConfig, fitSignUpInfo, fitUserUpdateInfo, } = config; this.cognitoIdentityProvider = new client_cognito_identity_provider_1.CognitoIdentityProvider(cognitoIdentityProviderClientConfig ?? {}); this.userStructure = userStructure; this.userAttributes = Object.entries(userStructure); this.fitSignUpInfo = fitSignUpInfo || BasicCognitoService.DEFAULT_FIT_INFO; this.fitUserUpdateInfo = fitUserUpdateInfo || BasicCognitoService.DEFAULT_FIT_INFO; this.userPoolId = userPoolId; } async tryDo(fun) { try { return await fun(); } catch (error) { throw this.createError(error); } } createAttributesFromObject(user, defaultIfUndefined = true) { const attributes = this.userAttributes.collect([ [ ([key]) => user[key] != undefined, ([key, { cognitoName, stringify }]) => ({ Name: cognitoName, Value: stringify(user[key]), }), ], [ ([, { defaultValue }]) => defaultIfUndefined && defaultValue != undefined, ([, { cognitoName, stringify, defaultValue }]) => ({ Name: cognitoName, Value: stringify(defaultValue), }), ], ]); return attributes; } createUserInfoAttributesFromAttributes(attributes) { const attributesByName = attributes.groupBy(x => x.Name, x => x); return this.userAttributes.reduce((userInfo, attribute) => { const [key, { cognitoName }] = attribute; const attr = attributesByName.get(cognitoName); if (attr != undefined) userInfo[key] = this.userStructure[key].parse(attr.Value); else userInfo[key] = this.userStructure[key].defaultValue; return userInfo; }, {}); } getBasicUserInfo(username) { return { id: username }; } createUserInfo(username, attributes) { return { ...this.getBasicUserInfo(username), ...this.createUserInfoAttributesFromAttributes(attributes), }; } parseUserAttributes(attributes) { return attributes.singleCollect(attr => attr.Name && attr.Value, ({ Name, Value }) => ({ Name: Name, Value: Value || '', })); } createError(error) { if (!(error instanceof Error)) return new errors_1.UnknownInternalError(error); if (error instanceof errors_1.BasicError) return error; switch (error.name) { case client_cognito_identity_provider_1.UserNotFoundException.name: case errors_1.UserNotFoundError.name: return new errors_1.UserNotFoundError(error); case client_cognito_identity_provider_1.NotAuthorizedException.name: return new errors_1.WrongUsernameOrPasswordError(error); case client_cognito_identity_provider_1.UsernameExistsException.name: return new errors_1.UserAlreadyExistsError(error); case client_cognito_identity_provider_1.UnauthorizedException.name: return new errors_1.UnauthorizedError(error); case errors_1.ForceChangePasswordError.name: return new errors_1.ForceChangePasswordError(error); case errors_1.UserNotRetrievedError.name: return new errors_1.UserNotRetrievedError(error); case client_cognito_identity_provider_1.InvalidPasswordException.name: return new errors_1.InvalidPasswordError(error); case client_cognito_identity_provider_1.ExpiredCodeException.name: return new errors_1.InvalidOrExpiredCodeError(error); case client_cognito_identity_provider_1.UserNotConfirmedException.name: return new errors_1.UserNotConfirmedError(error); default: return error; } } } exports.BasicCognitoService = BasicCognitoService; BasicCognitoService.DEFAULT_FIT_INFO = x => x;