Ensure that a user is logged in on each page

This commit is contained in:
2024-12-21 17:28:37 +01:00
parent 9b1d6053a0
commit ea23a235d4
41 changed files with 45 additions and 9 deletions
+16
View File
@@ -55,6 +55,22 @@ func LoginUser(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Login successful"})
}
// Apply middleware to protected routes
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("Authorization")
if token == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
c.Abort()
return
}
// Add token validation logic here
c.Next()
}
}
/*
Character Handlers
+6 -6
View File
@@ -24,12 +24,12 @@ func main() {
// Routes
r.POST("/register", RegisterUser)
r.POST("/login", LoginUser)
r.GET("/characters", GetCharacters)
r.POST("/characters", CreateCharacter)
r.POST("/ausruestung", CreateAusruestung)
r.GET("/ausruestung/:character_id", GetAusruestung)
r.PUT("/ausruestung/:ausruestung_id", UpdateAusruestung)
r.DELETE("/ausruestung/:ausruestung_id", DeleteAusruestung)
r.GET("/characters", AuthMiddleware(), GetCharacters)
r.POST("/characters", AuthMiddleware(), CreateCharacter)
r.POST("/ausruestung", AuthMiddleware(), CreateAusruestung)
r.GET("/ausruestung/:character_id", AuthMiddleware(), GetAusruestung)
r.PUT("/ausruestung/:ausruestung_id", AuthMiddleware(), UpdateAusruestung)
r.DELETE("/ausruestung/:ausruestung_id", AuthMiddleware(), DeleteAusruestung)
r.Run(":8180") // Start server on port 8080
}
+1 -1
View File
@@ -11,7 +11,7 @@ API Testing
Create Character:
POST /characters
Body: { "user_id": 1, "name": "Hero", "rasse": "Elf", "typ": "Warrior", "alter": 25 }
Body: { "user_id": 1, "name": "Hero", "rasse": "Elf", "typ": "Warrior", "age": 25 }
Get Characters:
GET /characters
View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

Before

Width:  |  Height:  |  Size: 276 B

After

Width:  |  Height:  |  Size: 276 B

@@ -1,4 +1,5 @@
import { createRouter, createWebHistory } from "vue-router";
import { isLoggedIn } from "../utils/auth"; // Import the helper function
import LoginView from "../views/LoginView.vue";
import RegisterView from "../views/RegisterView.vue";
import DashboardView from "../views/DashboardView.vue";
@@ -7,8 +8,8 @@ import AusruestungView from "../views/AusruestungView.vue";
const routes = [
{ path: "/", name: "Login", component: LoginView },
{ path: "/register", name: "Register", component: RegisterView },
{ path: "/dashboard", name: "Dashboard", component: DashboardView },
{ path: "/ausruestung/:characterId", name: "Ausruestung", component: AusruestungView },
{ path: "/dashboard", name: "Dashboard", component: DashboardView, meta: { requiresAuth: true } },
{ path: "/ausruestung/:characterId", name: "Ausruestung", component: AusruestungView, meta: { requiresAuth: true } },
];
const router = createRouter({
@@ -16,4 +17,14 @@ const router = createRouter({
routes,
});
// Navigation guard
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth && !isLoggedIn()) {
// Redirect to login if not authenticated
next({ name: "Login" });
} else {
next(); // Allow navigation
}
});
export default router;
+9
View File
@@ -0,0 +1,9 @@
// src/utils/auth.js
export function isLoggedIn() {
const token = localStorage.getItem("token");
return !!token; // Returns true if the token exists
}
export function logout() {
localStorage.removeItem("token");
}