1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports["default"] = void 0;
|
5 |
|
6 | var _Client = _interopRequireDefault(require("./Client"));
|
7 |
|
8 | var _CognitoUser = _interopRequireDefault(require("./CognitoUser"));
|
9 |
|
10 | var _StorageHelper = _interopRequireDefault(require("./StorageHelper"));
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var CognitoUserPool = function () {
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
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 |
|
65 |
|
66 |
|
67 |
|
68 | this.advancedSecurityDataCollectionFlag = AdvancedSecurityDataCollectionFlag !== false;
|
69 | this.storage = data.Storage || new _StorageHelper["default"]().getStorage();
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 | var _proto = CognitoUserPool.prototype;
|
77 |
|
78 | _proto.getUserPoolId = function getUserPoolId() {
|
79 | return this.userPoolId;
|
80 | }
|
81 | |
82 |
|
83 |
|
84 | ;
|
85 |
|
86 | _proto.getClientId = function getClientId() {
|
87 | return this.clientId;
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
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 |
|
144 |
|
145 |
|
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 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | ;
|
173 |
|
174 | _proto.getUserContextData = function getUserContextData(username) {
|
175 | if (typeof AmazonCognitoAdvancedSecurityData === 'undefined') {
|
176 | return undefined;
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 | var amazonCognitoAdvancedSecurityDataConst = AmazonCognitoAdvancedSecurityData;
|
182 |
|
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 |
|
201 | exports["default"] = CognitoUserPool; |
\ | No newline at end of file |