All files / src/guards authGuard.js

100% Statements 14/14
100% Branches 9/9
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32      2x   2x     21x       16x 1x 1x     15x 3x 3x 3x     12x   12x 2x   10x      
import { NOTIF_FQ } from '../store/types';
import createLogger from '../utils/logger';
 
const log = createLogger('authGuard');
 
let storeInstance = null;
 
export function setStoreForGuards(store) {
  storeInstance = store;
}
 
export function authErrorGuard(to, from, next) {
  if (typeof window === 'undefined') {
    next();
    return;
  }
 
  if (!storeInstance) {
    log.warn('Store not yet initialized, allowing navigation');
    next();
    return;
  }
 
  const isAuthError = storeInstance.getters[NOTIF_FQ.getters.isAuthError];
 
  if (isAuthError && from.path === '/' && to.path !== '/') {
    next('/');
  } else {
    next();
  }
}