Skip to content

Checking for another store value (auth) in each getter, can this be extracted? #812

Discussion options

You must be logged in to vote

You could use a function, it can be outside the store definition, either in the same file or in another file if you want to use this check across multiple stores (or even in components):

function userMissing (): boolean {
  const authStore = useAuth()
  const authUser = authStore.user
  return authUser === undefined
}

export const useWhateverStore = defineStore(/* ... */)

And then in the getters/actions:

actions: {
  async fetchUserPhotos(amount = 6): Promise<void> {
    if (userMissing()) {
      throw new Error('User is not signed in')
    }
    // ...
  }

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@notflip
Comment options

Answer selected by notflip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants