UNPKG

amazon-cognito-identity-js

Version:

Amazon Cognito Identity Provider JavaScript SDK

178 lines (167 loc) 6.43 kB
"use strict"; exports.__esModule = true; exports["default"] = void 0; var _Client = _interopRequireDefault(require("./Client")); var _CognitoUser = _interopRequireDefault(require("./CognitoUser")); var _StorageHelper = _interopRequireDefault(require("./StorageHelper")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } /*! * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ var USER_POOL_ID_MAX_LENGTH = 55; /** @class */ var CognitoUserPool = exports["default"] = /*#__PURE__*/function () { /** * Constructs a new CognitoUserPool object * @param {object} data Creation options. * @param {string} data.UserPoolId Cognito user pool id. * @param {string} data.ClientId User pool application client id. * @param {string} data.endpoint Optional custom service endpoint. * @param {object} data.fetchOptions Optional options for fetch API. * (only credentials option is supported) * @param {object} data.Storage Optional storage object. * @param {boolean} data.AdvancedSecurityDataCollectionFlag Optional: * boolean flag indicating if the data collection is enabled * to support cognito advanced security features. By default, this * flag is set to true. */ function CognitoUserPool(data, wrapRefreshSessionCallback) { var _ref = data || {}, UserPoolId = _ref.UserPoolId, ClientId = _ref.ClientId, endpoint = _ref.endpoint, fetchOptions = _ref.fetchOptions, AdvancedSecurityDataCollectionFlag = _ref.AdvancedSecurityDataCollectionFlag; if (!UserPoolId || !ClientId) { throw new Error('Both UserPoolId and ClientId are required.'); } if (UserPoolId.length > USER_POOL_ID_MAX_LENGTH || !/^[\w-]+_[0-9a-zA-Z]+$/.test(UserPoolId)) { throw new Error('Invalid UserPoolId format.'); } var region = UserPoolId.split('_')[0]; this.userPoolId = UserPoolId; this.clientId = ClientId; this.client = new _Client["default"](region, endpoint, fetchOptions); /** * By default, AdvancedSecurityDataCollectionFlag is set to true, * if no input value is provided. */ this.advancedSecurityDataCollectionFlag = AdvancedSecurityDataCollectionFlag !== false; this.storage = data.Storage || new _StorageHelper["default"]().getStorage(); if (wrapRefreshSessionCallback) { this.wrapRefreshSessionCallback = wrapRefreshSessionCallback; } } /** * @returns {string} the user pool id */ var _proto = CognitoUserPool.prototype; _proto.getUserPoolId = function getUserPoolId() { return this.userPoolId; } /** * @returns {string} the user pool name */; _proto.getUserPoolName = function getUserPoolName() { return this.getUserPoolId().split('_')[1]; } /** * @returns {string} the client id */; _proto.getClientId = function getClientId() { return this.clientId; } /** * @typedef {object} SignUpResult * @property {CognitoUser} user New user. * @property {bool} userConfirmed If the user is already confirmed. */ /** * method for signing up a user * @param {string} username User's username. * @param {string} password Plain-text initial password entered by user. * @param {(AttributeArg[])=} userAttributes New user attributes. * @param {(AttributeArg[])=} validationData Application metadata. * @param {(AttributeArg[])=} clientMetadata Client metadata. * @param {nodeCallback<SignUpResult>} callback Called on error or with the new user. * @param {ClientMetadata} clientMetadata object which is passed from client to Cognito Lambda trigger * @returns {void} */; _proto.signUp = function signUp(username, password, userAttributes, validationData, callback, clientMetadata) { var _this = this; var jsonReq = { ClientId: this.clientId, Username: username, Password: password, UserAttributes: userAttributes, ValidationData: validationData, ClientMetadata: clientMetadata }; if (this.getUserContextData(username)) { jsonReq.UserContextData = this.getUserContextData(username); } this.client.request('SignUp', jsonReq, function (err, data) { if (err) { return callback(err, null); } var cognitoUser = { Username: username, Pool: _this, Storage: _this.storage }; var returnData = { user: new _CognitoUser["default"](cognitoUser), userConfirmed: data.UserConfirmed, userSub: data.UserSub, codeDeliveryDetails: data.CodeDeliveryDetails }; return callback(null, returnData); }); } /** * method for getting the current user of the application from the local storage * * @returns {CognitoUser} the user retrieved from storage */; _proto.getCurrentUser = function getCurrentUser() { var lastUserKey = "CognitoIdentityServiceProvider." + this.clientId + ".LastAuthUser"; var lastAuthUser = this.storage.getItem(lastUserKey); if (lastAuthUser) { var cognitoUser = { Username: lastAuthUser, Pool: this, Storage: this.storage }; return new _CognitoUser["default"](cognitoUser); } return null; } /** * This method returns the encoded data string used for cognito advanced security feature. * This would be generated only when developer has included the JS used for collecting the * data on their client. Please refer to documentation to know more about using AdvancedSecurity * features * @param {string} username the username for the context data * @returns {string} the user context data **/; _proto.getUserContextData = function getUserContextData(username) { if (typeof AmazonCognitoAdvancedSecurityData === 'undefined') { return undefined; } /* eslint-disable */ var amazonCognitoAdvancedSecurityDataConst = AmazonCognitoAdvancedSecurityData; /* eslint-enable */ if (this.advancedSecurityDataCollectionFlag) { var advancedSecurityData = amazonCognitoAdvancedSecurityDataConst.getData(username, this.userPoolId, this.clientId); if (advancedSecurityData) { var userContextData = { EncodedData: advancedSecurityData }; return userContextData; } } return {}; }; return CognitoUserPool; }();