import { randomBytes } from "crypto" ; import { InternalOptions } from "../../../lib/types" ; import { hashToken } from "../utils" ; /**
 * Starts an e-mail login flow, by generating a token,
 * and sending it to the user's e-mail (with the help of a DB adapter)
 */ export default async function email ( identifier , options ) { const { url , adapter , provider , logger , callbackUrl } = options ; const token = ( await provider . generateVerificationToken ?. ( ) ) ?? randomBytes ( 32 ) . toString ( "hex" ) ; const ONE_DAY_IN_SECONDS = 86400 ; const expires = new Date ( Date . now ( ) + ( provider . maxAge ?? ONE_DAY_IN_SECONDS ) * 1000 ) ; await adapter . createVerificationToken ( { identifier , token : hashToken ( token , options ) , expires , } ) ; const params = new URLSearchParams ( { callbackUrl , token , email : identifier } ) ; const _url = `${ url }/callback/${ provider . id }?${ params }` ; try { await provider . sendVerificationRequest ( { identifier , token , expires , url : _url , provider , } ) ; } catch ( error ) { logger . error ( "SEND_VERIFICATION_EMAIL_ERROR" , { identifier , url , error : error as Error , } ) ; throw new Error ( "SEND_VERIFICATION_EMAIL_ERROR" ) ; } }