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
|
* @author Pierre Hubert
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { AccountHelper } from "./AccountHelper";
|
||||||
import { ConfigHelper } from "./ConfigHelper";
|
import { ConfigHelper } from "./ConfigHelper";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,7 +15,11 @@ import { ConfigHelper } from "./ConfigHelper";
|
|||||||
* @returns The result of the request, in case of success,
|
* @returns The result of the request, in case of success,
|
||||||
* @throws An exception in case of failure
|
* @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 requestArguments = args || {};
|
||||||
|
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
@ -23,15 +28,19 @@ export async function serverRequest(uri: string, args?: any): Promise<any> {
|
|||||||
fd.append(arg, requestArguments[arg]);
|
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, {
|
const result = await fetch((await ConfigHelper.apiURL()) + uri, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: fd,
|
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);
|
throw new Error("Request failed with status " + result.status);
|
||||||
|
}
|
||||||
|
|
||||||
return await result.json();
|
return await result.json();
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,33 @@ export interface AuthOptions {
|
|||||||
reset_token: string;
|
reset_token: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AdminAccount {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
time_create: number;
|
||||||
|
}
|
||||||
|
|
||||||
const SESSION_STORAGE_TOKEN = "auth_token";
|
const SESSION_STORAGE_TOKEN = "auth_token";
|
||||||
|
|
||||||
|
let currentAccount: AdminAccount | null = null;
|
||||||
|
|
||||||
export class AccountHelper {
|
export class AccountHelper {
|
||||||
/**
|
/**
|
||||||
* Check whether access token are available
|
* Check whether access token are available
|
||||||
* or not
|
* or not
|
||||||
*/
|
*/
|
||||||
static get hasAccessToken(): boolean {
|
static get hasAccessToken(): boolean {
|
||||||
// TODO : implement support
|
return sessionStorage.getItem(SESSION_STORAGE_TOKEN) != null;
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Initialization widget
|
* Initialization widget
|
||||||
*
|
*
|
||||||
* @author Pierre Hubert
|
* @author Pierre Hubert
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -10,39 +10,45 @@ import { LoginRoute } from "../routes/LoginRoute";
|
|||||||
import { AsyncWidget } from "./AsyncWidget";
|
import { AsyncWidget } from "./AsyncWidget";
|
||||||
|
|
||||||
interface InitWidgetState {
|
interface InitWidgetState {
|
||||||
signedIn: boolean,
|
signedIn: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class InitWidget extends React.Component<{}, InitWidgetState> {
|
export class InitWidget extends React.Component<{}, InitWidgetState> {
|
||||||
|
constructor(props: any) {
|
||||||
|
super(props);
|
||||||
|
|
||||||
constructor(props: any) {
|
this.state = {
|
||||||
super(props);
|
signedIn: false,
|
||||||
|
};
|
||||||
|
|
||||||
this.state = {
|
this.init = this.init.bind(this);
|
||||||
signedIn: false
|
this.build = this.build.bind(this);
|
||||||
};
|
}
|
||||||
|
|
||||||
this.init = this.init.bind(this);
|
async init() {
|
||||||
this.build = this.build.bind(this);
|
this.setState({ signedIn: false });
|
||||||
}
|
|
||||||
|
|
||||||
async init() {
|
if (AccountHelper.hasAccessToken) {
|
||||||
this.setState({ signedIn: false });
|
await AccountHelper.refreshCurrentAccountInfo();
|
||||||
|
this.setState({ signedIn: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (AccountHelper.hasAccessToken) {
|
render() {
|
||||||
throw Error("UNIMPLEMENTED!");
|
return (
|
||||||
}
|
<AsyncWidget
|
||||||
}
|
errorMessage="Failed to initialize application!"
|
||||||
|
load={this.init}
|
||||||
|
onBuild={this.build}
|
||||||
|
></AsyncWidget>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
build() {
|
||||||
return (<AsyncWidget
|
return this.state.signedIn ? (
|
||||||
errorMessage = "Failed to initialize application!"
|
<div>{AccountHelper.currentAccount.name}</div>
|
||||||
load = {this.init}
|
) : (
|
||||||
onBuild = {this.build}
|
<LoginRoute></LoginRoute>
|
||||||
></AsyncWidget>);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
build() {
|
|
||||||
return this.state.signedIn ? null : (<LoginRoute></LoginRoute>);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user