UNPKG

6.2 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5
6var _Client = _interopRequireDefault(require("./Client"));
7
8var _CognitoUser = _interopRequireDefault(require("./CognitoUser"));
9
10var _StorageHelper = _interopRequireDefault(require("./StorageHelper"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
14/*!
15 * Copyright 2016 Amazon.com,
16 * Inc. or its affiliates. All Rights Reserved.
17 *
18 * Licensed under the Amazon Software License (the "License").
19 * You may not use this file except in compliance with the
20 * License. A copy of the License is located at
21 *
22 * http://aws.amazon.com/asl/
23 *
24 * or in the "license" file accompanying this file. This file is
25 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
26 * CONDITIONS OF ANY KIND, express or implied. See the License
27 * for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31/** @class */
32var CognitoUserPool = /*#__PURE__*/function () {
33 /**
34 * Constructs a new CognitoUserPool object
35 * @param {object} data Creation options.
36 * @param {string} data.UserPoolId Cognito user pool id.
37 * @param {string} data.ClientId User pool application client id.
38 * @param {object} data.Storage Optional storage object.
39 * @param {boolean} data.AdvancedSecurityDataCollectionFlag Optional:
40 * boolean flag indicating if the data collection is enabled
41 * to support cognito advanced security features. By default, this
42 * flag is set to true.
43 */
44 function CognitoUserPool(data) {
45 var _ref = data || {},
46 UserPoolId = _ref.UserPoolId,
47 ClientId = _ref.ClientId,
48 endpoint = _ref.endpoint,
49 AdvancedSecurityDataCollectionFlag = _ref.AdvancedSecurityDataCollectionFlag;
50
51 if (!UserPoolId || !ClientId) {
52 throw new Error('Both UserPoolId and ClientId are required.');
53 }
54
55 if (!/^[\w-]+_.+$/.test(UserPoolId)) {
56 throw new Error('Invalid UserPoolId format.');
57 }
58
59 var region = UserPoolId.split('_')[0];
60 this.userPoolId = UserPoolId;
61 this.clientId = ClientId;
62 this.client = new _Client["default"](region, endpoint);
63 /**
64 * By default, AdvancedSecurityDataCollectionFlag is set to true,
65 * if no input value is provided.
66 */
67
68 this.advancedSecurityDataCollectionFlag = AdvancedSecurityDataCollectionFlag !== false;
69 this.storage = data.Storage || new _StorageHelper["default"]().getStorage();
70 }
71 /**
72 * @returns {string} the user pool id
73 */
74
75
76 var _proto = CognitoUserPool.prototype;
77
78 _proto.getUserPoolId = function getUserPoolId() {
79 return this.userPoolId;
80 }
81 /**
82 * @returns {string} the client id
83 */
84 ;
85
86 _proto.getClientId = function getClientId() {
87 return this.clientId;
88 }
89 /**
90 * @typedef {object} SignUpResult
91 * @property {CognitoUser} user New user.
92 * @property {bool} userConfirmed If the user is already confirmed.
93 */
94
95 /**
96 * method for signing up a user
97 * @param {string} username User's username.
98 * @param {string} password Plain-text initial password entered by user.
99 * @param {(AttributeArg[])=} userAttributes New user attributes.
100 * @param {(AttributeArg[])=} validationData Application metadata.
101 * @param {(AttributeArg[])=} clientMetadata Client metadata.
102 * @param {nodeCallback<SignUpResult>} callback Called on error or with the new user.
103 * @returns {void}
104 */
105 ;
106
107 _proto.signUp = function signUp(username, password, userAttributes, validationData, callback, clientMetadata) {
108 var _this = this;
109
110 var jsonReq = {
111 ClientId: this.clientId,
112 Username: username,
113 Password: password,
114 UserAttributes: userAttributes,
115 ValidationData: validationData,
116 ClientMetadata: clientMetadata
117 };
118
119 if (this.getUserContextData(username)) {
120 jsonReq.UserContextData = this.getUserContextData(username);
121 }
122
123 this.client.request('SignUp', jsonReq, function (err, data) {
124 if (err) {
125 return callback(err, null);
126 }
127
128 var cognitoUser = {
129 Username: username,
130 Pool: _this,
131 Storage: _this.storage
132 };
133 var returnData = {
134 user: new _CognitoUser["default"](cognitoUser),
135 userConfirmed: data.UserConfirmed,
136 userSub: data.UserSub,
137 codeDeliveryDetails: data.CodeDeliveryDetails
138 };
139 return callback(null, returnData);
140 });
141 }
142 /**
143 * method for getting the current user of the application from the local storage
144 *
145 * @returns {CognitoUser} the user retrieved from storage
146 */
147 ;
148
149 _proto.getCurrentUser = function getCurrentUser() {
150 var lastUserKey = "CognitoIdentityServiceProvider." + this.clientId + ".LastAuthUser";
151 var lastAuthUser = this.storage.getItem(lastUserKey);
152
153 if (lastAuthUser) {
154 var cognitoUser = {
155 Username: lastAuthUser,
156 Pool: this,
157 Storage: this.storage
158 };
159 return new _CognitoUser["default"](cognitoUser);
160 }
161
162 return null;
163 }
164 /**
165 * This method returns the encoded data string used for cognito advanced security feature.
166 * This would be generated only when developer has included the JS used for collecting the
167 * data on their client. Please refer to documentation to know more about using AdvancedSecurity
168 * features
169 * @param {string} username the username for the context data
170 * @returns {string} the user context data
171 **/
172 ;
173
174 _proto.getUserContextData = function getUserContextData(username) {
175 if (typeof AmazonCognitoAdvancedSecurityData === 'undefined') {
176 return undefined;
177 }
178 /* eslint-disable */
179
180
181 var amazonCognitoAdvancedSecurityDataConst = AmazonCognitoAdvancedSecurityData;
182 /* eslint-enable */
183
184 if (this.advancedSecurityDataCollectionFlag) {
185 var advancedSecurityData = amazonCognitoAdvancedSecurityDataConst.getData(username, this.userPoolId, this.clientId);
186
187 if (advancedSecurityData) {
188 var userContextData = {
189 EncodedData: advancedSecurityData
190 };
191 return userContextData;
192 }
193 }
194
195 return {};
196 };
197
198 return CognitoUserPool;
199}();
200
201exports["default"] = CognitoUserPool;
\No newline at end of file