authGuard.js 707 B

12345678910111213141516171819202122232425
  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. user.applyUserPrefs()
  15. }
  16. }
  17. if (! user.isAuthenticated) {
  18. next({ name: 'login' })
  19. } else {
  20. next()
  21. }
  22. }