66 lines
1.4 KiB
JavaScript
66 lines
1.4 KiB
JavaScript
import {HTTP} from '../http-common'
|
|
import router from '../router'
|
|
// const API_URL = 'http://localhost:8082/api/v1/'
|
|
// const LOGIN_URL = 'http://localhost:8082/login'
|
|
|
|
export default {
|
|
|
|
user: {
|
|
authenticated: false
|
|
},
|
|
|
|
login (context, creds, redirect) {
|
|
HTTP.post('login', {
|
|
username: creds.username,
|
|
password: creds.password
|
|
})
|
|
.then(response => {
|
|
console.log(response)
|
|
// Save the token to local storage for later use
|
|
localStorage.setItem('token', response.data.token)
|
|
|
|
// Tell others the user is autheticated
|
|
this.user.authenticated = true
|
|
|
|
// Hide the loader
|
|
context.loading = false
|
|
|
|
// Redirect if nessecary
|
|
if (redirect) {
|
|
router.push({ name: redirect })
|
|
}
|
|
})
|
|
.catch(e => {
|
|
// Hide the loader
|
|
context.loading = false
|
|
if (e.response) {
|
|
context.error = e.response.data.Message
|
|
}
|
|
})
|
|
},
|
|
|
|
logout () {
|
|
localStorage.removeItem('token')
|
|
this.user.authenticated = false
|
|
},
|
|
|
|
checkAuth () {
|
|
var jwt = localStorage.getItem('token')
|
|
if (jwt) {
|
|
this.user.authenticated = true
|
|
} else {
|
|
this.user.authenticated = false
|
|
}
|
|
},
|
|
|
|
getAuthHeader () {
|
|
return {
|
|
'Authorization': 'Bearer ' + localStorage.getItem('token')
|
|
}
|
|
},
|
|
|
|
getToken () {
|
|
return localStorage.getItem('token')
|
|
}
|
|
}
|