From 860400e1f3bc4d3116af3250e2e7b7480689b864 Mon Sep 17 00:00:00 2001 From: DariusTFox Date: Mon, 6 Dec 2021 03:26:54 +0200 Subject: [PATCH] Implemented login/logout/username with api --- client/src/App.svelte | 35 +++++++++++++++++------ client/src/Login.svelte | 13 +++++++-- client/src/MainPage.svelte | 24 +++++++++++++--- client/src/api.js | 57 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 client/src/api.js diff --git a/client/src/App.svelte b/client/src/App.svelte index 21c7b8d..0f9a314 100644 --- a/client/src/App.svelte +++ b/client/src/App.svelte @@ -1,15 +1,18 @@
diff --git a/client/src/Login.svelte b/client/src/Login.svelte index 1c3d965..e0184b8 100644 --- a/client/src/Login.svelte +++ b/client/src/Login.svelte @@ -4,6 +4,7 @@ import CardBG from "./CardBG.svelte"; import InputField from "./InputField.svelte"; import {createEventDispatcher} from 'svelte'; + import {login} from './api.js'; const dispatch = createEventDispatcher(); @@ -11,9 +12,15 @@ let username = ""; let code = ""; - function checkLogin(){ - //todo: CHeck here - dispatch("loginSuccess",null); + async function checkLogin(){ + const result = await login(username, code); + if(result.status == "success") { + sessionStorage.setItem("token", result.token); + dispatch("loginSuccess",null); + }else{ + alert(result.code); + } + } diff --git a/client/src/MainPage.svelte b/client/src/MainPage.svelte index 9739762..e8c1eb0 100644 --- a/client/src/MainPage.svelte +++ b/client/src/MainPage.svelte @@ -1,15 +1,19 @@
@@ -115,9 +131,9 @@
- +
-

{username}

+

{fullname}

diff --git a/client/src/api.js b/client/src/api.js new file mode 100644 index 0000000..ab6b606 --- /dev/null +++ b/client/src/api.js @@ -0,0 +1,57 @@ +const baseURL = "https://foxbank-api.extras.dcdevelop.xyz"; + +export async function login(username, code) { + try { + const result = await fetch(new URL("/login/", baseURL), { + method: "POST", + body: JSON.stringify({ + username, code, + }), + headers: { + "Content-Type": "application/json" + }, + }); + + return (await result.json()); + } catch (error) { + return { + status: "error", + code: "request/failure" + } + } +} + + +export async function whoami(token) { + try { + const result = await fetch(new URL("/login/whoami", baseURL), { + method: "GET", + headers: { + "Authorization": "Bearer " + token, + }, + }); + + return (await result.json()); + } catch (error) { + return { + status: "error", + code: "request/failure" + } + } +} + +export async function logout(token) { + try { + await fetch(new URL("/login/logout", baseURL), { + method: "POST", + headers: { + "Authorization": "Bearer " + token, + }, + }); + } catch (error) { + return { + status: "error", + code: "request/failure" + } + } +} \ No newline at end of file