From 5aa5520e4c30ed5761a2a6421ff89be6db52ed52 Mon Sep 17 00:00:00 2001 From: Maxime Duchene-Savard Date: Fri, 11 Apr 2025 23:47:01 -0400 Subject: [PATCH] work --- apis/apis.go | 2 +- apis/auth_endpoints_test.go | 4 ++-- frontend/src/pages/signin.vue | 2 +- frontend/src/router/index.ts | 17 +++++++++++++++++ frontend/src/stores/app.ts | 12 +++++++++++- frontend/vite.config.mts | 2 +- 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/apis/apis.go b/apis/apis.go index 6a322bc..757c91f 100644 --- a/apis/apis.go +++ b/apis/apis.go @@ -7,7 +7,7 @@ import ( ) func SetupRouter(r *gin.Engine, authMiddleware gin.HandlerFunc) { - private := r.Group("/private") + private := r.Group("/gui") // Gets the session from the cookie, and puts it in the current request. if authMiddleware != nil { private.Use(authMiddleware) diff --git a/apis/auth_endpoints_test.go b/apis/auth_endpoints_test.go index 9b1c5bf..bcfadee 100644 --- a/apis/auth_endpoints_test.go +++ b/apis/auth_endpoints_test.go @@ -43,7 +43,7 @@ func TestInitAuthEndpoints_authLogin(t *testing.T) { reqBody := loginRequest{Username: "admin", Password: *adminPass} strReqBody, _ := json.Marshal(reqBody) w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/private/auth/login", strings.NewReader(string(strReqBody))) + req, _ := http.NewRequest("POST", "/gui/auth/login", strings.NewReader(string(strReqBody))) r.ServeHTTP(w, req) assert.Equal(t, 200, w.Code) @@ -59,7 +59,7 @@ func TestInitAuthEndpoints_getMe(t *testing.T) { SetupRouter(r, MockAuthMiddleware(*user)) w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/private/auth/me", nil) + req, _ := http.NewRequest("GET", "/gui/auth/me", nil) r.ServeHTTP(w, req) strUser, _ := json.Marshal(user) diff --git a/frontend/src/pages/signin.vue b/frontend/src/pages/signin.vue index abfa605..43e0881 100644 --- a/frontend/src/pages/signin.vue +++ b/frontend/src/pages/signin.vue @@ -91,7 +91,7 @@ async function login() { // For now, we'll just simulate a successful login console.log('Login attempted with:', {username: username.value, password: password.value}) - const res = await fetch("/private/auth/login", { + const res = await fetch("/gui/auth/login", { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 5d4b8bc..244a804 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -8,6 +8,7 @@ import { createRouter, createWebHistory } from 'vue-router/auto' import { setupLayouts } from 'virtual:generated-layouts' import { routes } from 'vue-router/auto-routes' +import {useAppStore} from "@/stores/app"; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -33,4 +34,20 @@ router.isReady().then(() => { localStorage.removeItem('vuetify:dynamic-reload') }) +router.beforeEach(async (to, from, next) => { + const appStore = useAppStore(); + await appStore.updateUser() + + if (to.meta.public) { + next() + return + } + + if (!appStore.user) { + next('/signin') + } else { + next() + } +}) + export default router diff --git a/frontend/src/stores/app.ts b/frontend/src/stores/app.ts index ab53343..84b6d23 100644 --- a/frontend/src/stores/app.ts +++ b/frontend/src/stores/app.ts @@ -1,8 +1,18 @@ // Utilities -import { defineStore } from 'pinia' +import {defineStore} from 'pinia' export const useAppStore = defineStore('app', { state: () => ({ user: null, }), + actions: { + async updateUser() { + const res = await fetch('/gui/auth/me') + if (res.status === 200) { + this.user = await res.json() + } else if (res.status === 401) { + this.user = null + } + } + } }) diff --git a/frontend/vite.config.mts b/frontend/vite.config.mts index 499ece3..ecb2cd3 100644 --- a/frontend/vite.config.mts +++ b/frontend/vite.config.mts @@ -71,7 +71,7 @@ export default defineConfig({ server: { port: 3000, proxy: { - '/private': 'http://localhost:8080', + '/gui': 'http://localhost:8080', } }, css: {