2021-07-25 11:49:15 +02:00
|
|
|
import {HTTPFactory} from '@/http-common'
|
|
|
|
import {ERROR_MESSAGE, LOADING} from '../mutation-types'
|
2020-09-05 22:35:52 +02:00
|
|
|
import UserModel from '../../models/user'
|
2021-07-09 20:10:57 +02:00
|
|
|
import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
|
2020-05-08 20:43:51 +02:00
|
|
|
|
2021-06-03 18:12:40 +02:00
|
|
|
const defaultSettings = settings => {
|
|
|
|
if (typeof settings.weekStart === 'undefined' || settings.weekStart === '') {
|
|
|
|
settings.weekStart = 0
|
|
|
|
}
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
2020-05-08 20:43:51 +02:00
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state: () => ({
|
|
|
|
authenticated: false,
|
|
|
|
isLinkShareAuth: false,
|
2021-04-07 15:58:29 +02:00
|
|
|
info: null,
|
2020-05-08 20:43:51 +02:00
|
|
|
needsTotpPasscode: false,
|
2020-08-02 19:17:29 +02:00
|
|
|
avatarUrl: '',
|
2021-04-07 15:58:29 +02:00
|
|
|
lastUserInfoRefresh: null,
|
2021-04-07 21:31:14 +02:00
|
|
|
settings: {},
|
2020-05-08 20:43:51 +02:00
|
|
|
}),
|
|
|
|
mutations: {
|
|
|
|
info(state, info) {
|
|
|
|
state.info = info
|
2021-04-07 15:58:29 +02:00
|
|
|
if (info !== null) {
|
|
|
|
state.avatarUrl = info.getAvatarUrl()
|
2021-04-09 16:29:07 +02:00
|
|
|
|
|
|
|
if (info.settings) {
|
2021-06-03 18:12:40 +02:00
|
|
|
state.settings = defaultSettings(info.settings)
|
2021-04-09 16:29:07 +02:00
|
|
|
}
|
2021-07-25 11:49:15 +02:00
|
|
|
|
|
|
|
state.isLinkShareAuth = info.id < 0
|
2021-04-07 21:31:14 +02:00
|
|
|
}
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2021-04-07 21:31:14 +02:00
|
|
|
setUserSettings(state, settings) {
|
2021-06-03 18:12:40 +02:00
|
|
|
state.settings = defaultSettings(settings)
|
2021-04-07 21:31:14 +02:00
|
|
|
const info = state.info !== null ? state.info : {}
|
|
|
|
info.name = settings.name
|
|
|
|
state.info = info
|
2020-11-21 22:25:00 +01:00
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
authenticated(state, authenticated) {
|
|
|
|
state.authenticated = authenticated
|
|
|
|
},
|
|
|
|
isLinkShareAuth(state, is) {
|
|
|
|
state.isLinkShareAuth = is
|
|
|
|
},
|
|
|
|
needsTotpPasscode(state, needs) {
|
|
|
|
state.needsTotpPasscode = needs
|
|
|
|
},
|
2020-08-02 19:17:29 +02:00
|
|
|
reloadAvatar(state) {
|
|
|
|
state.avatarUrl = `${state.info.getAvatarUrl()}&=${+new Date()}`
|
|
|
|
},
|
2021-04-07 15:58:29 +02:00
|
|
|
lastUserRefresh(state) {
|
|
|
|
state.lastUserInfoRefresh = new Date()
|
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
// Logs a user in with a set of credentials.
|
|
|
|
login(ctx, credentials) {
|
2020-10-11 12:13:35 +02:00
|
|
|
const HTTP = HTTPFactory()
|
2021-07-25 11:49:15 +02:00
|
|
|
ctx.commit(LOADING, true, {root: true})
|
2020-05-08 20:43:51 +02:00
|
|
|
|
|
|
|
// Delete an eventually preexisting old token
|
2021-07-09 20:10:57 +02:00
|
|
|
removeToken()
|
2020-05-08 20:43:51 +02:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
const data = {
|
2020-05-08 20:43:51 +02:00
|
|
|
username: credentials.username,
|
2020-09-05 22:35:52 +02:00
|
|
|
password: credentials.password,
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
if (credentials.totpPasscode) {
|
2020-05-08 20:43:51 +02:00
|
|
|
data.totp_passcode = credentials.totpPasscode
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP.post('login', data)
|
|
|
|
.then(response => {
|
|
|
|
// Save the token to local storage for later use
|
2021-07-25 11:49:15 +02:00
|
|
|
saveToken(response.data.token, true)
|
2020-05-08 20:43:51 +02:00
|
|
|
|
|
|
|
// Tell others the user is autheticated
|
|
|
|
ctx.dispatch('checkAuth')
|
|
|
|
return Promise.resolve()
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
if (e.response) {
|
|
|
|
if (e.response.data.code === 1017 && !credentials.totpPasscode) {
|
|
|
|
ctx.commit('needsTotpPasscode', true)
|
2021-06-24 01:24:57 +02:00
|
|
|
return Promise.reject(e)
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 16:29:07 +02:00
|
|
|
|
2021-06-24 01:24:57 +02:00
|
|
|
return Promise.reject(e)
|
2020-05-08 20:43:51 +02:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2021-07-25 11:49:15 +02:00
|
|
|
ctx.commit(LOADING, false, {root: true})
|
2020-05-08 20:43:51 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
// Registers a new user and logs them in.
|
|
|
|
// Not sure if this is the right place to put the logic in, maybe a seperate js component would be better suited.
|
|
|
|
register(ctx, credentials) {
|
2020-10-11 12:13:35 +02:00
|
|
|
const HTTP = HTTPFactory()
|
2020-06-21 20:54:57 +02:00
|
|
|
return HTTP.post('register', {
|
2020-05-08 20:43:51 +02:00
|
|
|
username: credentials.username,
|
|
|
|
email: credentials.email,
|
2020-09-05 22:35:52 +02:00
|
|
|
password: credentials.password,
|
2020-05-08 20:43:51 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return ctx.dispatch('login', credentials)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
2021-04-09 16:29:07 +02:00
|
|
|
if (e.response && e.response.data && e.response.data.message) {
|
2021-07-25 11:49:15 +02:00
|
|
|
ctx.commit(ERROR_MESSAGE, e.response.data.message, {root: true})
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
2021-04-09 16:29:07 +02:00
|
|
|
|
|
|
|
return Promise.reject(e)
|
2020-05-08 20:43:51 +02:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2021-07-25 11:49:15 +02:00
|
|
|
ctx.commit(LOADING, false, {root: true})
|
2020-05-08 20:43:51 +02:00
|
|
|
})
|
|
|
|
},
|
2021-07-25 11:49:15 +02:00
|
|
|
openIdAuth(ctx, {provider, code}) {
|
2020-11-21 17:38:40 +01:00
|
|
|
const HTTP = HTTPFactory()
|
2021-07-25 11:49:15 +02:00
|
|
|
ctx.commit(LOADING, true, {root: true})
|
2020-11-21 17:38:40 +01:00
|
|
|
|
|
|
|
const data = {
|
|
|
|
code: code,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete an eventually preexisting old token
|
2021-07-09 20:10:57 +02:00
|
|
|
removeToken()
|
2020-11-21 17:38:40 +01:00
|
|
|
return HTTP.post(`/auth/openid/${provider}/callback`, data)
|
|
|
|
.then(response => {
|
|
|
|
// Save the token to local storage for later use
|
2021-07-25 11:49:15 +02:00
|
|
|
saveToken(response.data.token, true)
|
2020-11-21 17:38:40 +01:00
|
|
|
|
|
|
|
// Tell others the user is autheticated
|
|
|
|
ctx.dispatch('checkAuth')
|
|
|
|
return Promise.resolve()
|
|
|
|
})
|
|
|
|
.catch(e => {
|
2021-06-24 01:24:57 +02:00
|
|
|
return Promise.reject(e)
|
2020-11-21 17:38:40 +01:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2021-07-25 11:49:15 +02:00
|
|
|
ctx.commit(LOADING, false, {root: true})
|
2020-11-21 17:38:40 +01:00
|
|
|
})
|
|
|
|
},
|
2021-07-25 11:49:15 +02:00
|
|
|
linkShareAuth(ctx, {hash, password}) {
|
2020-10-11 12:13:35 +02:00
|
|
|
const HTTP = HTTPFactory()
|
2021-04-11 15:18:19 +02:00
|
|
|
return HTTP.post('/shares/' + hash + '/auth', {
|
|
|
|
password: password,
|
|
|
|
})
|
2020-05-08 20:43:51 +02:00
|
|
|
.then(r => {
|
2021-07-09 20:10:57 +02:00
|
|
|
saveToken(r.data.token, false)
|
2020-05-08 20:43:51 +02:00
|
|
|
ctx.dispatch('checkAuth')
|
|
|
|
return Promise.resolve(r.data)
|
|
|
|
}).catch(e => {
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// Populates user information from jwt token saved in local storage in store
|
|
|
|
checkAuth(ctx) {
|
2021-04-07 15:58:29 +02:00
|
|
|
|
|
|
|
// This function can be called from multiple places at the same time and shortly after one another.
|
|
|
|
// To prevent hitting the api too frequently or race conditions, we check at most once per minute.
|
|
|
|
if (ctx.state.lastUserInfoRefresh !== null && ctx.state.lastUserInfoRefresh > (new Date()).setMinutes((new Date()).getMinutes() + 1)) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
2021-07-09 20:10:57 +02:00
|
|
|
const jwt = getToken()
|
2020-05-08 20:43:51 +02:00
|
|
|
let authenticated = false
|
|
|
|
if (jwt) {
|
|
|
|
const base64 = jwt
|
|
|
|
.split('.')[1]
|
|
|
|
.replace('-', '+')
|
|
|
|
.replace('_', '/')
|
|
|
|
const info = new UserModel(JSON.parse(window.atob(base64)))
|
|
|
|
const ts = Math.round((new Date()).getTime() / 1000)
|
2021-04-07 15:58:29 +02:00
|
|
|
authenticated = info.exp >= ts
|
2020-05-08 20:43:51 +02:00
|
|
|
ctx.commit('info', info)
|
2021-04-07 15:58:29 +02:00
|
|
|
|
2021-04-07 21:31:14 +02:00
|
|
|
if (authenticated) {
|
2021-08-11 21:08:18 +02:00
|
|
|
ctx.dispatch('refreshUserInfo')
|
|
|
|
ctx.commit('authenticated', authenticated)
|
2021-04-07 15:58:29 +02:00
|
|
|
}
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
2021-04-07 15:58:29 +02:00
|
|
|
|
2020-05-08 20:43:51 +02:00
|
|
|
ctx.commit('authenticated', authenticated)
|
2021-04-07 15:58:29 +02:00
|
|
|
if (!authenticated) {
|
|
|
|
ctx.commit('info', null)
|
|
|
|
}
|
|
|
|
|
2020-05-16 12:02:30 +02:00
|
|
|
return Promise.resolve()
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2021-08-11 21:08:18 +02:00
|
|
|
refreshUserInfo(ctx) {
|
|
|
|
const jwt = getToken()
|
|
|
|
if (!jwt) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const HTTP = HTTPFactory()
|
|
|
|
// We're not returning the promise here to prevent blocking the initial ui render if the user is
|
|
|
|
// accessing the site with a token in local storage
|
|
|
|
HTTP.get('user', {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${jwt}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(r => {
|
|
|
|
const info = new UserModel(r.data)
|
|
|
|
info.type = ctx.state.info.type
|
|
|
|
info.email = ctx.state.info.email
|
|
|
|
info.exp = ctx.state.info.exp
|
|
|
|
|
|
|
|
ctx.commit('info', info)
|
|
|
|
ctx.commit('lastUserRefresh')
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
console.error('Error while refreshing user info:', e)
|
|
|
|
})
|
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
// Renews the api token and saves it to local storage
|
|
|
|
renewToken(ctx) {
|
2021-07-10 12:32:04 +02:00
|
|
|
// Timeout to avoid race conditions when authenticated as a user (=auth token in localStorage) and as a
|
|
|
|
// link share in another tab. Without the timeout both the token renew and link share auth are executed at
|
|
|
|
// the same time and one might win over the other.
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!ctx.state.authenticated) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-08 20:43:51 +02:00
|
|
|
|
2021-07-25 11:49:15 +02:00
|
|
|
refreshToken(!ctx.state.isLinkShareAuth)
|
2021-07-10 12:32:04 +02:00
|
|
|
.then(() => {
|
|
|
|
ctx.dispatch('checkAuth')
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
// Don't logout on network errors as the user would then get logged out if they don't have
|
|
|
|
// internet for a short period of time - such as when the laptop is still reconnecting
|
|
|
|
if (e.request.status) {
|
|
|
|
ctx.dispatch('logout')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, 5000)
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
|
|
|
logout(ctx) {
|
2021-07-09 20:10:57 +02:00
|
|
|
removeToken()
|
2020-05-08 20:43:51 +02:00
|
|
|
ctx.dispatch('checkAuth')
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
|
|
|
}
|