Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added check if user is logged in #17

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions public/home.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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 () => {
Expand Down
12 changes: 7 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ <h1>Sport Challenge</h1>

<article>
<h2>Create a new Account or LogIn</h2>
<button onclick="location.href = '/auth/sign_up.html';">Sign Up</button>
<button onclick="location.href = '/auth/sign_in.html';">Sign In</button>
<button id="button-sign_up" onclick="location.href = '/auth/sign_up.html';">Sign Up</button>
<button id="button-sign_in" onclick="location.href = '/auth/sign_in.html';">Sign In</button>
</article>

<article>
<h2>Already loogged in?</h2>
<p>When you are logged in, go to home to see your data.</p>
<article id="already_signd_in" style="display: none">
<h2>You are already loogged in!</h2>
<p>You can just open your home. No need to SignUp or SignIn.</p>
<button onclick="window.location = '/home.html'">Go to Home</button>
</article>

Expand All @@ -39,6 +39,8 @@ <h2>Comare</h2>
<h2>Keep track</h2>
<p>Keep a log of your activities.</p>
</article>

<script src="index.js" type="module"></script>
</body>

</html>
18 changes: 18 additions & 0 deletions public/index.js
Original file line number Diff line number Diff line change
@@ -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();
19 changes: 17 additions & 2 deletions public/scripts/requests.js
Original file line number Diff line number Diff line change
@@ -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!");
}
Expand All @@ -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);
}
4 changes: 4 additions & 0 deletions src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use chrono::{DateTime, Utc};

type AuthContext = axum_login::extractors::AuthContext<i64, User, SqliteStore<User>>;

pub async fn ping() -> impl IntoResponse {
(StatusCode::OK).into_response()
}

pub async fn sign_up(Json(payload): Json<BareUser>) -> impl IntoResponse {
// if username already exists, return with error
if storage::user_exists(&payload.name).await {
Expand Down
2 changes: 2 additions & 0 deletions src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64, User>::login())
// authentication routes
Expand Down