Skip to content

Instantly share code, notes, and snippets.

@msonowal
Forked from wobsoriano/auth.js
Created March 15, 2022 18:02
Show Gist options
  • Save msonowal/f263fc8fdb6c83172a894ccdf3cc131f to your computer and use it in GitHub Desktop.
Save msonowal/f263fc8fdb6c83172a894ccdf3cc131f to your computer and use it in GitHub Desktop.
nuxtServerInit like implementation for Pinia
import { defineStore } from 'pinia'
export const useAuthStore = defineStore({
id: 'auth',
state: () => ({
isAuthenticated: false,
user: null
}),
actions: {
async nuxtServerInit() {
const user = await this.$axios.get('/api/user')
this.$patch({
isAuthenticated: true,
user
})
}
}
})
import { useAuthStore } from '~/store/auth'
export default async function ({ $pinia }) {
if (process.server) {
const store = useAuthStore($pinia)
await store.nuxtServerInit()
}
}
// Add this file to nuxt.config.js
// { router: { middleware: ['nuxt-server-init'] } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment