diff --git a/src/helpers/APIHelper.ts b/src/helpers/APIHelper.ts index 4e4ada5..8b31b96 100644 --- a/src/helpers/APIHelper.ts +++ b/src/helpers/APIHelper.ts @@ -4,6 +4,7 @@ * @author Pierre Hubert */ +import { AccountHelper } from "./AccountHelper"; import { ConfigHelper } from "./ConfigHelper"; /** @@ -14,7 +15,11 @@ import { ConfigHelper } from "./ConfigHelper"; * @returns The result of the request, in case of success, * @throws An exception in case of failure */ -export async function serverRequest(uri: string, args?: any): Promise { +export async function serverRequest( + uri: string, + args?: any, + continueOnError = false +): Promise { const requestArguments = args || {}; const fd = new FormData(); @@ -23,15 +28,19 @@ export async function serverRequest(uri: string, args?: any): Promise { fd.append(arg, requestArguments[arg]); } - // TODO : add access token, once supported + // Add access token, if any + if (AccountHelper.hasAccessToken) + fd.append("token", AccountHelper.accessToken); const result = await fetch((await ConfigHelper.apiURL()) + uri, { method: "POST", body: fd, }); - if (result.status !== 200) + if (result.status !== 200) { + if (continueOnError) return { error: result.status }; throw new Error("Request failed with status " + result.status); + } return await result.json(); } diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 98e1f2e..152f8a9 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -10,16 +10,33 @@ export interface AuthOptions { reset_token: string; } +export interface AdminAccount { + id: number; + name: string; + email: string; + time_create: number; +} + const SESSION_STORAGE_TOKEN = "auth_token"; +let currentAccount: AdminAccount | null = null; + export class AccountHelper { /** * Check whether access token are available * or not */ static get hasAccessToken(): boolean { - // TODO : implement support - return false; + return sessionStorage.getItem(SESSION_STORAGE_TOKEN) != null; + } + + /** + * Get current user access token + */ + static get accessToken(): string { + const token = sessionStorage.getItem(SESSION_STORAGE_TOKEN); + if (!token) throw new Error("No access token for now!"); + return token; } /** @@ -47,4 +64,29 @@ export class AccountHelper { sessionStorage.setItem(SESSION_STORAGE_TOKEN, res.token); } + + /** + * Attempt to refresh current account information + */ + static async refreshCurrentAccountInfo() { + const res = await serverRequest("accounts/info", {}, true); + + if (res.error) { + if (res.error > 0) { + sessionStorage.removeItem(SESSION_STORAGE_TOKEN); + document.location.href = document.location.href + ""; + } + throw new Error("Request failed!"); + } + + currentAccount = res; + } + + /** + * Get current account information + */ + static get currentAccount(): AdminAccount { + if (currentAccount == null) throw new Error("Current account is null!"); + return currentAccount; + } } diff --git a/src/ui/widgets/InitWidget.tsx b/src/ui/widgets/InitWidget.tsx index 8b61bbf..fe7fe15 100644 --- a/src/ui/widgets/InitWidget.tsx +++ b/src/ui/widgets/InitWidget.tsx @@ -1,6 +1,6 @@ /** * Initialization widget - * + * * @author Pierre Hubert */ @@ -10,39 +10,45 @@ import { LoginRoute } from "../routes/LoginRoute"; import { AsyncWidget } from "./AsyncWidget"; interface InitWidgetState { - signedIn: boolean, + signedIn: boolean; } export class InitWidget extends React.Component<{}, InitWidgetState> { + constructor(props: any) { + super(props); - constructor(props: any) { - super(props); + this.state = { + signedIn: false, + }; - this.state = { - signedIn: false - }; + this.init = this.init.bind(this); + this.build = this.build.bind(this); + } - this.init = this.init.bind(this); - this.build = this.build.bind(this); - } + async init() { + this.setState({ signedIn: false }); - async init() { - this.setState({ signedIn: false }); + if (AccountHelper.hasAccessToken) { + await AccountHelper.refreshCurrentAccountInfo(); + this.setState({ signedIn: true }); + } + } - if (AccountHelper.hasAccessToken) { - throw Error("UNIMPLEMENTED!"); - } - } + render() { + return ( + + ); + } - render() { - return (); - } - - build() { - return this.state.signedIn ? null : (); - } -} \ No newline at end of file + build() { + return this.state.signedIn ? ( +
{AccountHelper.currentAccount.name}
+ ) : ( + + ); + } +}