Skip to content

Commit 829721a

Browse files
committed
fix: update public routes to fix auth
1 parent b6d9d83 commit 829721a

5 files changed

Lines changed: 15 additions & 10 deletions

File tree

app/components/SideBar/Left/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const userStore = useUserStore();
2828
}"
2929
></SideBarLeftTab>
3030
<SideBarLeftTab :tab="{ label: 'more', icon: 'more-horiz', route: '#' }"></SideBarLeftTab>
31-
<UiButton variant="ghost-default" size="icon-xl" @click="loginService.logout()">
31+
<UiButton variant="ghost-default" size="icon-xl" @click="loginService.logout">
3232
<Icon name="ic:outline-logout" size="24" />
3333
</UiButton>
3434
</div>

app/middleware/auth.global.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { apiFetch } from '~/api';
22
import { isAuthenticated, parseSetCookie } from '~/services/auth/authService';
33

44
// Public routes that don’t require auth
5-
const publicRoutes = ['/', '/auth/forgot-password', '/playground/dummy-login'];
5+
const publicRoutePatterns: RegExp[] = [
6+
/^\/$/, // root "/"
7+
/^\/password-reset(?:\/.*)?$/, // "/reset-password" and subpaths
8+
/^\/auth(?:\/.*)?$/, // "/auth/*"
9+
/^\/profile(?:\/.*)?$/, // "/profile/*"
10+
];
611

712
export default defineNuxtRouteMiddleware(async (to) => {
813
let isAuth = isAuthenticated();
@@ -12,7 +17,8 @@ export default defineNuxtRouteMiddleware(async (to) => {
1217
return navigateTo('/home');
1318
}
1419
// Allow if it's a public route
15-
if (publicRoutes.includes(to.path)) return;
20+
const isPublic = publicRoutePatterns.some((regex) => regex.test(to.path));
21+
if (isPublic) return;
1622

1723
if (!isAuth) {
1824
// try to authenticate using refresh token

app/services/auth/loginService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { apiFetch } from '~/api';
2+
13
export interface LoginSchema {
24
identifier: string;
35
password: string;
@@ -22,7 +24,7 @@ export const loginService = {
2224
},
2325

2426
async logout() {
25-
const response = await $fetch<ApiResponseBase>('/api/auth/logout', {
27+
const response = await apiFetch<ApiResponseBase>('/api/auth/logout', {
2628
method: 'POST',
2729
});
2830
navigateTo('/');

nuxt.config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default defineNuxtConfig({
1111
githubClientId: process.env.NUXT_PUBLIC_GITHUB_CLIENT_ID || '',
1212
githubRedirectUri: process.env.NUXT_PUBLIC_GITHUB_REDIRECT_URI || '',
1313
githubScope: process.env.NUXT_PUBLIC_GITHUB_SCOPE || '',
14+
siteKey: process.env.NUXT_PUBLIC_RECAPTCHA_SITE_KEY || '',
1415
},
1516
},
1617
devtools: {
@@ -25,12 +26,6 @@ export default defineNuxtConfig({
2526
include: ['../shared/types/**/*.ts', '../mocks/**/*.ts', '../test/**/*.ts'],
2627
},
2728
},
28-
runtimeConfig: {
29-
public: {
30-
siteKey: process.env.NUXT_PUBLIC_RECAPTCHA_SITE_KEY || '',
31-
},
32-
},
33-
3429
app: {
3530
head: {
3631
link: [

server/api/auth/logout.post.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import type { ApiResponseBase } from '~~/shared/types/api';
33

44
export default defineWrappedResponseHandler(async (event) => {
55
const cookie = getHeader(event, 'cookie');
6+
const authHeader = getHeader(event, 'authorization');
67
const response = await serverApiFetch.raw<ApiResponseBase>('/auth/logout', {
78
method: 'POST',
89
credentials: 'include',
910
headers: {
1011
...(cookie ? { cookie } : {}), // Forward client cookies
12+
...(authHeader ? { Authorization: authHeader } : {}),
1113
},
1214
});
1315
deleteCookie(event, 'access_token', { path: '/' });

0 commit comments

Comments
 (0)