🚦 Add feature flag checks to route guards and logging for flagged routes
This commit is contained in:
@@ -1,24 +1,36 @@
|
|||||||
/**
|
/**
|
||||||
* Route Guards
|
* Route Guards
|
||||||
*
|
*
|
||||||
* Navigation guards for protecting admin routes
|
* Navigation guards for protecting admin routes and feature-flagged routes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { useAuth } from '../composables/useAuth.js';
|
import { useAuth } from '../composables/useAuth.js';
|
||||||
|
import { useFeatureFlags } from '../composables/useFeatureFlags.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create router guards with auth check
|
* Create router guards with auth and feature flag checks
|
||||||
* @param {Router} router - Vue Router instance
|
* @param {Router} router - Vue Router instance
|
||||||
*/
|
*/
|
||||||
export function setupAuthGuards(router) {
|
export function setupAuthGuards(router) {
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
const { isAuthenticated, initializeAuth } = useAuth();
|
const { isAuthenticated, initializeAuth } = useAuth();
|
||||||
|
const { isEnabled } = useFeatureFlags();
|
||||||
|
|
||||||
// Initialize auth from stored token
|
// Initialize auth from stored token
|
||||||
if (!isAuthenticated.value) {
|
if (!isAuthenticated.value) {
|
||||||
await initializeAuth();
|
await initializeAuth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if route is behind a feature flag
|
||||||
|
if (to.meta.featureFlag) {
|
||||||
|
const flagEnabled = isEnabled.value(to.meta.featureFlag);
|
||||||
|
if (!flagEnabled) {
|
||||||
|
console.warn(`[Router] Feature flag "${to.meta.featureFlag}" is disabled`);
|
||||||
|
next({ name: 'Home', replace: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if route requires admin access
|
// Check if route requires admin access
|
||||||
if (to.meta.requiresAdmin) {
|
if (to.meta.requiresAdmin) {
|
||||||
if (!isAuthenticated.value) {
|
if (!isAuthenticated.value) {
|
||||||
@@ -40,6 +52,9 @@ export function setupAuthGuards(router) {
|
|||||||
if (to.meta.requiresAdmin) {
|
if (to.meta.requiresAdmin) {
|
||||||
console.log(`[Auth] Navigated to protected route: ${to.path}`);
|
console.log(`[Auth] Navigated to protected route: ${to.path}`);
|
||||||
}
|
}
|
||||||
|
if (to.meta.featureFlag) {
|
||||||
|
console.log(`[FeatureFlag] Navigated to flagged route: ${to.path} (flag: ${to.meta.featureFlag})`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user