2023-09-27 10:44:54 +02:00
|
|
|
import authService from '@/services/authService'
|
|
|
|
|
|
|
|
export default async function auth({ to, next, stores }) {
|
|
|
|
const { user } = stores
|
|
|
|
|
|
|
|
// No authenticated user on the front-end side, we try to
|
|
|
|
// get an active user from the back-end side
|
|
|
|
if (! user.isAuthenticated) {
|
|
|
|
const currentUser = await authService.getCurrentUser()
|
|
|
|
if (currentUser) {
|
|
|
|
user.$patch({
|
|
|
|
name: currentUser.name,
|
2023-10-05 16:59:53 +02:00
|
|
|
email: currentUser.email,
|
2023-09-27 10:44:54 +02:00
|
|
|
preferences: currentUser.preferences,
|
|
|
|
isAdmin: currentUser.is_admin,
|
|
|
|
})
|
2023-10-04 09:30:05 +02:00
|
|
|
user.applyUserPrefs()
|
2023-09-27 10:44:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! user.isAuthenticated) {
|
|
|
|
next({ name: 'login' })
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|