2019-07-11 17:23:10 +00:00
|
|
|
/**
|
|
|
|
* If this middleware is added to a page, it will redirect back to the login
|
|
|
|
* page if the user is not authenticated.
|
|
|
|
*/
|
2022-10-28 16:04:23 +00:00
|
|
|
export default function ({ req, store, route, redirect }) {
|
2019-07-11 17:23:10 +00:00
|
|
|
// If nuxt generate, pass this middleware
|
|
|
|
if (process.server && !req) return
|
|
|
|
|
|
|
|
if (!store.getters['auth/isAuthenticated']) {
|
2020-11-02 16:47:20 +00:00
|
|
|
const query = {}
|
|
|
|
if (req) {
|
|
|
|
query.original = encodeURI(req.originalUrl)
|
2023-06-06 11:01:11 +02:00
|
|
|
} else {
|
|
|
|
query.original = route.path
|
2020-11-02 16:47:20 +00:00
|
|
|
}
|
2023-06-06 11:01:11 +02:00
|
|
|
|
2021-02-09 10:29:53 +00:00
|
|
|
return redirect({ name: 'login', query })
|
2019-07-11 17:23:10 +00:00
|
|
|
}
|
|
|
|
}
|