Skip to content

Instantly share code, notes, and snippets.

@vouill

vouill/login.js Secret

Last active January 18, 2018 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vouill/02c2f57eb4e8cc4329af24365fc8d644 to your computer and use it in GitHub Desktop.
Save vouill/02c2f57eb4e8cc4329af24365fc8d644 to your computer and use it in GitHub Desktop.
// 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