UNPKG

@light-auth/nuxt

Version:

light auth framework for nuxt, using arctic

159 lines (155 loc) 6.26 kB
/*! @light-auth/nuxt v0.3.1 2025-06-13 */ 'use strict'; import { buildFullUrl, internalFetch } from '@light-auth/core'; import { resolveBasePath, createSignoutClientFunction, createSigninClientFunction } from '@light-auth/core/client'; import { useAsyncData, useRequestEvent, useRequestHeaders, useRequestURL } from '#imports'; /** * createNuxtJsSignIn is a function that creates a sign-in function for Nuxt.js. * It takes the LightAuth createSigninFunction base function and returns a user friendly function by * removing the req and res parameters, that are not needed in the Nuxt.js context. */ const createSignIn = (config) => { const signIn = createSigninClientFunction(config); return async (providerName, callbackUrl = "/") => { await signIn({ providerName, callbackUrl }); }; }; /** * createNuxtJsSignOut is a function that creates a sign-out function for Nuxt.js. * It takes the LightAuth createSignoutFunction base function and returns a user friendly function by * removing the req and res parameters, that are not needed in the Nuxt.js context. */ const createSignOut = (config) => { const signOut = createSignoutClientFunction(config); return async (revokeToken, callbackUrl = "/") => { await signOut({ revokeToken, callbackUrl }); }; }; function createCustomFetch(key, fetcher) { const data = useAsyncData(key, fetcher, { server: true, lazy: false, default: () => null, // @ts-ignore transform: (data) => data ?? false, // Ensure we return false instead of null to avoid hydration issues // avoid WARN [nuxt] useAsyncData must return a value (it should not be undefined) or the request may be duplicated on the client side. }); // Map AsyncData to FetchResult<T> return data; } const createGetAuthSession = (config) => { const sessionFunction = async () => { try { const event = useRequestEvent(); const headers = new Headers(useRequestHeaders()); const url = useRequestURL(); let urlEndpoint = `${config.basePath}/session`; if (url) { const incomingHeaders = new Headers(); incomingHeaders.set("host", url.host); urlEndpoint = buildFullUrl({ url: urlEndpoint, incomingHeaders }); } const session = await internalFetch({ config, method: "POST", endpoint: urlEndpoint, event, headers, }); return session ?? null; } catch (error) { console.error("Error fetching session:", error); throw error; } }; return () => createCustomFetch("light-auth-session", sessionFunction); }; const createSetAuthSession = (config) => { return async (session) => { try { const event = useRequestEvent(); const headers = new Headers(useRequestHeaders()); const url = useRequestURL(); let urlEndpoint = `${config.basePath}/set_session`; if (url) { const incomingHeaders = new Headers(); incomingHeaders.set("host", url.host); urlEndpoint = buildFullUrl({ url: urlEndpoint, incomingHeaders }); } const updatedSession = await internalFetch({ config, method: "POST", endpoint: urlEndpoint, body: JSON.stringify(session), event, headers, }); return updatedSession ?? null; } catch (error) { console.error("Error fetching session:", error); throw error; } }; }; const createSetUser = (config) => { return async (user) => { try { const event = useRequestEvent(); const headers = new Headers(useRequestHeaders()); const url = useRequestURL(); let urlEndpoint = `${config.basePath}/set_user`; if (url) { const incomingHeaders = new Headers(); incomingHeaders.set("host", url.host); urlEndpoint = buildFullUrl({ url: urlEndpoint, incomingHeaders }); } const updatedUser = await internalFetch({ config, method: "POST", endpoint: urlEndpoint, body: JSON.stringify(user), event, headers, }); return updatedUser ?? null; } catch (error) { console.error("Error fetching session:", error); throw error; } }; }; const createGetUser = (config) => { const userFunction = async (providerUserId) => { const event = useRequestEvent(); const headers = new Headers(useRequestHeaders()); const url = useRequestURL(); let userUrlEndpoint = providerUserId ? `${config.basePath}/user/${providerUserId}` : `${config.basePath}/user`; if (url) { const incomingHeaders = new Headers(); incomingHeaders.set("host", url.host); userUrlEndpoint = buildFullUrl({ url: userUrlEndpoint, incomingHeaders }); } // get the user from the user adapter const user = await internalFetch({ config, method: "POST", endpoint: userUrlEndpoint, headers, event }); return user ?? null; }; return (providerUserId) => createCustomFetch(`light-auth-user-${providerUserId ?? "from-session"}`, () => userFunction(providerUserId)); }; function CreateLightAuthClient(config = {}) { config.env = config.env || process.env; config.basePath = resolveBasePath(config.basePath, config.env); return { basePath: config.basePath, useSession: createGetAuthSession(config), setSession: createSetAuthSession(config), setUser: createSetUser(config), useUser: createGetUser(config), signIn: createSignIn(config), signOut: createSignOut(config), }; } export { CreateLightAuthClient, createCustomFetch, createGetAuthSession, createSetAuthSession, createSetUser }; //# sourceMappingURL=index.mjs.map