[Vuejs]-How to redirected to home after successful login with Vue.JS

0👍

So, I got my answer on the Vue forum just a few seconds after posting this : I need to return the promise of my authenticate method. So the new code is :

export function authenticate (context, creds) {
  let requestConfig = {
    headers: {
      'Content-Type': 'application/json'
    }
  }
  return Vue.http.post(
    url + apiPaths.auth,
    creds,
    requestConfig
  ).then(response => {
    return response.json()
  }).then(data => {
    context.commit('setUsername', creds.username)
    context.commit('setJwtToken', data.token)
  }).catch(error => {
    console.log('error:')
    console.log(error)
  })
}

Source

0👍

This looks a bit fishy for me.

First: I hope, you are not using vuex to hold your crentials/token.
Security tip #1, Security tip #2.

I would recommend only setting authenticated:true and use that for your requests.

Then:
Have a look at global guards of the vue router, which could be leveraged, to check, whether the current user is logged in.

Last:
To programatically route to sections of your app, you could use router.push.

So, you have to store, which route the user wanted before the login page and programmatically push this route afterwards.

Leave a comment