You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

  1. import { make } from 'vuex-pathify'
  2. import jwt from 'jsonwebtoken'
  3. import Cookies from 'js-cookie'
  4. const state = {
  5. id: 0,
  6. email: '',
  7. name: '',
  8. pictureUrl: '',
  9. localeCode: '',
  10. defaultEditor: '',
  11. timezone: '',
  12. dateFormat: '',
  13. appearance: '',
  14. permissions: [],
  15. iat: 0,
  16. exp: 0,
  17. authenticated: false
  18. }
  19. export default {
  20. namespaced: true,
  21. state,
  22. mutations: {
  23. ...make.mutations(state),
  24. REFRESH_AUTH(st) {
  25. const jwtCookie = Cookies.get('jwt')
  26. if (jwtCookie) {
  27. try {
  28. const jwtData = jwt.decode(jwtCookie)
  29. st.id = jwtData.id
  30. st.email = jwtData.email
  31. st.name = jwtData.name
  32. st.pictureUrl = jwtData.av
  33. st.localeCode = jwtData.lc
  34. st.timezone = jwtData.tz || Intl.DateTimeFormat().resolvedOptions().timeZone || ''
  35. st.dateFormat = jwtData.df || ''
  36. st.appearance = jwtData.ap || ''
  37. // st.defaultEditor = jwtData.defaultEditor
  38. st.permissions = jwtData.permissions
  39. st.iat = jwtData.iat
  40. st.exp = jwtData.exp
  41. st.authenticated = true
  42. } catch (err) {
  43. console.debug('Invalid JWT. Silent authentication skipped.')
  44. }
  45. }
  46. }
  47. }
  48. }