Skip to content

Commit e48b5b9

Browse files
committed
feat: implement user authentication check in router and create UserService for fetching current user
1 parent a19ce4a commit e48b5b9

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

services/frontend/src/router/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createRouter, createWebHistory } from 'vue-router'
22
import { useDatabaseStore } from '@/stores/database'
3+
import { UserService } from '@/services/userService'
34

45
const routes = [
56
{
@@ -76,6 +77,15 @@ const router = createRouter({
7677
// Navigation guard to check database setup
7778
router.beforeEach(async (to, from, next) => {
7879
const databaseStore = useDatabaseStore()
80+
const user = await UserService.getCurrentUser()
81+
82+
// If user is logged in and trying to access Login or Register, redirect to Dashboard
83+
if (user) {
84+
if (to.name === 'Login' || to.name === 'Register') {
85+
next('/dashboard')
86+
return
87+
}
88+
}
7989

8090
// Skip setup check for the setup route itself
8191
if (to.name === 'Setup') {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { getEnv } from '@/utils/env';
2+
3+
export interface User {
4+
id: string;
5+
email: string;
6+
// Add other user properties as needed
7+
}
8+
9+
export class UserService {
10+
private static getApiUrl(): string {
11+
const apiUrl = getEnv('VITE_DEPLOYSTACK_BACKEND_URL');
12+
if (!apiUrl) {
13+
throw new Error('API URL not configured. Make sure VITE_DEPLOYSTACK_BACKEND_URL is set.');
14+
}
15+
return apiUrl;
16+
}
17+
18+
static async getCurrentUser(): Promise<User | null> {
19+
try {
20+
const apiUrl = this.getApiUrl();
21+
const response = await fetch(`${apiUrl}/api/users/me`, {
22+
method: 'GET',
23+
headers: {
24+
'Content-Type': 'application/json',
25+
},
26+
credentials: 'include', // Important for sending session cookies
27+
});
28+
29+
if (response.ok) {
30+
const data = await response.json();
31+
if (data.success && data.data) {
32+
return data.data as User;
33+
}
34+
// If success is false or data is missing, treat as not logged in for this check
35+
return null;
36+
}
37+
38+
if (response.status === 401) {
39+
// Unauthorized, user is not logged in
40+
return null;
41+
}
42+
43+
// For other errors, we might not want to block navigation,
44+
// but for the purpose of this check, any error means we can't confirm the user.
45+
console.error('Failed to fetch current user:', response.status);
46+
return null;
47+
} catch (error) {
48+
console.error('Error in getCurrentUser:', error);
49+
return null; // Network error or other issues
50+
}
51+
}
52+
}

services/frontend/src/views/Logout.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ onMounted(async () => {
4444
message.value = t('logout.inProgressMessage');
4545
console.log('Attempting to logout from backend...');
4646
47-
const backendBaseUrl = getEnv('VITE_DEPLOYSTACK_APP_URL');
47+
const backendBaseUrl = getEnv('VITE_DEPLOYSTACK_BACKEND_URL');
4848
4949
if (!backendBaseUrl) {
5050
console.error(
51-
'VITE_DEPLOYSTACK_APP_URL is not set in your environment. ' +
51+
'VITE_DEPLOYSTACK_BACKEND_URL is not set in your environment. ' +
5252
'Please ensure it is defined in .env.local (for local development) or as a runtime environment variable (for Docker). ' +
5353
'API call to logout will likely fail.'
5454
);

0 commit comments

Comments
 (0)