diff options
Diffstat (limited to 'front/src/Authentication/ContextProvider.ts')
| -rw-r--r-- | front/src/Authentication/ContextProvider.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/front/src/Authentication/ContextProvider.ts b/front/src/Authentication/ContextProvider.ts new file mode 100644 index 0000000..040d22e --- /dev/null +++ b/front/src/Authentication/ContextProvider.ts @@ -0,0 +1,38 @@ +import axios from "axios" +import { createContext } from "react" +import { BackendURL } from "../Config" +import { User } from "./Models" + +export type Authentication = { + User: User + Token: string +} + +const UserInfoURL = `${BackendURL}/auth`; + +export const AuthContext = createContext<User | null>(null); + +export function SaveState(data: Authentication | null, setCookie: (cookie: string) => void) { + if (data == null) return; + setCookie(data.Token); +} + +export async function GetUserInfo(displayToken: string): Promise<User | null> { + if (!displayToken || displayToken.length == 0) return null; + try { + const { data, status } = await axios.get<User>( + UserInfoURL, + { + headers: { + Accept: "application/json", + Authorization: `Bearer ${displayToken}` + } + } + ); + if (status != 200) return null; + return data; + } catch (err) { + console.log(`Failed to get user info: ${err}`); + return null; + } +} |
