UNPKG

@happykit/auth-email

Version:

- A `useAuth` hook which returns the current user - An optional `getServerSideAuth` for server-side rendering - HappyAuth is tiny - it adds only 4.6 kB to the first load JS - it adds less than 0.04 kB if you're transitioning from another page - Extremely

138 lines (137 loc) 5.59 kB
/// <reference types="node" /> import { NextApiResponse, NextApiRequest } from "next"; import { BaseTokenData, PublicConfig, AccountStatus, BaseAuthState } from ".."; import { IncomingMessage, ServerResponse } from "http"; import { FetchAdditionalTokenContent } from "./login"; import { sendConfirmAccountMailToConsole, SendConfirmAccountMail } from "./signup"; import { sendForgotPasswordMailToConsole, SendForgotPasswordMail } from "./forgot-password"; import { Token, ModuleOptions } from "simple-oauth2"; export { createFaunaEmailDriver } from "../drivers/fauna"; export { SendForgotPasswordMail, SendConfirmAccountMail, sendConfirmAccountMailToConsole, sendForgotPasswordMailToConsole, }; export declare type ServerConfig = { /** * Used to sign different tokens (auth, confirm account, reset password, change password, OAuth). */ tokenSecret: string; /** * Name under which your auth cookie will be stored */ cookieName: string; /** * Whether to set the auth cookie to "secure" or not. * * If the cookie is set to "secure", it will only be sent over HTTPS. It's * recommended to set this to true in production, and to false in development. * * Note that the cookie is always set to httpOnly. * * Example: { secure: process.env.NODE_ENV === 'production' } */ secure: boolean; /** * A list of identity provider configurations for OAuth logins and signups. * * This is the server part of the configuration. You'll also need to configure `publicConfig.identityProviders`. */ identityProviders: IdentityProviderConfig; triggers: Triggers; driver: Driver; }; export declare type GetServerSideAuth = ReturnType<typeof createGetServerSideAuth>; /** * Returns preconfigured getServerSideAuth function. * * Use that getServerSideAuth function in your application. */ export declare function createGetServerSideAuth<T extends BaseTokenData>(serverConfig: ServerConfig): (req: IncomingMessage) => BaseAuthState<T>; /** * OAuth providers. */ export declare type IdentityProviderConfig = { [idp: string]: { credentials: ModuleOptions<"client_id">; /** * The scopes to request */ scope?: string; /** * A custom function which upserts the user into your database. * * This function will get called with the OAuth token when a user signs up * using OAuth and every time they sign in using OAuth. Use this function to * map attributes from your OAuth provider to your database. * * Return the userId from your function. */ upsertUser: (token: Token) => Promise<string>; }; }; export declare type Triggers = { /** * Provide a function which sends the confirmation email to your user. */ sendConfirmAccountMail: SendConfirmAccountMail; /** * Provide a function which sends the forgot-password email to your user. */ sendForgotPasswordMail: SendForgotPasswordMail; /** * Put additional content into user tokens. * * When a user signs up or logs in, you can store anything you like in the * users token. Simply return the additional information from this function. * * Remember not to include private information here, as the users token is a * JSON Web Token, so the data is not encrypted. */ fetchAdditionalTokenContent?: FetchAdditionalTokenContent; }; /** * Handles all communication with the database. * * The concept of drivers is what makes HappyAuth database agnostic. You can * use whichever database you want by passing a custom driver. */ export declare type Driver = { attemptEmailPasswordLogin: (email: string, password: string) => Promise<{ success: true; data: { userId: string; accountStatus: AccountStatus; }; } | { success: false; reason: "authentication failed"; }>; createEmailUser: (email: string, password: string) => Promise<{ success: true; data: { userId: string; }; } | { success: false; reason: "instance not unique"; }>; updateEmailUserPassword: (userId: string, password: string) => Promise<void>; changeEmailUserPassword: (userId: string, currentPassword: string, newPassword: string) => Promise<void>; getUserIdByEmail: (email: string) => Promise<string | null>; confirmAccount: (userId: string) => Promise<boolean>; }; export declare type AuthRouteHandlerOptions = { publicConfig: PublicConfig; serverConfig: ServerConfig; getServerSideAuth: GetServerSideAuth; }; export declare function createAuthRouteHandler<T extends BaseTokenData>(options: AuthRouteHandlerOptions): (req: NextApiRequest, res: NextApiResponse) => void | Promise<void>; export declare function serializeAuthCookie<T extends BaseTokenData>(serverConfig: ServerConfig, data: T, options?: { rememberMe: boolean; }): string[]; export declare function redirect(res: ServerResponse, location?: string): { props: {}; }; export declare function unauthorized(res: NextApiResponse, payload?: object): void; export declare function noContent(res: NextApiResponse): void; export declare function authenticationFailed(res: NextApiResponse, payload?: object): void; export declare function unexpectedError(res: NextApiResponse, error?: Error): void; export declare function jwtExpired(res: NextApiResponse, payload?: object): void; export declare function ok(res: NextApiResponse): void;