Skip to content

Instantly share code, notes, and snippets.

View kargozeyan's full-sized avatar
🦈
Focusing

Karen Gozeyan kargozeyan

🦈
Focusing
View GitHub Profile
@kargozeyan
kargozeyan / nuxt-auth-api-plugin.ts
Created April 5, 2026 10:03
Nuxt plugin for handling access/refresh token cookies with external backend (SSR-compatible)
// app/plugins/api.ts
import { appendResponseHeader } from 'h3';
import { parseCookie, parseSetCookie, stringifyCookie } from 'cookie';
export default defineNuxtPlugin(() => {
const event = import.meta.server ? useRequestEvent() : undefined;
// During SSR, we parse the initial browser cookies. During the request lifecycle,
// we update this object with new cookies from backend responses (e.g. after a token refresh).
const cookies = import.meta.server && event ? parseCookie(event.node.req.headers.cookie ?? '') : {};