2018-09-06 17:46:09 +00:00
|
|
|
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 {
|
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
user: {
|
|
|
|
authenticated: false,
|
2019-09-09 17:55:43 +00:00
|
|
|
infos: {},
|
2019-03-02 10:25:10 +00:00
|
|
|
},
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
login(context, creds, redirect) {
|
|
|
|
localStorage.removeItem('token') // Delete an eventually preexisting old token
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
HTTP.post('login', {
|
|
|
|
username: creds.username,
|
|
|
|
password: creds.password
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
// Save the token to local storage for later use
|
|
|
|
localStorage.setItem('token', response.data.token)
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
// Tell others the user is autheticated
|
|
|
|
this.user.authenticated = true
|
2019-09-09 17:55:43 +00:00
|
|
|
this.user.isLinkShareAuth = false
|
2019-06-06 10:52:05 +00:00
|
|
|
const inf = this.getUserInfos()
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.log(inf)
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
// Hide the loader
|
|
|
|
context.loading = false
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
// Redirect if nessecary
|
|
|
|
if (redirect) {
|
|
|
|
router.push({name: redirect})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
// Hide the loader
|
|
|
|
context.loading = false
|
|
|
|
if (e.response) {
|
2020-01-31 15:33:14 +00:00
|
|
|
context.errorMsg = e.response.data.message
|
2019-03-02 10:25:10 +00:00
|
|
|
if (e.response.status === 401) {
|
2020-01-31 15:33:14 +00:00
|
|
|
context.errorMsg = 'Wrong username or password.'
|
2019-03-02 10:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-09-08 20:27:13 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
register(context, creds, redirect) {
|
|
|
|
HTTP.post('register', {
|
|
|
|
username: creds.username,
|
|
|
|
email: creds.email,
|
|
|
|
password: creds.password
|
|
|
|
})
|
2019-04-29 21:41:39 +00:00
|
|
|
.then(() => {
|
2019-03-02 10:25:10 +00:00
|
|
|
this.login(context, creds, redirect)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
// Hide the loader
|
|
|
|
context.loading = false
|
|
|
|
if (e.response) {
|
2020-01-31 15:33:14 +00:00
|
|
|
context.errorMsg = e.response.data.message
|
2019-03-02 10:25:10 +00:00
|
|
|
if (e.response.status === 401) {
|
2020-01-31 15:33:14 +00:00
|
|
|
context.errorMsg = 'Wrong username or password.'
|
2019-03-02 10:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
logout() {
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
router.push({name: 'login'})
|
|
|
|
this.user.authenticated = false
|
|
|
|
},
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-09-09 17:55:43 +00:00
|
|
|
linkShareAuth(hash) {
|
2019-12-19 20:50:07 +00:00
|
|
|
return HTTP.post('/shares/' + hash + '/auth')
|
2019-09-09 17:55:43 +00:00
|
|
|
.then(r => {
|
|
|
|
localStorage.setItem('token', r.data.token)
|
|
|
|
this.getUserInfos()
|
|
|
|
return Promise.resolve(r.data)
|
2019-12-19 20:50:07 +00:00
|
|
|
}).catch(e => {
|
2019-09-09 17:55:43 +00:00
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-12-19 20:50:07 +00:00
|
|
|
renewToken() {
|
|
|
|
HTTP.post('user/token', null, {
|
|
|
|
headers: {
|
|
|
|
Authorization: 'Bearer ' + localStorage.getItem('token'),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(r => {
|
|
|
|
localStorage.setItem('token', r.data.token)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.log('Error renewing token: ', e)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
checkAuth() {
|
|
|
|
let jwt = localStorage.getItem('token')
|
|
|
|
this.getUserInfos()
|
|
|
|
this.user.authenticated = false
|
|
|
|
if (jwt) {
|
|
|
|
let infos = this.user.infos
|
|
|
|
let ts = Math.round((new Date()).getTime() / 1000)
|
|
|
|
if (infos.exp >= ts) {
|
|
|
|
this.user.authenticated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
getUserInfos() {
|
|
|
|
let jwt = localStorage.getItem('token')
|
|
|
|
if (jwt) {
|
|
|
|
this.user.infos = this.parseJwt(localStorage.getItem('token'))
|
|
|
|
return this.parseJwt(localStorage.getItem('token'))
|
|
|
|
} else {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
},
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
parseJwt(token) {
|
|
|
|
let base64Url = token.split('.')[1]
|
|
|
|
let base64 = base64Url.replace('-', '+').replace('_', '/')
|
|
|
|
return JSON.parse(window.atob(base64))
|
|
|
|
},
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
getAuthHeader() {
|
|
|
|
return {
|
|
|
|
'Authorization': 'Bearer ' + localStorage.getItem('token')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getToken() {
|
|
|
|
return localStorage.getItem('token')
|
|
|
|
}
|
2018-09-06 17:46:09 +00:00
|
|
|
}
|