2019-08-14 19:11:18 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
import { ErrorUnauthorized } from '@/api/errors/ErrorUnauthorized';
|
|
|
|
import { UpdatedUser, User } from '@/types/users';
|
|
|
|
import { HttpClient } from '@/utils/httpClient';
|
2019-08-14 19:11:18 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-29 14:24:16 +00:00
|
|
|
* AuthHttpApi is a console Auth API.
|
2019-08-14 19:11:18 +01:00
|
|
|
* Exposes all auth-related functionality
|
|
|
|
*/
|
2019-10-29 14:24:16 +00:00
|
|
|
export class AuthHttpApi {
|
|
|
|
private readonly http: HttpClient = new HttpClient();
|
|
|
|
private readonly ROOT_PATH: string = '/api/v0/auth';
|
2019-11-06 12:27:26 +00:00
|
|
|
|
2019-08-14 19:11:18 +01:00
|
|
|
/**
|
|
|
|
* Used to resend an registration confirmation email
|
|
|
|
*
|
|
|
|
* @param userId - id of newly created user
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async resendEmail(userId: string): Promise<void> {
|
2019-10-29 14:24:16 +00:00
|
|
|
const path = `${this.ROOT_PATH}/resend-email/${userId}`;
|
|
|
|
const response = await this.http.post(path, userId, false);
|
|
|
|
if (response.ok) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
throw new Error('can not resend Email');
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to get authentication token
|
|
|
|
*
|
|
|
|
* @param email - email of the user
|
|
|
|
* @param password - password of the user
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async token(email: string, password: string): Promise<string> {
|
2019-10-29 14:24:16 +00:00
|
|
|
const path = `${this.ROOT_PATH}/token`;
|
|
|
|
const body = {
|
|
|
|
email: email,
|
|
|
|
password: password,
|
2019-08-14 19:11:18 +01:00
|
|
|
};
|
2019-10-29 14:24:16 +00:00
|
|
|
const response = await this.http.post(path, JSON.stringify(body), false);
|
|
|
|
if (response.ok) {
|
|
|
|
return await response.json();
|
|
|
|
}
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2019-11-05 11:55:26 +00:00
|
|
|
if (response.status === 400) {
|
|
|
|
throw new Error('your email or password was incorrect, please try again');
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
throw new Error('can not receive authentication token');
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to restore password
|
|
|
|
*
|
|
|
|
* @param email - email of the user
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async forgotPassword(email: string): Promise<void> {
|
2019-10-29 14:24:16 +00:00
|
|
|
const path = `${this.ROOT_PATH}/forgot-password/${email}`;
|
|
|
|
const response = await this.http.post(path, email, false);
|
|
|
|
if (response.ok) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
throw new Error('can not resend password');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to update user full and short name
|
|
|
|
*
|
|
|
|
* @param userInfo - full name and short name of the user
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async update(userInfo: UpdatedUser): Promise<void> {
|
|
|
|
const path = `${this.ROOT_PATH}/account`;
|
|
|
|
const body = {
|
|
|
|
fullName: userInfo.fullName,
|
|
|
|
shortName: userInfo.shortName,
|
2019-08-14 19:11:18 +01:00
|
|
|
};
|
2019-10-29 14:24:16 +00:00
|
|
|
const response = await this.http.patch(path, JSON.stringify(body), true);
|
|
|
|
if (response.ok) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
throw new Error('can not update user data');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to get user data
|
|
|
|
*
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async get(): Promise<User> {
|
|
|
|
const path = `${this.ROOT_PATH}/account`;
|
|
|
|
const response = await this.http.get(path, true);
|
|
|
|
if (response.ok) {
|
|
|
|
return await response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('can not get user data');
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to change password
|
|
|
|
*
|
|
|
|
* @param password - old password of the user
|
|
|
|
* @param newPassword - new password of the user
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async changePassword(password: string, newPassword: string): Promise<void> {
|
2019-10-29 14:24:16 +00:00
|
|
|
const path = `${this.ROOT_PATH}/account/change-password`;
|
|
|
|
const body = {
|
|
|
|
password: password,
|
|
|
|
newPassword: newPassword,
|
2019-08-14 19:11:18 +01:00
|
|
|
};
|
2019-10-29 14:24:16 +00:00
|
|
|
const response = await this.http.post(path, JSON.stringify(body), true);
|
|
|
|
if (response.ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:55:26 +00:00
|
|
|
switch (response.status) {
|
|
|
|
case 401: {
|
|
|
|
throw new ErrorUnauthorized();
|
|
|
|
}
|
|
|
|
case 500: {
|
|
|
|
throw new Error('can not change password');
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error('old password is incorrect, please try again');
|
|
|
|
}
|
2019-10-29 14:24:16 +00:00
|
|
|
}
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to delete account
|
|
|
|
*
|
|
|
|
* @param password - password of the user
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async delete(password: string): Promise<void> {
|
2019-10-29 14:24:16 +00:00
|
|
|
const path = `${this.ROOT_PATH}/account/delete`;
|
|
|
|
const body = {
|
|
|
|
password: password,
|
2019-08-14 19:11:18 +01:00
|
|
|
};
|
2019-10-29 14:24:16 +00:00
|
|
|
const response = await this.http.post(path, JSON.stringify(body), true);
|
|
|
|
if (response.ok) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
if (response.status === 401) {
|
|
|
|
throw new ErrorUnauthorized();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('can not delete user');
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove secret after Vanguard release
|
|
|
|
/**
|
2019-10-29 14:24:16 +00:00
|
|
|
* Used to register account
|
2019-08-14 19:11:18 +01:00
|
|
|
*
|
|
|
|
* @param user - stores user information
|
|
|
|
* @param secret - registration token used in Vanguard release
|
2019-10-29 14:24:16 +00:00
|
|
|
* @param referrerUserId - referral id to participate in bonus program
|
2019-08-14 19:11:18 +01:00
|
|
|
* @returns id of created user
|
|
|
|
* @throws Error
|
|
|
|
*/
|
2019-10-29 14:24:16 +00:00
|
|
|
public async register(user: {fullName: string; shortName: string; email: string; partnerId: string; password: string}, secret: string, referrerUserId: string): Promise<string> {
|
|
|
|
const path = `${this.ROOT_PATH}/register`;
|
|
|
|
const body = {
|
|
|
|
secret: secret,
|
|
|
|
referrerUserId: referrerUserId ? referrerUserId : '',
|
|
|
|
password: user.password,
|
2019-08-14 19:11:18 +01:00
|
|
|
fullName: user.fullName,
|
|
|
|
shortName: user.shortName,
|
2019-10-29 14:24:16 +00:00
|
|
|
email: user.email,
|
|
|
|
partnerId: user.partnerId ? user.partnerId : '',
|
2019-08-14 19:11:18 +01:00
|
|
|
};
|
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
const response = await this.http.post(path, JSON.stringify(body), false);
|
|
|
|
if (!response.ok) {
|
2019-11-05 11:55:26 +00:00
|
|
|
if (response.status === 400) {
|
|
|
|
throw new Error('we are unable to create your account. This is an invite-only alpha, please join our waitlist to receive an invitation');
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
throw new Error('can not register user');
|
|
|
|
}
|
2019-08-14 19:11:18 +01:00
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
return await response.json();
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
|
|
|
}
|