Skip to content

Commit

Permalink
Merge pull request #67 from acmauth/authFlow
Browse files Browse the repository at this point in the history
Properly judging the user's authentication status
  • Loading branch information
Christos Balaktsis authored Feb 27, 2024
2 parents a2ebcb5 + 6cc8448 commit 76bc26e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
62 changes: 60 additions & 2 deletions src/lib/authentication/authValidator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,69 @@
import { userCreds, userTokens } from "$stores/credentials.store";
import { get } from "svelte/store";
import reauthenticate from "../-universis/authenticator/reauthenticate.js";
import { Network } from '@capacitor/network';

// Do we wanna log out? Let's clear our path
export function invalidateAuth(){
localStorage.clear();
}

// TODO: Implement a proper login check
export async function judgeAuth() {
// Give a judgement on wether the user should be directed to the login page or not
// If the user is logged in, we return true
// If the user is not logged in, we return false
// If the user if offline, we return true, unless they don't have a token, in which case we return false

const onLineStatus = (await Network.getStatus()).connected;
const _userCreds: any = get(userCreds);

if (!_userCreds.password || !_userCreds.username) return false; // If we don't have any credentials, we're not logged in
if (!onLineStatus) return true; // If we're offline, there is no way to check if we're logged in, so we assume we are and use cached data
return await getLoginStatus();

}


export async function getLoginStatus() : Promise<boolean> {
return false;
// Checking for our login status by doing a dummy request to the server
// If we get a 200, we're logged in
// if we get a 40x, we're not logged in

// We perform a request to the server to check if we're logged in
// If successful, we return true
// If not, out token might just be invalid, so we try to reauthenticate
// If we're still not logged in, we return false
try {
let _userTokens: any = get(userTokens);

// We get the token from the store
const url = `https://universis-api.it.auth.gr/api/users/me`;
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${_userTokens.universis.token}`,
},
});

if (response.status >= 500 || response.status === 200) {
return true;
}
else {
await reauthenticate();
_userTokens = get(userTokens);
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${_userTokens.universis.token}`,
},
});
if (response.status >= 500 || response.status === 200) {
return true;
}
else {
return false;
}
}
}
catch (e) {
return false;
}
}
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script>
import { goto } from '$app/navigation';
import { getLoginStatus } from '$lib/authentication/authValidator';
import { judgeAuth } from '$lib/authentication/authValidator';
import { onMount } from 'svelte';
// Handling the redirect to the homepage
onMount(async () => {
if (await getLoginStatus()) {
if (await judgeAuth()) {
goto('pages/homepage');
} else {
goto('login');}
Expand Down

0 comments on commit 76bc26e

Please sign in to comment.