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