-
- My App
-
-
- Logged in as {{ userEmail }}
-
+
+
+
+
+
+
diff --git a/travian/frontend/src/router.ts b/travian/frontend/src/router.ts
index 672ad131..6794f464 100644
--- a/travian/frontend/src/router.ts
+++ b/travian/frontend/src/router.ts
@@ -3,6 +3,7 @@
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import LoginPage from '@/views/LoginPage.vue';
import HomePage from '@/views/HomePage.vue';
+import { useAuthStore } from '@/store/auth';
const routes: Array
= [
{ path: '/', component: LoginPage },
@@ -14,11 +15,10 @@ const router = createRouter({
routes,
});
-// Check authentication status before each route navigation
router.beforeEach((to, from, next) => {
- const isAuthenticated = true; // Replace with your actual authentication check
+ const authStore = useAuthStore();
- if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
+ if (to.matched.some(record => record.meta.requiresAuth) && !authStore.isLoggedIn) {
next('/');
} else {
next();
diff --git a/travian/frontend/src/store/auth.ts b/travian/frontend/src/store/auth.ts
index 4fe844bf..b4bfda95 100644
--- a/travian/frontend/src/store/auth.ts
+++ b/travian/frontend/src/store/auth.ts
@@ -1,7 +1,8 @@
-// src/store/auth.ts
+// auth.ts
import { defineStore } from 'pinia';
import apiClient from '@/api/api';
+import { useRouter } from 'vue-router';
interface AuthState {
isLoggedIn: boolean;
@@ -21,29 +22,20 @@ export const useAuthStore = defineStore('auth', {
password: password
});
- // Assuming your backend returns the username in the response
- const { access_token, username: responseUsername } = response.data;
+ const { access_token, username: returnedUsername } = response.data;
- // Save username and set logged in status
this.isLoggedIn = true;
- this.username = responseUsername;
+ this.username = returnedUsername;
- // Save token to localStorage or sessionStorage if needed
localStorage.setItem('access_token', access_token);
-
- // You may want to redirect here after successful login
- // Example: router.push('/home');
} catch (error) {
console.error('Login failed:', error);
throw new Error('Login failed');
}
},
logout(this: any): void {
- // Clear user data and set logged out status
this.isLoggedIn = false;
this.username = '';
-
- // Clear token from localStorage or sessionStorage if used
localStorage.removeItem('access_token');
}
}
diff --git a/travian/frontend/src/views/HomePage.vue b/travian/frontend/src/views/HomePage.vue
index bd33c334..0f2a2ca8 100644
--- a/travian/frontend/src/views/HomePage.vue
+++ b/travian/frontend/src/views/HomePage.vue
@@ -1,21 +1,29 @@
-
-
Home Page
-
Welcome to the Home Page
-
-
-
-
-
-
-
\ No newline at end of file
+
+
Home Page
+
Welcome to the Home Page, {{ username }}
+
+
+
+
+
+