UNPKG

remix-auth-email-link

Version:

> This strategy is heavily based on **kcd** strategy present in the [v2 of Remix Auth](https://github.com/sergiodxa/remix-auth/blob/v2.6.0/docs/strategies/kcd.md). The major difference being we are using `crypto-js` instead of `crypto` so that it can be d

144 lines (143 loc) 4.83 kB
import type { SessionStorage } from '@remix-run/server-runtime'; import type { AuthenticateOptions, StrategyVerifyCallback } from 'remix-auth'; import { Strategy } from 'remix-auth'; export type SendEmailOptions<User> = { emailAddress: string; magicLink: string; user?: User | null; domainUrl: string; form: FormData; }; export type SendEmailFunction<User> = { (options: SendEmailOptions<User>): Promise<void>; }; /** * Validate the email address the user is trying to use to login. * This can be useful to ensure it's not a disposable email address. * @param emailAddress The email address to validate */ export type VerifyEmailFunction = { (email: string): Promise<void>; }; /** * The content of the magic link payload. Keys are minified so that the resulting link is as short as possible. */ export type MagicLinkPayload = { /** * Email address used to authenticate. */ e: string; /** * Form data received in the request. */ f?: Record<string, unknown>; /** * Creation date of the magic link, as an ISO string. This is used to check * the email link is still valid. */ c: number; }; /** * This interface declares what configuration the strategy needs from the * developer to correctly work. */ export type EmailLinkStrategyOptions<User> = { /** * The endpoint the user will go after clicking on the email link. * A whole URL is not required, the pathname is enough, the strategy will * detect the host of the request and use it to build the URL. * @default "/magic" */ callbackURL?: string; /** * A function to send the email. This function should receive the email * address of the user and the URL to redirect to and should return a Promise. * The value of the Promise will be ignored. */ sendEmail: SendEmailFunction<User>; /** * A function to validate the email address. This function should receive the * email address as a string and return a Promise. The value of the Promise * will be ignored, in case of error throw an error. * * By default it only test the email against the RegExp `/.+@.+/`. */ verifyEmailAddress?: VerifyEmailFunction; /** * A secret string used to encrypt and decrypt the token and magic link. */ secret: string; /** * The name of the form input used to get the email. * @default "email" */ emailField?: string; /** * The param name the strategy will use to read the token from the email link. * @default "token" */ magicLinkSearchParam?: string; /** * How long the magic link will be valid. Default to 30 minutes. * @default 1_800_000 */ linkExpirationTime?: number; /** * The key on the session to store any error message. * @default "auth:error" */ sessionErrorKey?: string; /** * The key on the session to store the magic link. * @default "auth:magiclink" */ sessionMagicLinkKey?: string; /** * Add an extra layer of protection and validate the magic link is valid. * @default false */ validateSessionMagicLink?: boolean; /** * The key on the session to store the email. * It's unset the same time the sessionMagicLinkKey is. * @default "auth:email" */ sessionEmailKey?: string; }; /** * This interface declares what the developer will receive from the strategy * to verify the user identity in their system. */ export type EmailLinkStrategyVerifyParams = { email: string; form: FormData; /** * True, if the verify callback is called after clicking on the magic link */ magicLinkVerify: boolean; }; export declare class EmailLinkStrategy<User> extends Strategy<User, EmailLinkStrategyVerifyParams> { name: string; private readonly emailField; private readonly callbackURL; private readonly sendEmail; private readonly validateEmail; private readonly secret; private readonly magicLinkSearchParam; private readonly linkExpirationTime; private readonly sessionErrorKey; private readonly sessionMagicLinkKey; private readonly validateSessionMagicLink; private readonly sessionEmailKey; constructor(options: EmailLinkStrategyOptions<User>, verify: StrategyVerifyCallback<User, EmailLinkStrategyVerifyParams>); authenticate(request: Request, sessionStorage: SessionStorage, options: AuthenticateOptions): Promise<User>; getMagicLink(emailAddress: string, domainUrl: string, form: FormData): Promise<string>; private getDomainURL; private sendToken; private createFormPayload; private createMagicLinkPayload; private encrypt; private decrypt; private getMagicLinkCode; private validateMagicLink; }