2018-09-06 17:46:09 +00:00
|
|
|
import {HTTP} from '../http-common'
|
|
|
|
import router from '../router'
|
2020-02-09 12:28:33 +00:00
|
|
|
import UserModel from '../models/user'
|
2018-09-06 17:46:09 +00:00
|
|
|
// 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
|
|
|
|
2020-04-17 23:46:07 +00:00
|
|
|
login(context, credentials, redirect = '') {
|
2019-03-02 10:25:10 +00:00
|
|
|
localStorage.removeItem('token') // Delete an eventually preexisting old token
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2020-04-17 23:46:07 +00:00
|
|
|
const data = {
|
|
|
|
username: credentials.username,
|
|
|
|
password: credentials.password
|
|
|
|
}
|
|
|
|
|
|
|
|
if(credentials.totpPasscode) {
|
|
|
|
data.totp_passcode = credentials.totpPasscode
|
|
|
|
}
|
|
|
|
|
|
|
|
HTTP.post('login', data)
|
2019-03-02 10:25:10 +00:00
|
|
|
.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
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
// Redirect if nessecary
|
2020-04-17 23:46:07 +00:00
|
|
|
if (redirect !== '') {
|
2019-03-02 10:25:10 +00:00
|
|
|
router.push({name: redirect})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
if (e.response) {
|
2020-04-17 23:46:07 +00:00
|
|
|
if (e.response.data.code === 1017 && !credentials.totpPasscode) {
|
|
|
|
context.needsTotpPasscode = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-04-17 23:46:07 +00:00
|
|
|
.finally(() => {
|
|
|
|
context.loading = false
|
|
|
|
})
|
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 ts = Math.round((new Date()).getTime() / 1000)
|
2020-02-09 12:28:33 +00:00
|
|
|
if (this.user.infos.exp >= ts) {
|
2019-03-02 10:25:10 +00:00
|
|
|
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) {
|
2020-02-09 12:28:33 +00:00
|
|
|
this.user.infos = new UserModel(this.parseJwt(localStorage.getItem('token')))
|
|
|
|
return this.user.infos
|
2019-03-02 10:25:10 +00:00
|
|
|
} 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
|
|
|
}
|