diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-04-28 23:33:59 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-04-28 23:33:59 +0500 |
| commit | 533c04d9558bd0a575671c157ca42236f59a2c5a (patch) | |
| tree | 895ec8672f9633fd6cfef85f6a12921fc5339ab3 /front/src/Authentication/ContextProvider.ts | |
| parent | ec5ef35ed47b90b9f09199d28f7885f8287815a8 (diff) | |
frontend auth
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; + } +} |
