UNPKG

@getmocha/users-service

Version:

An API client, Hono middleware, and a React provider for the Mocha Users Service

71 lines (70 loc) 3.2 kB
import type { MochaUser } from '@getmocha/users-service/shared'; export interface MochaUsersServiceOptions { apiUrl?: string; apiKey: string; } export declare const DEFAULT_MOCHA_USERS_SERVICE_API_URL: "https://getmocha.com/u"; export declare const MOCHA_SESSION_TOKEN_COOKIE_NAME: "mocha_session_token"; export declare const SUPPORTED_OAUTH_PROVIDERS: readonly ["google"]; export type OAuthProvider = (typeof SUPPORTED_OAUTH_PROVIDERS)[number]; declare module 'hono' { interface ContextVariableMap { user?: MochaUser; } } /** * Fetch the OAuth redirect URL from the Mocha Users Service. * @param provider - The OAuth provider to use (currently only "google" is supported) * @param options - Configuration options including API key and optional API URL * @returns The redirect URL to initiate the OAuth flow */ export declare function getOAuthRedirectUrl(provider: OAuthProvider, options: MochaUsersServiceOptions): Promise<string>; /** * Exchanges a code for a session token using the Mocha Users Service. * @param code - The OAuth code received after successful authentication * @param options - Configuration options including API key and optional API URL * @returns The session token to use for authenticated requests */ export declare function exchangeCodeForSessionToken(code: string, options: MochaUsersServiceOptions): Promise<string>; /** * Fetch the current user by their session token from the Mocha Users Service. * @param sessionToken - The session token obtained from exchangeCodeForSessionToken * @param options - Configuration options including API key and optional API URL * @returns The user object or null if the session is invalid */ export declare function getCurrentUser(sessionToken: string, options: MochaUsersServiceOptions): Promise<MochaUser | null>; /** * Delete the current session in the Mocha Users Service when logging out. * @param sessionToken - The users session token from their cookie. * @param options - Configuration options including API key and optional API URL */ export declare function deleteSession(sessionToken: string, options: MochaUsersServiceOptions): Promise<void>; /** * Hono middleware that authenticates requests against the Mocha Users Service. * * This middleware requests the current user using the session token stored in * cookies. If the request fails to return a valid user object, the middleware * throws an HTTPException with status 401. On success, it sets the authenticated * user in the Hono context for use in subsequent route handlers. * * Use this to protect routes and load the current user. * * @throws {HTTPException} 401 - When session token is invalid or not provided * * @example * * // Fetch the authenticated user's todos. * // Doesn't execute if the user is not authenticated. * app.get("/api/todos", authMiddleware, async (c) => { * const user = c.get("user"); * * const { results } = await c.env.DB.prepare( * "SELECT * FROM todos WHERE user_id = ? ORDER BY created_at DESC" * ) * .bind(user.id) * .all(); * * return c.json(results); * }); */ export declare const authMiddleware: import("hono").MiddlewareHandler<any, string, {}>;