Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- login() {
- this.$store.dispatch('RETRIEVE_TOKEN', {
- username: this.username,
- password: this.password,
- })
- }
- RETRIEVE_TOKEN(context, credentials) {
- return new Promise((resolve, reject) => {
- axios.post('someurl/auth', {
- email: credentials.username,
- password: credentials.password
- })
- .then(response => {
- const token = response.data.key
- localStorage.setItem('key', token)
- context.commit('retrieveToken', token)
- resolve(response)
- this.$router.push('/backoffice')
- })
- .catch(error => {
- console.log(error);
- reject(error)
- });
- })
- }
- RETRIEVE_TOKEN(context, credentials) {
- return axios.post('someurl/auth', {
- email: credentials.username,
- password: credentials.password
- })
- .then(response => {
- console.log(response.data) // If it not works, please show what's logged here
- const token = response.data.key
- if (token) { // check if token is valid
- localStorage.setItem('key', token)
- context.commit('retrieveToken', token)
- this.$router.push('/backoffice')
- } else {
- console.log('Token is not available')
- }
- })
- .catch(error => {
- console.log(error);
- });
- }
Add Comment
Please, Sign In to add comment