authGuard.js 673 B

123456789101112131415161718192021222324
  1. import authService from '@/services/authService'
  2. export default async function auth({ to, next, stores }) {
  3. const { user } = stores
  4. // No authenticated user on the front-end side, we try to
  5. // get an active user from the back-end side
  6. if (! user.isAuthenticated) {
  7. const currentUser = await authService.getCurrentUser()
  8. if (currentUser) {
  9. user.$patch({
  10. name: currentUser.name,
  11. preferences: currentUser.preferences,
  12. isAdmin: currentUser.is_admin,
  13. })
  14. }
  15. }
  16. if (! user.isAuthenticated) {
  17. next({ name: 'login' })
  18. } else {
  19. next()
  20. }
  21. }