2023-09-27 10:44:54 +02:00
|
|
|
import authService from '@/services/authService'
|
|
|
|
|
2023-11-15 14:07:26 +01:00
|
|
|
export default async function authGuard({ to, next, nextMiddleware, stores }) {
|
2023-09-27 10:44:54 +02:00
|
|
|
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) {
|
2023-11-15 14:07:26 +01:00
|
|
|
await authService.getCurrentUser({ returnError: true }).then(async (response) => {
|
|
|
|
const currentUser = response.data
|
2023-11-02 08:13:31 +01:00
|
|
|
await user.loginAs({
|
2023-09-27 10:44:54 +02:00
|
|
|
name: currentUser.name,
|
2023-10-05 16:59:53 +02:00
|
|
|
email: currentUser.email,
|
2023-12-09 17:22:24 +01:00
|
|
|
oauth_provider: currentUser.oauth_provider,
|
2023-09-27 10:44:54 +02:00
|
|
|
preferences: currentUser.preferences,
|
|
|
|
isAdmin: currentUser.is_admin,
|
|
|
|
})
|
2023-11-15 14:07:26 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
// nothing to do
|
|
|
|
})
|
2023-09-27 10:44:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! user.isAuthenticated) {
|
|
|
|
next({ name: 'login' })
|
|
|
|
} else {
|
2023-11-15 14:07:26 +01:00
|
|
|
nextMiddleware()
|
2023-09-27 10:44:54 +02:00
|
|
|
}
|
|
|
|
}
|