@workos/authkit-sveltekit
Version:
Official WorkOS AuthKit SDK for SvelteKit
83 lines (79 loc) • 2.77 kB
TypeScript
import * as _sveltejs_kit from '@sveltejs/kit';
import { RequestEvent } from '@sveltejs/kit';
import * as _workos_authkit_session from '@workos/authkit-session';
import { User, Impersonator } from '@workos/authkit-session';
export * from '@workos/authkit-session';
/**
* Configuration options for AuthKit SvelteKit integration
*/
interface AuthKitConfig {
/** WorkOS Client ID */
clientId: string;
/** WorkOS API Key */
apiKey: string;
/** OAuth redirect URI */
redirectUri: string;
/** Cookie encryption password (min 32 chars) */
cookiePassword: string;
/** Optional: Custom cookie name (default: 'wos-session') */
cookieName?: string;
/** Optional: Cookie domain restriction */
cookieDomain?: string;
/** Optional: Cookie max age in seconds (default: 400 days) */
cookieMaxAge?: number;
}
/**
* Authentication state available in event.locals.auth
*/
interface AuthKitAuth {
user: User | null;
organizationId?: string | null;
role?: string | null;
permissions?: string[];
sessionId?: string;
impersonator?: Impersonator | null;
accessToken?: string;
}
/**
* Options for authKitHandle() function
*/
interface AuthKitHandleOptions {
/** Enable debug logging */
debug?: boolean;
/** Custom error handler */
onError?: (error: Error) => void;
/** Runtime configuration (overrides environment) */
config?: AuthKitConfig;
}
/**
* Options for sign-in/sign-up URL generation
*/
interface SignInOptions {
/** Path to redirect after authentication */
returnTo?: string;
/** Pre-select organization */
organizationId?: string;
/** Pre-fill email address */
loginHint?: string;
}
/**
* Handler function that requires authentication
*/
type AuthenticatedHandler<T = unknown> = (event: RequestEvent & {
auth: Required<AuthKitAuth>;
}) => T | Promise<T>;
declare function configureAuthKit(config: AuthKitConfig): void;
declare const authKit: {
withAuth: <T>(handler: AuthenticatedHandler<T>) => (event: RequestEvent) => Promise<T>;
getUser: (event: RequestEvent) => Promise<_workos_authkit_session.User | null>;
getSignInUrl: (options?: SignInOptions) => Promise<string>;
getSignUpUrl: (options?: SignInOptions) => Promise<string>;
signOut: (event: RequestEvent) => Promise<Response>;
switchOrganization: (event: RequestEvent, options: {
organizationId: string;
}) => Promise<never>;
handleCallback: () => ({ url }: RequestEvent) => Promise<Response>;
refreshSession: (event: RequestEvent) => Promise<boolean>;
};
declare const authKitHandle: (options?: AuthKitHandleOptions) => _sveltejs_kit.Handle;
export { type AuthKitAuth, type AuthKitConfig, authKit, authKitHandle, configureAuthKit };