Library/frontend/src/auth/index.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-11-09 11:19:53 +00:00
import {HTTP} from '../http-common'
2017-11-10 10:49:02 +00:00
import router from '../router'
2017-11-09 11:19:53 +00:00
// const API_URL = 'http://localhost:8082/api/v1/'
// const LOGIN_URL = 'http://localhost:8082/login'
export default {
user: {
authenticated: false
},
2017-11-09 13:24:59 +00:00
login (context, creds, redirect) {
2017-11-09 11:19:53 +00:00
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
2017-11-09 13:24:59 +00:00
// Hide the loader
context.loading = false
// Redirect if nessecary
if (redirect) {
router.go(redirect)
}
2017-11-09 11:19:53 +00:00
})
.catch(e => {
2017-11-09 13:24:59 +00:00
// Hide the loader
context.loading = false
if (e.response) {
context.error = e.response.data.Message
}
2017-11-09 11:19:53 +00:00
})
},
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')
2017-11-09 11:19:53 +00:00
}
}