2023-06-09 08:45:01 +00:00
|
|
|
import { atom } from "jotai";
|
|
|
|
import { APIClient } from "./ApiClient";
|
|
|
|
|
|
|
|
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-09 08:45:01 +00:00
|
|
|
return sessionStorage.getItem(TokenStateKey) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static authStatus = atom(this.SignedIn);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
sessionStorage.setItem(TokenStateKey, res.token);
|
2023-06-06 14:39:47 +00:00
|
|
|
}
|
|
|
|
}
|