mirror of
https://gitlab.com/comunic/comunicconsole
synced 2024-11-23 13:59:23 +00:00
Can get current account information
This commit is contained in:
parent
05117e71a3
commit
cd7cc0e5e4
@ -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<any> {
|
||||
export async function serverRequest(
|
||||
uri: string,
|
||||
args?: any,
|
||||
continueOnError = false
|
||||
): Promise<any> {
|
||||
const requestArguments = args || {};
|
||||
|
||||
const fd = new FormData();
|
||||
@ -23,15 +28,19 @@ export async function serverRequest(uri: string, args?: any): Promise<any> {
|
||||
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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -10,16 +10,15 @@ 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);
|
||||
|
||||
this.state = {
|
||||
signedIn: false
|
||||
signedIn: false,
|
||||
};
|
||||
|
||||
this.init = this.init.bind(this);
|
||||
@ -30,19 +29,26 @@ export class InitWidget extends React.Component<{}, InitWidgetState> {
|
||||
this.setState({ signedIn: false });
|
||||
|
||||
if (AccountHelper.hasAccessToken) {
|
||||
throw Error("UNIMPLEMENTED!");
|
||||
await AccountHelper.refreshCurrentAccountInfo();
|
||||
this.setState({ signedIn: true });
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
return (<AsyncWidget
|
||||
return (
|
||||
<AsyncWidget
|
||||
errorMessage="Failed to initialize application!"
|
||||
load={this.init}
|
||||
onBuild={this.build}
|
||||
></AsyncWidget>);
|
||||
></AsyncWidget>
|
||||
);
|
||||
}
|
||||
|
||||
build() {
|
||||
return this.state.signedIn ? null : (<LoginRoute></LoginRoute>);
|
||||
return this.state.signedIn ? (
|
||||
<div>{AccountHelper.currentAccount.name}</div>
|
||||
) : (
|
||||
<LoginRoute></LoginRoute>
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user