Moriz Wahl a3178a91b4
refactor(webapp): store for categories (#8551)
* after authentification, query the categories if active and store them

* get categories from store

* use category store to get categories

* get categories from store

* mock store to have access to categories

* to get rid of the active categories config variable in the frontend, the Category query returns an empty array when categories are not active

* remove CATEGORIES_ACTIVE from .env

* should return string to avoid warnings in console

* replace all env calls for categories active by getter from store

* use categoriesActive getter

* ignore order of returned categories

* mixin to get the category infos from the store, to ensure, that the quey has been called

* fix misspelling

---------

Co-authored-by: Wolfgang Huß <wolle.huss@pjannto.com>
2025-05-27 15:03:26 +02:00

126 lines
3.0 KiB
JavaScript

import gql from 'graphql-tag'
import { VERSION } from '~/constants/terms-and-conditions-version.js'
import { currentUserQuery } from '~/graphql/User'
import Cookie from 'universal-cookie'
import metadata from '~/constants/metadata'
const cookies = new Cookie()
export const state = () => {
return {
user: null,
token: null,
pending: false,
}
}
export const mutations = {
SET_USER(state, user) {
state.user = user || null
},
SET_USER_PARTIAL(state, user) {
state.user = { ...state.user, ...user }
},
SET_TOKEN(state, token) {
state.token = token || null
},
SET_PENDING(state, pending) {
state.pending = pending
},
}
export const getters = {
isAuthenticated(state) {
return !!state.token
},
isLoggedIn(state) {
return !!(state.user && state.token)
},
pending(state) {
return !!state.pending
},
isAdmin(state) {
return !!state.user && state.user.role === 'admin'
},
isModerator(state) {
return !!state.user && (state.user.role === 'admin' || state.user.role === 'moderator')
},
user(state) {
return state.user || {}
},
token(state) {
return state.token
},
termsAndConditionsAgreed(state) {
return state.user && state.user.termsAndConditionsAgreedVersion === VERSION
},
}
export const actions = {
async init({ commit, dispatch }) {
if (!process.server) {
return
}
const token = this.app.$apolloHelpers.getToken()
if (!token) {
return
}
commit('SET_TOKEN', token)
await dispatch('fetchCurrentUser')
},
async check({ commit, dispatch, getters }) {
if (!this.app.$apolloHelpers.getToken()) {
await dispatch('logout')
}
return getters.isLoggedIn
},
async fetchCurrentUser({ commit, dispatch }) {
const client = this.app.apolloProvider.defaultClient
const {
data: { currentUser },
} = await client.query({ query: currentUserQuery })
if (!currentUser) return dispatch('logout')
commit('SET_USER', currentUser)
return currentUser
},
async login({ commit, dispatch }, { email, password }) {
commit('SET_PENDING', true)
try {
const client = this.app.apolloProvider.defaultClient
const {
data: { login },
} = await client.mutate({
mutation: gql(`
mutation($email: String!, $password: String!) {
login(email: $email, password: $password)
}
`),
variables: {
email,
password,
},
})
await this.app.$apolloHelpers.onLogin(login)
commit('SET_TOKEN', login)
await dispatch('fetchCurrentUser')
await dispatch('categories/init', null, { root: true })
if (cookies.get(metadata.COOKIE_NAME) === undefined) {
throw new Error('no-cookie')
}
} catch (err) {
throw new Error(err)
} finally {
commit('SET_PENDING', false)
}
},
async logout({ commit }) {
commit('SET_USER', null)
commit('SET_TOKEN', null)
return this.app.$apolloHelpers.onLogout()
},
}