@auth/sveltekit
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -1,6 +0,8 @@ | ||
import { type AuthOptions } from "@auth/core"; | ||
import { type AuthOptions, type Session } from "@auth/core"; | ||
import type { Handle } from "@sveltejs/kit"; | ||
export declare function getServerSession(req: Request, options: AuthOptions): Promise<unknown>; | ||
interface SvelteKitAuthOptions extends AuthOptions { | ||
export type GetSessionResult = Promise<Session | null>; | ||
export declare function getSession(req: Request, options: AuthOptions): GetSessionResult; | ||
export interface SvelteKitAuthOptions extends AuthOptions { | ||
/** | ||
* Defines the base path for the auth routes. | ||
* @default '/auth' | ||
@@ -10,4 +12,21 @@ */ | ||
} | ||
/** The main entry point to @auth/sveltekit */ | ||
declare function SvelteKitAuth({ prefix, ...options }: SvelteKitAuthOptions): Handle; | ||
export default SvelteKitAuth; | ||
/** | ||
* The main entry point to `@auth/sveltekit` | ||
* @see https://sveltekit.authjs.dev | ||
*/ | ||
export default function SvelteKitAuth(options: SvelteKitAuthOptions): Handle; | ||
declare global { | ||
namespace App { | ||
interface Locals { | ||
getSession: () => GetSessionResult; | ||
} | ||
interface PageData { | ||
session: Session | null; | ||
} | ||
} | ||
} | ||
declare module "$env/static/private" { | ||
const AUTH_SECRET: string; | ||
const AUTH_TRUST_HOST: string; | ||
const VERCEL: string; | ||
} |
40
index.js
@@ -0,5 +1,6 @@ | ||
/// <reference types="@sveltejs/kit" /> | ||
import { dev } from "$app/environment"; | ||
import { AUTH_SECRET, AUTH_TRUST_HOST, VERCEL } from "$env/static/private"; | ||
import { dev } from "$app/environment"; | ||
import { AuthHandler } from "@auth/core"; | ||
export async function getServerSession(req, options) { | ||
import { AuthHandler, } from "@auth/core"; | ||
export async function getSession(req, options) { | ||
options.secret ??= AUTH_SECRET; | ||
@@ -13,5 +14,4 @@ options.trustHost ??= true; | ||
return null; | ||
if (status === 200) { | ||
if (status === 200) | ||
return data; | ||
} | ||
throw new Error(data.message); | ||
@@ -30,17 +30,23 @@ } | ||
]; | ||
/** The main entry point to @auth/sveltekit */ | ||
function SvelteKitAuth({ prefix = "/auth", ...options }) { | ||
options.secret ??= AUTH_SECRET; | ||
options.trustHost ??= !!(AUTH_TRUST_HOST ?? VERCEL ?? dev); | ||
function SvelteKitAuthHandler(prefix, authOptions) { | ||
return ({ event, resolve }) => { | ||
const [action] = event.url.pathname.slice(prefix.length + 1).split("/"); | ||
const isAuth = actions.includes(action); | ||
if (!event.locals.getSession) | ||
event.locals.getSession = async () => getServerSession(event.request, options); | ||
if (!event.url.pathname.startsWith(prefix + "/") || !isAuth) { | ||
return resolve(event); | ||
const { url, request } = event; | ||
event.locals.getSession ??= () => getSession(request, authOptions); | ||
const [action] = url.pathname.slice(prefix.length + 1).split("/"); | ||
if (actions.includes(action) && | ||
url.pathname.startsWith(prefix + "/")) { | ||
return AuthHandler(request, authOptions); | ||
} | ||
return AuthHandler(event.request, options); | ||
return resolve(event); | ||
}; | ||
} | ||
export default SvelteKitAuth; | ||
/** | ||
* The main entry point to `@auth/sveltekit` | ||
* @see https://sveltekit.authjs.dev | ||
*/ | ||
export default function SvelteKitAuth(options) { | ||
const { prefix = "/auth", ...authOptions } = options; | ||
authOptions.secret ??= AUTH_SECRET; | ||
authOptions.trustHost ??= !!(AUTH_TRUST_HOST ?? VERCEL ?? dev); | ||
return SvelteKitAuthHandler(prefix, authOptions); | ||
} |
{ | ||
"name": "@auth/sveltekit", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Authentication for SvelteKit.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://sveltekit.authjs.dev", |
@@ -0,11 +1,18 @@ | ||
/// <reference types="@sveltejs/kit" /> | ||
import { dev } from "$app/environment" | ||
import { AUTH_SECRET, AUTH_TRUST_HOST, VERCEL } from "$env/static/private" | ||
import { dev } from "$app/environment" | ||
import { AuthHandler, type AuthOptions, type AuthAction } from "@auth/core" | ||
import { | ||
AuthHandler, | ||
type AuthAction, | ||
type AuthOptions, | ||
type Session, | ||
} from "@auth/core" | ||
import type { Handle } from "@sveltejs/kit" | ||
export async function getServerSession( | ||
export type GetSessionResult = Promise<Session | null> | ||
export async function getSession( | ||
req: Request, | ||
options: AuthOptions | ||
): Promise<unknown> { | ||
): GetSessionResult { | ||
options.secret ??= AUTH_SECRET | ||
@@ -25,11 +32,9 @@ options.trustHost ??= true | ||
if (!data || !Object.keys(data).length) return null | ||
if (status === 200) { | ||
return data | ||
} | ||
if (status === 200) return data | ||
throw new Error(data.message) | ||
} | ||
interface SvelteKitAuthOptions extends AuthOptions { | ||
export interface SvelteKitAuthOptions extends AuthOptions { | ||
/** | ||
* Defines the base path for the auth routes. | ||
* @default '/auth' | ||
@@ -52,26 +57,51 @@ */ | ||
/** The main entry point to @auth/sveltekit */ | ||
function SvelteKitAuth({ | ||
prefix = "/auth", | ||
...options | ||
}: SvelteKitAuthOptions): Handle { | ||
options.secret ??= AUTH_SECRET | ||
options.trustHost ??= !!(AUTH_TRUST_HOST ?? VERCEL ?? dev) | ||
function SvelteKitAuthHandler( | ||
prefix: string, | ||
authOptions: AuthOptions | ||
): Handle { | ||
return ({ event, resolve }) => { | ||
const [action] = event.url.pathname.slice(prefix.length + 1).split("/") | ||
const isAuth = actions.includes(action as AuthAction) | ||
const { url, request } = event | ||
if (!event.locals.getSession) | ||
event.locals.getSession = async () => | ||
getServerSession(event.request, options) | ||
event.locals.getSession ??= () => getSession(request, authOptions) | ||
if (!event.url.pathname.startsWith(prefix + "/") || !isAuth) { | ||
return resolve(event) | ||
const [action] = url.pathname.slice(prefix.length + 1).split("/") | ||
if ( | ||
actions.includes(action as AuthAction) && | ||
url.pathname.startsWith(prefix + "/") | ||
) { | ||
return AuthHandler(request, authOptions) | ||
} | ||
return AuthHandler(event.request, options) | ||
return resolve(event) | ||
} | ||
} | ||
export default SvelteKitAuth | ||
/** | ||
* The main entry point to `@auth/sveltekit` | ||
* @see https://sveltekit.authjs.dev | ||
*/ | ||
export default function SvelteKitAuth(options: SvelteKitAuthOptions): Handle { | ||
const { prefix = "/auth", ...authOptions } = options | ||
authOptions.secret ??= AUTH_SECRET | ||
authOptions.trustHost ??= !!(AUTH_TRUST_HOST ?? VERCEL ?? dev) | ||
return SvelteKitAuthHandler(prefix, authOptions) | ||
} | ||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace App { | ||
interface Locals { | ||
getSession: () => GetSessionResult | ||
} | ||
interface PageData { | ||
session: Session | null | ||
} | ||
} | ||
} | ||
declare module "$env/static/private" { | ||
export const AUTH_SECRET: string | ||
export const AUTH_TRUST_HOST: string | ||
export const VERCEL: string | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15251
358
9