Skip to content

Commit

Permalink
simplified the auth process and dashboard redirects to login page if …
Browse files Browse the repository at this point in the history
…not logged in
  • Loading branch information
AmeerArsala committed Jun 5, 2024
1 parent 96906d8 commit 59b8e80
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
6 changes: 2 additions & 4 deletions frontend/src/data/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const githubStars = {
getAtom: () => numGitHubStars
};

// auth is not persistent on purpose--oh wait we have shouldCheckAuthentication for that
export const authState = persistentAtom("auth_state");
// Usually auth would not be persistent on purpose, but we can always re-run the auth check
export const authState = persistentAtom("auth_state"); // the actual STATE value from the authentication (a random string such as "BlueFox01")
const authenticated = persistentAtom("is_authenticated");

export const authentication = {
Expand All @@ -28,8 +28,6 @@ export const authentication = {
getAtom: () => authenticated
};

export const shouldCheckAuthentication = atom(true);

// App Stores
export const userID = persistentAtom("user_id"); //, null);

Expand Down
10 changes: 8 additions & 2 deletions frontend/src/pages/dashboard/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import DefaultLayout from '@layouts/DefaultLayout.astro';
export const prerender = false;
---

<DefaultLayout showHeader={true}>
<!-- IMPORTANT PAGE: UPDATE IMPORTANT VARIABLES -->
<DefaultLayout showHeader={false}>
<!-- IMPORTANT PAGE: UPDATE IMPORTANT CLIENT-SIDE VARIABLES -->
<script>
import update from "$lib/scripts/updatestate";
import { authentication } from "$lib/data/stores";

// updates important states
await update();

if (!authentication.isAuthenticated()) {
// redirect to login; let it stay in the browser history
window.location.href = '/login';
}
</script>

<p>Dashboard Coming soon...</p>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
// Server-side logic
import DefaultLayout from '@layouts/DefaultLayout.astro';
import { FlipWords } from "@components/ui/flip-words";
import { SocietyGenerator } from './SocietyGenerator';
Expand All @@ -15,7 +16,7 @@ const attentionWords = ["Multi-Agent Systems", "Communities", "a Text Adventure"

<DefaultLayout title="UniverseLM" showHeader gridBG>
<!-- SCRIPTS -->
<!-- IMPORTANT PAGE: UPDATE IMPORTANT VARIABLES -->
<!-- IMPORTANT PAGE: UPDATE IMPORTANT CLIENT-SIDE VARIABLES -->
<script>
import update from "$lib/scripts/updatestate";
import { updateStargazers } from "$lib/scripts/updatestate";
Expand Down
21 changes: 7 additions & 14 deletions frontend/src/scripts/updatestate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ import Cookies from 'js-cookie';

import { REPO_URL } from "$lib/data/constants";
import { BACKEND_URL } from "$lib/data/envconfig";
import { githubStars, authState, authentication, shouldCheckAuthentication, userID } from "$lib/data/stores";
import { githubStars, authState, authentication, userID } from "$lib/data/stores";

// when to update all major values
export default async function update() {
await attemptAuthUpdate();
}

export async function attemptAuthUpdate() {
if (shouldCheckAuthentication.get()) {
await updateAuthenticationState();
shouldCheckAuthentication.set(false);
}
await updateAuthentication();
}

export async function updateStargazers(repo_link: string = REPO_URL) {
Expand All @@ -39,8 +32,8 @@ export async function updateStargazers(repo_link: string = REPO_URL) {
return stars;
}

export async function updateAuthenticationState() {
console.log("Updating authentication state...");
export async function updateAuthentication() {
console.log("Updating authentication value...");

// retrieve state of user ID
console.log("Getting ID...");
Expand Down Expand Up @@ -91,8 +84,8 @@ export async function updateAuthenticationState() {

//const userId: string = localStorage.getItem('userId');

// update user authentication state now
console.log("Fetching authentication state...");
// update user authentication now
console.log("Fetching authentication...");
try {
const response = await axios.get(`${BACKEND_URL}/auth/is_authenticated`, {
params: {
Expand All @@ -101,7 +94,7 @@ export async function updateAuthenticationState() {
});
authentication.setIsAuthenticated(response.data);

console.log("Authentication State Fetched! Authenticated: " + authentication.isAuthenticated());
console.log("Authentication Fetched! Authenticated: " + authentication.isAuthenticated());
} catch(error) {
console.log("ERROR FINDING OUT WHETHER USER IS AUTHENTICATED:");
console.log(error);
Expand Down

0 comments on commit 59b8e80

Please sign in to comment.