mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-12-12 10:10:59 +01:00
24 lines
673 B
JavaScript
24 lines
673 B
JavaScript
|
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,
|
||
|
preferences: currentUser.preferences,
|
||
|
isAdmin: currentUser.is_admin,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (! user.isAuthenticated) {
|
||
|
next({ name: 'login' })
|
||
|
} else {
|
||
|
next()
|
||
|
}
|
||
|
}
|