Skip to content

Commit

Permalink
may have fixed auth bug in its entirety
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeerArsala committed Jun 9, 2024
1 parent ded2537 commit f798696
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
9 changes: 5 additions & 4 deletions frontend/src/components/AuthForms/AuthRedirect.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<script lang="ts">
import axios from "axios";
import { authState, userAuthID } from "$lib/data/stores";
import { authState/*, userAuthID, coreRegistration*/ } from "$lib/data/stores";
import { idNotFound } from "$lib/data/datafunctions";
import { idNotFound, onLogout } from "$lib/data/datafunctions";
export let href: string;
function handleAuth() {
// First things first: delete the current userAuthID if it exists
userAuthID.set("undefined")
// Make sure the state is reset
onLogout();
let realHref: string = href + "&redirect=false";
Expand Down Expand Up @@ -37,7 +38,7 @@
state = state.substring(0, endIndex);
}
// SET IT
// SET IT SO IT CAN BE USED LATER TO AUTHENTICATE
authState.set(state);
// Faciliate the redirect
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/components/NavBar/ActionButtons.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import { authentication } from "$lib/data/stores";
import { onLogout } from "$lib/data/datafunctions";
let authenticated: boolean = authentication.isAuthenticated();
onMount(() => {
Expand Down Expand Up @@ -62,7 +64,10 @@
</a>

<!--Log out button-->
<a href={`${BACKEND_URL}/auth/logout`}>
<a on:click={() => {
onLogout();
window.location.href = `${BACKEND_URL}/auth/logout`;
}}>
<button class="p-[3px] relative">
<div class="absolute inset-0 bg-gradient-to-r from-indigo-500 to-purple-500 rounded-full" />
<div class="px-4 py-1.5 bg-black rounded-full relative group transition duration-200 text-white hover:bg-transparent">
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/data/datafunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { BACKEND_URL } from "./envconfig";
import { userCoreID, userAuthID, apiKey } from "./stores";
import { userCoreID, userAuthID, apiKey, authentication, coreRegistration } from "./stores";

const MIN_ID_LENGTH: number = 3;
export const idNotFound = (id): boolean => (id === null || id === undefined || id === "undefined" || id === "null" || id.length < MIN_ID_LENGTH);
Expand All @@ -9,6 +9,13 @@ export function hasAPIKey(): boolean {
return !idNotFound(apiKey.get());
}

// reset state
export function onLogout() {
userAuthID.set("undefined");
authentication.setIsAuthenticated(false);
coreRegistration.deactivateRegistration();
}

export async function retrieveUserCoreID() {
try {
const response = await axios.get(`${BACKEND_URL}/user/${userAuthID.get()}/view_details/user_core_id`, {
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/scripts/updatestate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@ export async function updateAuthentication() {
console.log("Authentication Fetched! Authenticated: " + authentication.isAuthenticated());
postAuthentication();
} catch(error) {
console.log("ERROR FINDING OUT WHETHER USER IS AUTHENTICATED:");
console.log(error);
console.error("ERROR FINDING OUT WHETHER USER IS AUTHENTICATED: " + error);

authentication.setIsAuthenticated(false);
}
}

function postAuthentication() {
console.log("Post Authentication: Fetch core user ID if not registered");
if (!coreRegistration.isUserRegistered()) {
// Call the route to manifest the user
axios.post(`${BACKEND_URL}/user/manifest`, {}, {
axios.post(`${BACKEND_URL}/user/${userAuthID.get()}/manifest`, {}, {
withCredentials: true
}).then((response) => {
userCoreID.setID(response.data);
}).catch((error) => {
console.log("Error manifesting user:\n" + error);
console.error("Error manifesting user:\n" + error);
});

// activate the registration
Expand All @@ -143,6 +143,6 @@ export function updateAPIKey() {
// It's already set by the function, so no need to do it again
//console.log("API Key Retrieved.");
}).catch((error) => {
console.log("Error retrieving API Key: " + error.toString());
console.error("Error retrieving API Key: " + error.toString());
});
}

0 comments on commit f798696

Please sign in to comment.