From 7807c2784c4ef54162cf75113a669aefd9326ded Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Fri, 4 Nov 2022 15:28:29 +0100 Subject: [PATCH] add more backend services --- src/services/auth.ts | 31 +++++++++++++++++++++++++++++++ src/services/index.ts | 4 +++- src/services/system.ts | 18 ++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/services/auth.ts create mode 100644 src/services/system.ts diff --git a/src/services/auth.ts b/src/services/auth.ts new file mode 100644 index 0000000..9b77c88 --- /dev/null +++ b/src/services/auth.ts @@ -0,0 +1,31 @@ +import axios from 'axios'; +import { Configuration } from './configuration'; +import { User } from '../types'; + +export class Auth { + static async user(): Promise { + const token = sessionStorage.getItem('token'); + if (!token) { + return { + vatsimId: '', + fullName: '', + administrator: false, + airportConfigurationAccess: [], + }; + } + + return axios + .get(`${Configuration.resourceServer}/auth/user`, { + headers: { + Authorization: `Bearer ${sessionStorage.getItem('token')}`, + }, + }) + .then((response) => response.data) + .catch(() => ({ + vatsimId: '', + fullName: '', + administrator: false, + airportConfigurationAccess: [], + })); + } +} diff --git a/src/services/index.ts b/src/services/index.ts index d4798a0..e479ca4 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,4 +1,6 @@ import { Airport } from "./airport"; +import { Auth } from './auth'; import { Configuration } from "./configuration"; +import { System } from './system'; -export { Airport, Configuration }; +export { Airport, Auth, Configuration, System }; diff --git a/src/services/system.ts b/src/services/system.ts new file mode 100644 index 0000000..1fd3bcf --- /dev/null +++ b/src/services/system.ts @@ -0,0 +1,18 @@ +import axios from 'axios'; +import { Configuration } from './configuration'; + +export class System { + static async timestamp(): Promise { + const token = sessionStorage.getItem('token'); + if (!token) return 0; + + return axios + .get(`${Configuration.resourceServer}/system/timestamp`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .then((response) => response.data) + .catch(() => 0); + } +}