supaapps-auth 2.0.0-rc.6 → 2.0.0-rc.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AuthManager.d.ts +7 -0
- package/dist/AuthManager.js +106 -0
- package/package.json +1 -1
- package/src/AuthManager.ts +134 -0
package/dist/AuthManager.d.ts
CHANGED
|
@@ -18,7 +18,14 @@ export declare class AuthManager {
|
|
|
18
18
|
getLoginWithGoogleUri(): string;
|
|
19
19
|
isLoggedIn(): Promise<boolean>;
|
|
20
20
|
getAccessToken(mustBeLoggedIn?: boolean): Promise<string>;
|
|
21
|
+
verifyEmail(email: string, code: string): Promise<boolean>;
|
|
22
|
+
doPassReset(email: string, code: string, newPassword: string): Promise<boolean>;
|
|
23
|
+
changeEmail(email: string): Promise<boolean>;
|
|
24
|
+
initPasswordReset(email: string): Promise<boolean>;
|
|
25
|
+
changePassword(oldPassword: string, newPassword: string, email: string): Promise<boolean>;
|
|
26
|
+
registerUsingEmail(firstName: string, lastName: string, email: string, password: string): Promise<void>;
|
|
21
27
|
private saveTokens;
|
|
28
|
+
loginUsingEmail(email: string, password: string): Promise<void>;
|
|
22
29
|
loginUsingPkce(code: string): Promise<void>;
|
|
23
30
|
logout(): Promise<void>;
|
|
24
31
|
static validateToken(authServer: string, bearerToken: string): Promise<UserTokenPayload>;
|
package/dist/AuthManager.js
CHANGED
|
@@ -139,6 +139,99 @@ class AuthManager {
|
|
|
139
139
|
}
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
|
+
verifyEmail(email, code) {
|
|
143
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
144
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/verify`, {
|
|
145
|
+
realm_name: this.realmName,
|
|
146
|
+
email,
|
|
147
|
+
code,
|
|
148
|
+
});
|
|
149
|
+
if (response.data.error || response.data.errors) {
|
|
150
|
+
throw new Error(response.data.error || response.data.message);
|
|
151
|
+
}
|
|
152
|
+
return response.status === 200;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
doPassReset(email, code, newPassword) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/do_pass_reset`, {
|
|
158
|
+
realm_name: this.realmName,
|
|
159
|
+
email,
|
|
160
|
+
});
|
|
161
|
+
if (response.data.error || response.data.errors) {
|
|
162
|
+
throw new Error(response.data.error || response.data.message);
|
|
163
|
+
}
|
|
164
|
+
return response.status === 200;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
changeEmail(email) {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
const accessToken = localStorage.getItem('access_token');
|
|
170
|
+
if (!accessToken) {
|
|
171
|
+
throw new Error('Access token not found');
|
|
172
|
+
}
|
|
173
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/change_email`, {
|
|
174
|
+
realm_name: this.realmName,
|
|
175
|
+
email,
|
|
176
|
+
}, {
|
|
177
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
178
|
+
});
|
|
179
|
+
if (response.data.error || response.data.errors) {
|
|
180
|
+
throw new Error(response.data.error || response.data.message);
|
|
181
|
+
}
|
|
182
|
+
return response.status === 200;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
initPasswordReset(email) {
|
|
186
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
187
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/init_pass_reset`, {
|
|
188
|
+
realm_name: this.realmName,
|
|
189
|
+
email,
|
|
190
|
+
});
|
|
191
|
+
if (response.data.error || response.data.errors) {
|
|
192
|
+
throw new Error(response.data.error || response.data.message);
|
|
193
|
+
}
|
|
194
|
+
return response.status === 200 || response.status === 201;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
changePassword(oldPassword, newPassword, email) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
const accessToken = localStorage.getItem('access_token');
|
|
200
|
+
if (!accessToken) {
|
|
201
|
+
throw new Error('Access token not found');
|
|
202
|
+
}
|
|
203
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/change_pass`, {
|
|
204
|
+
realm_name: this.realmName,
|
|
205
|
+
email,
|
|
206
|
+
old_password: oldPassword,
|
|
207
|
+
new_password: newPassword,
|
|
208
|
+
}, {
|
|
209
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
210
|
+
});
|
|
211
|
+
if (response.data.error || response.data.errors) {
|
|
212
|
+
throw new Error(response.data.error || response.data.message);
|
|
213
|
+
}
|
|
214
|
+
return response.status === 200;
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
registerUsingEmail(firstName, lastName, email, password) {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
219
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/register`, {
|
|
220
|
+
realm_name: this.realmName,
|
|
221
|
+
first_name: firstName,
|
|
222
|
+
last_name: lastName,
|
|
223
|
+
email,
|
|
224
|
+
password,
|
|
225
|
+
});
|
|
226
|
+
if (response.data.message || response.data.error) {
|
|
227
|
+
throw new Error(response.data.message || response.data.error);
|
|
228
|
+
}
|
|
229
|
+
if (!response.data.access_token) {
|
|
230
|
+
throw new Error('Something went wrong');
|
|
231
|
+
}
|
|
232
|
+
this.saveTokens(response, false);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
142
235
|
saveTokens(response, byRefresh) {
|
|
143
236
|
localStorage.setItem('access_token', response.data.access_token);
|
|
144
237
|
localStorage.setItem('refresh_token', response.data.refresh_token);
|
|
@@ -149,6 +242,19 @@ class AuthManager {
|
|
|
149
242
|
const user = this.tokenToPayload(response.data.access_token);
|
|
150
243
|
localStorage.setItem('user', JSON.stringify(user));
|
|
151
244
|
}
|
|
245
|
+
loginUsingEmail(email, password) {
|
|
246
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
247
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/login`, {
|
|
248
|
+
realm_name: this.realmName,
|
|
249
|
+
email,
|
|
250
|
+
password,
|
|
251
|
+
});
|
|
252
|
+
if (response.data.message || response.data.error) {
|
|
253
|
+
throw new Error(response.data.message || response.data.error);
|
|
254
|
+
}
|
|
255
|
+
this.saveTokens(response, false);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
152
258
|
loginUsingPkce(code) {
|
|
153
259
|
return __awaiter(this, void 0, void 0, function* () {
|
|
154
260
|
try {
|
package/package.json
CHANGED
package/src/AuthManager.ts
CHANGED
|
@@ -170,6 +170,125 @@ export class AuthManager {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
+
public async verifyEmail(email: string, code: string): Promise<boolean> {
|
|
174
|
+
const response = await axios.post(
|
|
175
|
+
`${this.authServer}auth/email/verify`,
|
|
176
|
+
{
|
|
177
|
+
realm_name: this.realmName,
|
|
178
|
+
email,
|
|
179
|
+
code,
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
if (response.data.error || response.data.errors) {
|
|
183
|
+
throw new Error(response.data.error || response.data.message);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return response.status === 200;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public async doPassReset(email: string, code: string, newPassword: string): Promise<boolean> {
|
|
190
|
+
const response = await axios.post(
|
|
191
|
+
`${this.authServer}auth/email/do_pass_reset`,
|
|
192
|
+
{
|
|
193
|
+
realm_name: this.realmName,
|
|
194
|
+
email,
|
|
195
|
+
},
|
|
196
|
+
);
|
|
197
|
+
if (response.data.error || response.data.errors) {
|
|
198
|
+
throw new Error(response.data.error || response.data.message);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return response.status === 200;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
public async changeEmail(email: string): Promise<boolean> {
|
|
205
|
+
const accessToken = localStorage.getItem('access_token');
|
|
206
|
+
if (!accessToken) {
|
|
207
|
+
throw new Error('Access token not found');
|
|
208
|
+
}
|
|
209
|
+
const response = await axios.post(
|
|
210
|
+
`${this.authServer}auth/email/change_email`,
|
|
211
|
+
{
|
|
212
|
+
realm_name: this.realmName,
|
|
213
|
+
email,
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
217
|
+
},
|
|
218
|
+
);
|
|
219
|
+
if (response.data.error || response.data.errors) {
|
|
220
|
+
throw new Error(response.data.error || response.data.message);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return response.status === 200;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
public async initPasswordReset(email: string): Promise<boolean> {
|
|
227
|
+
const response = await axios.post(
|
|
228
|
+
`${this.authServer}auth/email/init_pass_reset`,
|
|
229
|
+
{
|
|
230
|
+
realm_name: this.realmName,
|
|
231
|
+
email,
|
|
232
|
+
},
|
|
233
|
+
);
|
|
234
|
+
if (response.data.error || response.data.errors) {
|
|
235
|
+
throw new Error(response.data.error || response.data.message);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return response.status === 200 || response.status === 201;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public async changePassword(oldPassword: string, newPassword: string, email: string): Promise<boolean> {
|
|
242
|
+
const accessToken = localStorage.getItem('access_token');
|
|
243
|
+
if (!accessToken) {
|
|
244
|
+
throw new Error('Access token not found');
|
|
245
|
+
}
|
|
246
|
+
const response = await axios.post(
|
|
247
|
+
`${this.authServer}auth/email/change_pass`,
|
|
248
|
+
{
|
|
249
|
+
realm_name: this.realmName,
|
|
250
|
+
email,
|
|
251
|
+
old_password: oldPassword,
|
|
252
|
+
new_password: newPassword,
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
if (response.data.error || response.data.errors) {
|
|
259
|
+
throw new Error(response.data.error || response.data.message);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return response.status === 200;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
public async registerUsingEmail(
|
|
266
|
+
firstName: string,
|
|
267
|
+
lastName: string,
|
|
268
|
+
email: string,
|
|
269
|
+
password: string
|
|
270
|
+
): Promise<void> {
|
|
271
|
+
const response = await axios.post(
|
|
272
|
+
`${this.authServer}auth/email/register`,
|
|
273
|
+
{
|
|
274
|
+
realm_name: this.realmName,
|
|
275
|
+
first_name: firstName,
|
|
276
|
+
last_name: lastName,
|
|
277
|
+
email,
|
|
278
|
+
password,
|
|
279
|
+
},
|
|
280
|
+
);
|
|
281
|
+
if (response.data.message || response.data.error) {
|
|
282
|
+
throw new Error(response.data.message || response.data.error);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (!response.data.access_token) {
|
|
286
|
+
throw new Error('Something went wrong');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
this.saveTokens(response, false);
|
|
290
|
+
}
|
|
291
|
+
|
|
173
292
|
private saveTokens(response: AxiosResponse, byRefresh: boolean): void {
|
|
174
293
|
localStorage.setItem('access_token', response.data.access_token);
|
|
175
294
|
localStorage.setItem(
|
|
@@ -184,6 +303,21 @@ export class AuthManager {
|
|
|
184
303
|
localStorage.setItem('user', JSON.stringify(user));
|
|
185
304
|
}
|
|
186
305
|
|
|
306
|
+
public async loginUsingEmail(email: string, password: string): Promise<void> {
|
|
307
|
+
const response = await axios.post(
|
|
308
|
+
`${this.authServer}auth/email/login`,
|
|
309
|
+
{
|
|
310
|
+
realm_name: this.realmName,
|
|
311
|
+
email,
|
|
312
|
+
password,
|
|
313
|
+
},
|
|
314
|
+
);
|
|
315
|
+
if (response.data.message || response.data.error) {
|
|
316
|
+
throw new Error(response.data.message || response.data.error);
|
|
317
|
+
}
|
|
318
|
+
this.saveTokens(response, false);
|
|
319
|
+
}
|
|
320
|
+
|
|
187
321
|
public async loginUsingPkce(code: string): Promise<void> {
|
|
188
322
|
try {
|
|
189
323
|
const codeVerifier = localStorage.getItem('codeVerifier');
|