From 3da3793282a2bb50f7f3e39fbe08efee7582743b Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 9 Jan 2021 01:07:35 +0800 Subject: ... --- FrontEnd/src/app/http/common.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'FrontEnd/src/app/http/common.ts') diff --git a/FrontEnd/src/app/http/common.ts b/FrontEnd/src/app/http/common.ts index 54203d1a..2dd3677b 100644 --- a/FrontEnd/src/app/http/common.ts +++ b/FrontEnd/src/app/http/common.ts @@ -1,7 +1,24 @@ -import { AxiosError, AxiosResponse } from "axios"; +import rawAxios, { AxiosError, AxiosResponse } from "axios"; +import { BehaviorSubject } from "rxjs"; export const apiBaseUrl = "/api"; +export const axios = rawAxios.create(); + +export const tokenSubject: BehaviorSubject = new BehaviorSubject< + string | null +>(null); + +tokenSubject.subscribe((token) => { + if (token == null) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + delete axios.defaults.headers.common["Authorization"]; + } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + axios.defaults.headers.common["Authorization"] = `Bearer ${token}`; + } +}); + export function base64(blob: Blob): Promise { return new Promise((resolve) => { const reader = new FileReader(); -- cgit v1.2.3