wikijs-fork/client/store/user.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-12-03 02:42:43 +00:00
import { make } from 'vuex-pathify'
import jwt from 'jsonwebtoken'
import Cookies from 'js-cookie'
const state = {
id: 0,
email: '',
name: '',
pictureUrl: '',
localeCode: '',
defaultEditor: '',
timezone: '',
dateFormat: '',
appearance: '',
2018-12-03 02:42:43 +00:00
permissions: [],
iat: 0,
exp: 0,
authenticated: false
}
export default {
namespaced: true,
state,
mutations: {
...make.mutations(state),
2019-09-08 16:39:05 +00:00
REFRESH_AUTH(st) {
2018-12-03 02:42:43 +00:00
const jwtCookie = Cookies.get('jwt')
if (jwtCookie) {
try {
const jwtData = jwt.decode(jwtCookie)
2019-09-08 16:39:05 +00:00
st.id = jwtData.id
st.email = jwtData.email
st.name = jwtData.name
st.pictureUrl = jwtData.av
st.localeCode = jwtData.lc
st.timezone = jwtData.tz || Intl.DateTimeFormat().resolvedOptions().timeZone || ''
st.dateFormat = jwtData.df || ''
st.appearance = jwtData.ap || ''
// st.defaultEditor = jwtData.defaultEditor
2019-09-08 16:39:05 +00:00
st.permissions = jwtData.permissions
st.iat = jwtData.iat
st.exp = jwtData.exp
st.authenticated = true
2018-12-03 02:42:43 +00:00
} catch (err) {
console.debug('Invalid JWT. Silent authentication skipped.')
}
}
}
}
}