瀏覽代碼

add more backend services

Sven Czarnian 2 年之前
父節點
當前提交
7807c2784c
共有 3 個文件被更改,包括 52 次插入1 次删除
  1. 31 0
      src/services/auth.ts
  2. 3 1
      src/services/index.ts
  3. 18 0
      src/services/system.ts

+ 31 - 0
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<User> {
+    const token = sessionStorage.getItem('token');
+    if (!token) {
+      return {
+        vatsimId: '',
+        fullName: '',
+        administrator: false,
+        airportConfigurationAccess: [],
+      };
+    }
+
+    return axios
+      .get<User>(`${Configuration.resourceServer}/auth/user`, {
+        headers: {
+          Authorization: `Bearer ${sessionStorage.getItem('token')}`,
+        },
+      })
+      .then((response) => response.data)
+      .catch(() => ({
+        vatsimId: '',
+        fullName: '',
+        administrator: false,
+        airportConfigurationAccess: [],
+      }));
+  }
+}

+ 3 - 1
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 };

+ 18 - 0
src/services/system.ts

@@ -0,0 +1,18 @@
+import axios from 'axios';
+import { Configuration } from './configuration';
+
+export class System {
+  static async timestamp(): Promise<number> {
+    const token = sessionStorage.getItem('token');
+    if (!token) return 0;
+
+    return axios
+      .get<number>(`${Configuration.resourceServer}/system/timestamp`, {
+        headers: {
+          Authorization: `Bearer ${token}`,
+        },
+      })
+      .then((response) => response.data)
+      .catch(() => 0);
+  }
+}