I'm working on an application that blocks a user from accessing any page besides home and signup unless they are signed in. I have a catch all route guard that checks if the user is signed in when trying to access a page and redirects to the sign in if it returns false. In the sign in component, once the user signs in, they are directed to the home page. What I want to set up is when the user signs in it redirects to the last page on the app if they had previously been on a page or had typed in a direct url to a page, if not redirect to the home page. For example:
User navigates to my.app/{somePage} -> route guard check for signin returns false -> redirect to my.app/sigin -> user successfully signs in -> redirects to my.app/{somePage}
//Route Guard
router.beforeEach((to, from, next) => {
if(!signedIn()){
router.replace('/signin')
}
else{
next()
}
})
//Successful signin
signInSuccess(){
//Want this to redirect to intended page if exists
this.$router.replace('/')
}
I've tried redirecting using this.$router.go(-1) but when I redirect to the signin page in the route guard the intended route doesn't seem to get pushed into the history.
I've also tried to set up the state in my vuex store to capture the intended route and use that to redirect after sign in but the route guard seems to reload the entire page so the store state gets reset.
The only other thing I can think of is to store the intended page route in the browser localstorage but that doesn't seem like a reliable solution.
router.push
instead ofrouter.replace
. See router.vuejs.org/guide/essentials/… – PenetratingreturnURL
query parameter in/signin
? – Durkin