2023-06-09 08:45:01 +00:00
|
|
|
import { APIClient } from "./ApiClient";
|
|
|
|
|
2023-06-13 08:06:04 +00:00
|
|
|
export enum CreateAccountResult {
|
|
|
|
TooManyRequests,
|
|
|
|
BadInputData,
|
|
|
|
MailAlreadyExists,
|
|
|
|
Success,
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
2023-06-13 08:42:28 +00:00
|
|
|
export enum PasswordLoginResult {
|
|
|
|
TooManyRequests,
|
|
|
|
InvalidCredentials,
|
|
|
|
Success,
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
2023-06-12 14:25:38 +00:00
|
|
|
export interface CheckResetTokenResponse {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2023-06-09 08:45:01 +00:00
|
|
|
const TokenStateKey = "auth-token";
|
|
|
|
|
2023-06-06 14:39:47 +00:00
|
|
|
export class AuthApi {
|
|
|
|
/**
|
|
|
|
* Check out whether user is signed in or not
|
|
|
|
*/
|
|
|
|
static get SignedIn(): boolean {
|
2023-06-13 13:29:15 +00:00
|
|
|
return localStorage.getItem(TokenStateKey) !== null;
|
2023-06-09 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 09:19:40 +00:00
|
|
|
/**
|
|
|
|
* Get user auth token
|
|
|
|
*/
|
|
|
|
static get AuthToken(): string {
|
|
|
|
if (!this.SignedIn) throw new Error("User is not authenticated!");
|
2023-06-13 13:29:15 +00:00
|
|
|
return localStorage.getItem(TokenStateKey)!;
|
2023-06-09 09:19:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 08:06:04 +00:00
|
|
|
/**
|
|
|
|
* Create a new account
|
|
|
|
*/
|
|
|
|
static async CreateAccount(
|
|
|
|
name: string,
|
|
|
|
mail: string
|
|
|
|
): Promise<CreateAccountResult> {
|
|
|
|
const res = await APIClient.exec({
|
|
|
|
uri: "/auth/create_account",
|
|
|
|
method: "POST",
|
|
|
|
allowFail: true,
|
|
|
|
jsonData: {
|
|
|
|
name: name,
|
|
|
|
email: mail,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
switch (res.status) {
|
|
|
|
case 429:
|
|
|
|
return CreateAccountResult.TooManyRequests;
|
|
|
|
case 400:
|
|
|
|
return CreateAccountResult.BadInputData;
|
|
|
|
case 409:
|
|
|
|
return CreateAccountResult.MailAlreadyExists;
|
|
|
|
case 200:
|
|
|
|
case 201:
|
|
|
|
return CreateAccountResult.Success;
|
|
|
|
default:
|
|
|
|
return CreateAccountResult.Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 08:42:28 +00:00
|
|
|
/**
|
|
|
|
* Authenticate using an email and a password
|
|
|
|
*
|
|
|
|
* @param mail The email address to use
|
|
|
|
* @param password The password to use
|
|
|
|
*/
|
|
|
|
static async LoginWithPassword(
|
|
|
|
mail: string,
|
|
|
|
password: string
|
|
|
|
): Promise<PasswordLoginResult> {
|
|
|
|
const res = await APIClient.exec({
|
|
|
|
uri: "/auth/password_login",
|
|
|
|
method: "POST",
|
|
|
|
allowFail: true,
|
|
|
|
jsonData: {
|
|
|
|
mail: mail,
|
|
|
|
password: password,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
switch (res.status) {
|
|
|
|
case 429:
|
|
|
|
return PasswordLoginResult.TooManyRequests;
|
|
|
|
case 401:
|
|
|
|
return PasswordLoginResult.InvalidCredentials;
|
|
|
|
case 200:
|
|
|
|
case 201:
|
2023-06-13 13:29:15 +00:00
|
|
|
localStorage.setItem(TokenStateKey, res.data.token);
|
2023-06-13 08:42:28 +00:00
|
|
|
return PasswordLoginResult.Success;
|
|
|
|
default:
|
|
|
|
return PasswordLoginResult.Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 08:45:01 +00:00
|
|
|
/**
|
|
|
|
* Start OpenID login
|
|
|
|
*
|
|
|
|
* @param id The ID of the OIDC provider to use
|
|
|
|
*/
|
|
|
|
static async StartOpenIDLogin(id: string): Promise<{ url: string }> {
|
|
|
|
return (
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/auth/start_openid_login",
|
|
|
|
method: "POST",
|
|
|
|
jsonData: { provider: id },
|
|
|
|
})
|
|
|
|
).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish OpenID login
|
|
|
|
*/
|
|
|
|
static async FinishOpenIDLogin(code: string, state: string): Promise<void> {
|
|
|
|
const res: { user_id: number; token: string } = (
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/auth/finish_openid_login",
|
|
|
|
method: "POST",
|
|
|
|
jsonData: { code: code, state: state },
|
|
|
|
})
|
|
|
|
).data;
|
|
|
|
|
2023-06-13 13:29:15 +00:00
|
|
|
localStorage.setItem(TokenStateKey, res.token);
|
2023-06-06 14:39:47 +00:00
|
|
|
}
|
2023-06-09 16:27:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sign out
|
|
|
|
*/
|
|
|
|
static async SignOut(): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/auth/logout",
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
|
2023-06-13 13:29:15 +00:00
|
|
|
this.RemoveAuthToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove auth token from storage
|
|
|
|
*/
|
|
|
|
static RemoveAuthToken() {
|
|
|
|
localStorage.removeItem(TokenStateKey);
|
2023-06-09 16:27:03 +00:00
|
|
|
}
|
2023-06-09 16:55:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request to reset password
|
|
|
|
*/
|
|
|
|
static async RequestResetPassword(mail: string): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/auth/request_reset_password",
|
|
|
|
method: "POST",
|
|
|
|
jsonData: { mail: mail },
|
|
|
|
});
|
|
|
|
}
|
2023-06-12 14:25:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check reset password token
|
|
|
|
*/
|
|
|
|
static async CheckResetPasswordToken(
|
|
|
|
token: string
|
|
|
|
): Promise<CheckResetTokenResponse> {
|
|
|
|
return (
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/auth/check_reset_password_token",
|
|
|
|
method: "POST",
|
|
|
|
jsonData: { token: token },
|
|
|
|
})
|
|
|
|
).data;
|
|
|
|
}
|
2023-06-12 17:10:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset password
|
|
|
|
*/
|
|
|
|
static async ResetPassword(
|
|
|
|
token: string,
|
|
|
|
newPassword: string
|
|
|
|
): Promise<void> {
|
|
|
|
await APIClient.exec({
|
|
|
|
uri: "/auth/reset_password",
|
|
|
|
method: "POST",
|
|
|
|
jsonData: { token: token, password: newPassword },
|
|
|
|
});
|
|
|
|
}
|
2023-06-06 14:39:47 +00:00
|
|
|
}
|