UNPKG

idea-toolbox

Version:
64 lines (63 loc) 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CognitoUser = void 0; const utils_1 = require("./utils"); /** * A user stored in a Cognito User Pool. */ class CognitoUser { constructor(x = {}) { this.userId = x.userId || x.sub; this.email = x.email; this.name = x.name; this.picture = x.picture; this.groups = x.groups || this.parseGroupsFromClaims(x['cognito:groups']); this.disabled = x.disabled; if (x.attributes) this.attributes = x.attributes; else { this.attributes = {}; Object.keys(x) .filter(a => a.startsWith('custom:')) .forEach(a => (this.attributes[a.slice('custom:'.length)] = x[a])); } } // the groups claim can come in many forms, depending on the input payload type and version. parseGroupsFromClaims(groupsClaim) { if (!groupsClaim) return []; // ['myGroup1', 'myGroup2'] if (Array.isArray(groupsClaim)) return groupsClaim; // '[myGroup1 myGroup2]' if (groupsClaim.startsWith('[')) return groupsClaim.slice(1, groupsClaim.length - 1).split(' '); // 'myGroup1, myGroup2' else return groupsClaim.split(','); } /** * Check whether the user's attributes are valid. */ validate() { const e = []; if ((0, utils_1.isEmpty)(this.name)) e.push('name'); if ((0, utils_1.isEmpty)(this.email, 'email')) e.push('email'); return e; } /** * Whether the user is part of the administrators group. */ isAdmin() { return this.groups.includes('admins'); } /** * Whether the user is part of the robots group. */ isRobot() { return this.groups.includes('robots'); } } exports.CognitoUser = CognitoUser;