Session cookie authentication

Hello there, I'm trying to setup authentication for a nuxt3 app and community modules i analized dont satisfy my requirements. I just need a way to propagate the session cookie on client e server side to determine user authentication and use it with @melody/apollo. Is this a valuable solution for my use case?
export default defineNuxtPlugin(async (nuxtApp) => {
const customerStore = useCustomer()
const cookie = useCookie('vendure_session')
const { session, customer } = storeToRefs(customerStore)
/**
* - Set session token both on server and client sides
* - Fetch customer data verifying token validity
* - Reset auth token if not valid
*/
nuxtApp.hook('app:created', async () => {
console.log('AUTH - [app:created]')
if (cookie.value) {
customerStore.setSession(cookie.value)
if (!customer.value) {
if (!(await customerStore.fetchCustomer())) {
customerStore.setSession(null)
}
}
}
})
/**
* Inject session token to Apollo default client
*/
nuxtApp.hook('apollo:auth', async ({ token }) => {
console.log('AUTH - [apollo:auth]')
if (cookie.value) {
token.value = session.value
}
})
})
export default defineNuxtPlugin(async (nuxtApp) => {
const customerStore = useCustomer()
const cookie = useCookie('vendure_session')
const { session, customer } = storeToRefs(customerStore)
/**
* - Set session token both on server and client sides
* - Fetch customer data verifying token validity
* - Reset auth token if not valid
*/
nuxtApp.hook('app:created', async () => {
console.log('AUTH - [app:created]')
if (cookie.value) {
customerStore.setSession(cookie.value)
if (!customer.value) {
if (!(await customerStore.fetchCustomer())) {
customerStore.setSession(null)
}
}
}
})
/**
* Inject session token to Apollo default client
*/
nuxtApp.hook('apollo:auth', async ({ token }) => {
console.log('AUTH - [apollo:auth]')
if (cookie.value) {
token.value = session.value
}
})
})
Could you please give me an idea of when you are planning to release the official nuxt auth module? I also have another project using nuxt2 with the nuxt-auth module and it also has SSR cookies-sync problem, is app:created hook a good choise to sync auth cookies on both server and client sides also on nuxt2? Thanks for your help!
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server