I need to redirect users to login page if they are not authenticated. I need something like route.beforeEach
in Vue.js, ideally:
sapper.beforeRouteChange((to, from, next) => {
const isAuth = "[some session or token check]";
if (!isAuth) {
next('/login')
}
next()
})
I found Sapper - protected routes (route guard) this question but I think it's not enough for my needs. What if token or auth changes in runtime? OR is it covered by reactivity?
Edit 1: I think that this issue on Sapper GitHub solves my problem.
So I placed this code to
/src/routes/_layout.svelte
:And this is
authMiddleware.js
file:more information on route hooks can be found here
You can also use
authenticationMiddleware.js
insideserver.js
fileHere is
authenticationMiddleware.js
filee.g. https://github.com/itswadesh/sapper-ecommerce/blob/master/src/server.js