mirror of https://github.com/requarks/wiki
parent
8e87a5d489
commit
55b0b00cee
@ -1,47 +1,95 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import jwtDecode from 'jwt-decode'
|
import jwtDecode from 'jwt-decode'
|
||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
|
import gql from 'graphql-tag'
|
||||||
|
import { DateTime } from 'luxon'
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', {
|
export const useUserStore = defineStore('user', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
id: 0,
|
id: '10000000-0000-4000-8000-000000000001',
|
||||||
email: '',
|
email: '',
|
||||||
name: '',
|
name: '',
|
||||||
pictureUrl: '',
|
pictureUrl: '',
|
||||||
localeCode: '',
|
localeCode: '',
|
||||||
defaultEditor: '',
|
|
||||||
timezone: '',
|
timezone: '',
|
||||||
dateFormat: '',
|
dateFormat: 'YYYY-MM-DD',
|
||||||
appearance: '',
|
timeFormat: '12h',
|
||||||
|
appearance: 'site',
|
||||||
permissions: [],
|
permissions: [],
|
||||||
iat: 0,
|
iat: 0,
|
||||||
exp: 0,
|
exp: null,
|
||||||
authenticated: false
|
authenticated: false,
|
||||||
|
token: '',
|
||||||
|
profileLoaded: false
|
||||||
}),
|
}),
|
||||||
getters: {},
|
getters: {},
|
||||||
actions: {
|
actions: {
|
||||||
refreshAuth () {
|
async refreshAuth () {
|
||||||
const jwtCookie = Cookies.get('jwt')
|
const jwtCookie = Cookies.get('jwt')
|
||||||
if (jwtCookie) {
|
if (jwtCookie) {
|
||||||
try {
|
try {
|
||||||
const jwtData = jwtDecode(jwtCookie)
|
const jwtData = jwtDecode(jwtCookie)
|
||||||
this.id = jwtData.id
|
this.id = jwtData.id
|
||||||
this.email = jwtData.email
|
this.email = jwtData.email
|
||||||
this.name = jwtData.name
|
|
||||||
this.pictureUrl = jwtData.av
|
|
||||||
this.localeCode = jwtData.lc
|
|
||||||
this.timezone = jwtData.tz || Intl.DateTimeFormat().resolvedOptions().timeZone || ''
|
|
||||||
this.dateFormat = jwtData.df || ''
|
|
||||||
this.appearance = jwtData.ap || ''
|
|
||||||
// this.defaultEditor = jwtData.defaultEditor
|
|
||||||
this.permissions = jwtData.permissions
|
|
||||||
this.iat = jwtData.iat
|
this.iat = jwtData.iat
|
||||||
this.exp = jwtData.exp
|
this.exp = DateTime.fromSeconds(jwtData.exp, { zone: 'utc' })
|
||||||
this.authenticated = true
|
this.token = jwtCookie
|
||||||
|
if (this.exp <= DateTime.utc()) {
|
||||||
|
console.info('Token has expired. Attempting renew...')
|
||||||
|
} else {
|
||||||
|
this.authenticated = true
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.debug('Invalid JWT. Silent authentication skipped.')
|
console.debug('Invalid JWT. Silent authentication skipped.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
async refreshProfile () {
|
||||||
|
if (!this.authenticated || !this.id) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const respRaw = await APOLLO_CLIENT.query({
|
||||||
|
query: gql`
|
||||||
|
query refreshProfile (
|
||||||
|
$id: UUID!
|
||||||
|
) {
|
||||||
|
userById(id: $id) {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
email
|
||||||
|
meta
|
||||||
|
prefs
|
||||||
|
lastLoginAt
|
||||||
|
groups {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
variables: {
|
||||||
|
id: this.id
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const resp = respRaw?.data?.userById
|
||||||
|
if (!resp || resp.id !== this.id) {
|
||||||
|
throw new Error('Failed to fetch user profile!')
|
||||||
|
}
|
||||||
|
this.name = resp.name || 'Unknown User'
|
||||||
|
this.email = resp.email
|
||||||
|
this.pictureUrl = (resp.pictureUrl === 'local') ? `/_user/${this.id}/avatar` : resp.pictureUrl
|
||||||
|
this.location = resp.meta.location || ''
|
||||||
|
this.jobTitle = resp.meta.jobTitle || ''
|
||||||
|
this.pronouns = resp.meta.pronouns || ''
|
||||||
|
this.timezone = resp.prefs.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone || ''
|
||||||
|
this.dateFormat = resp.prefs.dateFormat || ''
|
||||||
|
this.timeFormat = resp.prefs.timeFormat || '12h'
|
||||||
|
this.appearance = resp.prefs.appearance || 'site'
|
||||||
|
this.profileLoaded = true
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in new issue