From 53e7ef8a5283ea4e2e821d08e595090b5aabda64 Mon Sep 17 00:00:00 2001 From: mobergmann Date: Thu, 9 Nov 2023 22:18:00 +0100 Subject: [PATCH] added check if user is logged in --- public/home.js | 8 ++++++++ public/index.html | 12 +++++++----- public/index.js | 18 ++++++++++++++++++ public/scripts/requests.js | 19 +++++++++++++++++-- src/logic.rs | 4 ++++ src/services.rs | 2 ++ 6 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 public/index.js diff --git a/public/home.js b/public/home.js index 762e0eb..c47bdbd 100644 --- a/public/home.js +++ b/public/home.js @@ -1,5 +1,6 @@ import {get_activities} from "./scripts/activity.js"; import {get_user_by_id} from "./scripts/user.js"; +import {ping} from "./scripts/requests.js" /// @source: https://stackoverflow.com/a/31810991/11186407 Date.prototype.getWeek = function() { @@ -277,6 +278,13 @@ async function update_frontend() { } async function main() { + try { + await ping(); + } catch (error) { + alert("You are not signed in. Sign in first."); + window.location = "/auth/sign_in.html" + } + await update_frontend(); document.querySelector("#button-load_previous_week").onclick = async () => { diff --git a/public/index.html b/public/index.html index 8675461..75bfd94 100644 --- a/public/index.html +++ b/public/index.html @@ -14,13 +14,13 @@

Sport Challenge

Create a new Account or LogIn

- - + +
-
-

Already loogged in?

-

When you are logged in, go to home to see your data.

+ @@ -39,6 +39,8 @@

Comare

Keep track

Keep a log of your activities.

+ + \ No newline at end of file diff --git a/public/index.js b/public/index.js new file mode 100644 index 0000000..ff359e4 --- /dev/null +++ b/public/index.js @@ -0,0 +1,18 @@ +import {ping} from "./scripts/requests.js" + +async function main() { + try { + await ping(); + } catch (error) { + return; + } + + // disable buttons + document.querySelector("#button-sign_up").disabled = true; + document.querySelector("#button-sign_in").disabled = true; + + document.querySelector("#already_signd_in").style.display = "block"; + +} + +await main(); diff --git a/public/scripts/requests.js b/public/scripts/requests.js index 08e0c53..b71f13d 100644 --- a/public/scripts/requests.js +++ b/public/scripts/requests.js @@ -1,8 +1,14 @@ -export async function do_request(request) { +import {BASE_URL} from "./variables.js"; + +export async function do_request(request, body_expected = true) { return await fetch(request) .then((response) => { if (response.status === 200) { - return response.json(); + if (!body_expected) { + return response; + } else { + return response.json(); + } } else { throw new Error("Something went wrong on API server!"); } @@ -15,3 +21,12 @@ export async function do_request(request) { throw error; }); } + + +export async function ping() { + const request = new Request(`${BASE_URL}/ping`, { + method: "GET", + }); + + return await do_request(request, false); +} diff --git a/src/logic.rs b/src/logic.rs index 6f4452e..38ce2b7 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -11,6 +11,10 @@ use chrono::{DateTime, Utc}; type AuthContext = axum_login::extractors::AuthContext>; +pub async fn ping() -> impl IntoResponse { + (StatusCode::OK).into_response() +} + pub async fn sign_up(Json(payload): Json) -> impl IntoResponse { // if username already exists, return with error if storage::user_exists(&payload.name).await { diff --git a/src/services.rs b/src/services.rs index 63fcb00..1369541 100644 --- a/src/services.rs +++ b/src/services.rs @@ -49,6 +49,8 @@ pub async fn backend_router() -> Router { .route("/v1/activities", post(new_activity)) .route("/v1/activities/edit", post(edit_activity)) .route("/v1/activities/:id", delete(delete_activity)) + // for checking if you are logged in + .route("/v1/ping", get(ping)) // routes above are protected .route_layer(RequireAuthorizationLayer::::login()) // authentication routes