Instantly share code, notes, and snippets.

@vouill /login.js Secret
Last active Jan 18, 2018

Embed
What would you like to do?
// Exported in a shared file
const myLoginRoutine = user => new Promise ((resolve, reject) => {
axios({url: 'auth', data: user, method: 'POST' })
.then(resp => {
const token = resp.data.token
localStorage.setItem('user-token', token) // store the token in localstorage
resolve(resp)
})
.catch(err => {
localStorage.removeItem('user-token') // if the request fails, remove any possible user token if possible
reject(err)
})
})
// in your vue component
import {myLoginRoutine} from 'the-created-shared-file-containing-auth-api-logic'
...
methods: {
login: function () {
const { username, password } = this
myLoginRoutine({ username, password }).then(() => {
this.$router.push('/')
})
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment