Add authentication layer
This commit is contained in:
70
central_frontend/src/api/AuthApi.ts
Normal file
70
central_frontend/src/api/AuthApi.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import { APIClient } from "./ApiClient";
|
||||
|
||||
export interface AuthInfo {
|
||||
name: string;
|
||||
}
|
||||
|
||||
const TokenStateKey = "auth-state";
|
||||
|
||||
export class AuthApi {
|
||||
/**
|
||||
* Check out whether user is signed in or not
|
||||
*/
|
||||
static get SignedIn(): boolean {
|
||||
return localStorage.getItem(TokenStateKey) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark user as authenticated
|
||||
*/
|
||||
static SetAuthenticated() {
|
||||
localStorage.setItem(TokenStateKey, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Un-mark user as authenticated
|
||||
*/
|
||||
static UnsetAuthenticated() {
|
||||
localStorage.removeItem(TokenStateKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate using user and password
|
||||
*/
|
||||
static async AuthWithPassword(user: string, password: string): Promise<void> {
|
||||
await APIClient.exec({
|
||||
uri: "/auth/password_auth",
|
||||
method: "POST",
|
||||
jsonData: {
|
||||
user,
|
||||
password,
|
||||
},
|
||||
});
|
||||
|
||||
this.SetAuthenticated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get auth information
|
||||
*/
|
||||
static async GetAuthInfo(): Promise<AuthInfo> {
|
||||
return (
|
||||
await APIClient.exec({
|
||||
uri: "/auth/info",
|
||||
method: "GET",
|
||||
})
|
||||
).data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign out
|
||||
*/
|
||||
static async SignOut(): Promise<void> {
|
||||
await APIClient.exec({
|
||||
uri: "/auth/sign_out",
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
this.UnsetAuthenticated();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user