Skip to content

Commit fb0394d

Browse files
authored
feat: add auth wall to report issue modal (#1332)
Signed-off-by: Efren Lim <[email protected]>
1 parent f695609 commit fb0394d

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

frontend/app/components/shared/modules/report/store/report.store.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
import { defineStore } from 'pinia';
44
import { ref } from 'vue';
55
import type { ReportDataForm } from '~/components/shared/modules/report/types/report.types';
6+
import { useAuth } from '~~/composables/useAuth';
67

78
export const useReportStore = defineStore('report', () => {
89
const isReportModalOpen = ref(false);
910
const reportDataDefaults = ref<Partial<ReportDataForm>>({});
11+
12+
const { isAuthenticated, login } = useAuth();
13+
1014
const openReportModal = (defaults: Partial<ReportDataForm> = {}) => {
15+
if (!isAuthenticated.value) {
16+
login(window.location.pathname + window.location.search + window.location.hash);
17+
return;
18+
}
19+
1120
reportDataDefaults.value = defaults;
1221
isReportModalOpen.value = true;
1322
};

frontend/composables/useAuth.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ const setSilentLoginAttempted = (value: boolean): void => {
2525
localStorage.setItem('lfx-silent-login-attempted', value.toString());
2626
};
2727

28+
const getWasUserLoggedIn = (): boolean => {
29+
if (!process.client) return false;
30+
return localStorage.getItem('lfx-user-logged-in') === 'true';
31+
};
32+
33+
const setWasUserLoggedIn = (value: boolean): void => {
34+
if (!process.client) return;
35+
localStorage.setItem('lfx-user-logged-in', value.toString());
36+
};
37+
2838
export const useAuth = () => {
2939
// Fetch user data from server
3040
const { data: userData, refresh: refreshAuth } = useAsyncData<AuthData>(
@@ -47,14 +57,20 @@ export const useAuth = () => {
4757
authState.value = userData.value;
4858

4959
// Attempt silent login if suggested by the server and not already attempted
50-
if (userData.value.shouldAttemptSilentLogin && process.client && !getSilentLoginAttempted()) {
60+
if (
61+
userData.value.shouldAttemptSilentLogin &&
62+
process.client &&
63+
!getSilentLoginAttempted() &&
64+
getWasUserLoggedIn() // Only attempt silent login if the user has logged in previously
65+
) {
5166
const currentPath =
5267
window.location.pathname + window.location.search + window.location.hash;
5368
login(currentPath, true);
5469
}
5570

5671
if (userData.value.isAuthenticated) {
5772
setSilentLoginAttempted(false);
73+
setWasUserLoggedIn(true);
5874
}
5975
}
6076
});
@@ -184,6 +200,8 @@ export const useAuth = () => {
184200
} else {
185201
await navigateTo(response.logoutUrl, { external: true });
186202
}
203+
204+
setWasUserLoggedIn(false);
187205
}
188206
} catch (error) {
189207
console.error('Logout error:', error);

frontend/server/middleware/jwt-auth.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ const isJWT = (token: string) => {
1313
export default defineEventHandler(async (event) => {
1414
const url = getRouterParam(event, '_') || event.node.req.url || '';
1515

16-
const protectedRoutes = ['/api/chat/'];
16+
const protectedRoutes = ['/api/report'];
17+
const protectedAndPermissionRoutes = ['/api/chat'];
1718

18-
const isProtectedRoute = protectedRoutes.some((route) => url.startsWith(route));
19+
const isProtectedRoute = [...protectedRoutes, ...protectedAndPermissionRoutes].some((route) =>
20+
url.startsWith(route),
21+
);
22+
23+
const isPermissionRequired = protectedAndPermissionRoutes.some((route) => url.startsWith(route));
1924

2025
if (!isProtectedRoute) {
2126
return;
@@ -24,8 +29,6 @@ export default defineEventHandler(async (event) => {
2429
const config = useRuntimeConfig();
2530
const oidcToken = getCookie(event, 'auth_oidc_token');
2631

27-
// Read authorization header
28-
// const authHeader = getHeader(event, 'authorization')
2932
if (!oidcToken) {
3033
throw createError({
3134
statusCode: 401,
@@ -42,7 +45,7 @@ export default defineEventHandler(async (event) => {
4245
if (decodedToken.original_id_token && isJWT(decodedToken.original_id_token)) {
4346
event.context.user = decodedToken;
4447

45-
if (!isLocal && !decodedToken.hasLfxInsightsPermission) {
48+
if (!isLocal && isPermissionRequired && !decodedToken.hasLfxInsightsPermission) {
4649
throw createError({
4750
statusCode: 401,
4851
statusMessage: `User does not belong to ${config.lfxAuth0TokenClaimGroupName}`,
@@ -58,7 +61,7 @@ export default defineEventHandler(async (event) => {
5861
console.error('JWT verification failed:', jwtError);
5962
throw createError({
6063
statusCode: 401,
61-
statusMessage: 'Invalid JWT token',
64+
statusMessage: jwtError instanceof Error ? jwtError.message : 'Invalid JWT token',
6265
});
6366
}
6467
});

0 commit comments

Comments
 (0)