-
-
Save vouill/02c2f57eb4e8cc4329af24365fc8d644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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