In my application, some routes are available only to authenticated users.
When an unauthenticated user clicks on the link for which he must be subscribed, he will be redirected to the login component.
If the user logs in successfully , I would like to redirect him to the URL that he requested before he was supposed to log in . However, there should also be a default route if the user did not request a different URL before logging in.
How can I achieve this with vue-router?
My code without redirection after loginrouter.beforeEach( (to, from, next) => { if(to.matched.some(record => record.meta.forVisitors)) { next() } else if(to.matched.some(record => record.meta.forAuth)) { if(!Vue.auth.isAuthenticated()) { next({ path: '/login'
My login function to my login component
login() { var data = { client_id: 2, client_secret: '**************', grant_type: 'password', username: this.email, password: this.password } // send data this.$http.post('oauth/token', data) .then(response => { // authenticate the user this.$auth.setToken(response.body.access_token, response.body.expires_in + Date.now()) // redirect to route after successful login this.$router.push('/') })
I would really appreciate any help!
Schwesi
source share