import { APIClient } from "./ApiClient"; export interface UserInfo { id: number; time_create: number; time_update: number; name: string; email: string; matrix_account_connected: boolean; matrix_user_id?: string; matrix_device_id?: string; matrix_recovery_state?: "Enabled" | "Disabled" | "Unknown" | "Incomplete"; } 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); } /** * Start OpenID login */ static async StartOpenIDLogin(): Promise<{ url: string }> { return ( await APIClient.exec({ uri: "/auth/start_oidc", method: "GET", }) ).data; } /** * Finish OpenID login */ static async FinishOpenIDLogin(code: string, state: string): Promise { await APIClient.exec({ uri: "/auth/finish_oidc", method: "POST", jsonData: { code: code, state: state }, }); this.SetAuthenticated(); } /** * Get user information */ static async GetUserInfo(): Promise { return ( await APIClient.exec({ uri: "/auth/info", method: "GET", }) ).data; } /** * Sign out */ static async SignOut(): Promise { try { await APIClient.exec({ uri: "/auth/sign_out", method: "GET", }); } catch (e) { console.error("Failed to sign out user on API!", e); } this.UnsetAuthenticated(); } }