This commit is contained in:
Maxime Duchene-Savard 2025-04-11 23:47:01 -04:00
parent 2c18a22443
commit 5aa5520e4c
6 changed files with 33 additions and 6 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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',

View File

@ -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

View File

@ -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
}
}
}
})

View File

@ -71,7 +71,7 @@ export default defineConfig({
server: {
port: 3000,
proxy: {
'/private': 'http://localhost:8080',
'/gui': 'http://localhost:8080',
}
},
css: {