import { NextApiRequest , NextApiResponse } from "next" ; import { ok , unexpectedError , AuthRouteHandlerOptions } from "." ; import jwt from "jsonwebtoken" ; export function createResendConfirmationEmail ( options ) { if ( ! options . serverConfig . tokenSecret ) throw new Error ( "HappyAuth: Missing token secret" ) ; return async function resendConfirmationEmail ( req , res ) { const { email } = req . body ; if ( typeof email !== "string" ) { res . status ( 200 ) . json ( { error : { code : "invalid email" , message : "Invalid email." , } , } ) ; return ; } if ( email . trim ( ) === "" ) { res . status ( 200 ) . json ( { error : { code : "missing email" , message : "Email must be provided." , } , } ) ; return ; } try { const userId = await options . serverConfig . driver . getUserIdByEmail ( email . trim ( ) . toLowerCase ( ) ) ; if ( ! userId ) { ok ( res ) ; return ; } const confirmJwt = jwt . sign ( { userId } , options . serverConfig . tokenSecret , { expiresIn : "1h" , } ) ; const link = `${ options . publicConfig . baseUrl }/confirm-account#token=${ confirmJwt }` ; await options . serverConfig . triggers . sendConfirmAccountMail ( email , link ) ; ok ( res ) ; } catch ( error ) { unexpectedError ( res , error ) ; } } ; }