Blame view

src/auth/authGuard.js 746 Bytes
8a0dd59d9   Digvijay Singh   first commit
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
  import { getInstance } from "./index"
  
  export const authGuard = (to, from, next) => {
      const authService = getInstance()
      const fn = () => {
          // If the user is authenticated, continue with the route
          if (authService.isAuthenticated) {
              return next()
          }
  
          // Otherwise, log in
          authService.loginWithRedirect({ appState: { targetUrl: to.fullPath } })
      };
  
      // If loading has already finished, check our auth state using `fn()`
      if (!authService.loading) {
          return fn()
      }
  
      // Watch for the loading property to change before we check isAuthenticated
      authService.$watch("loading", loading => {
          if (loading === false) {
              return fn()
          }
      });
  };