2024-06-29 05:22:22 +00:00
"use server" ;
2024-06-29 06:37:50 +00:00
import { daphnis } from "@/lib/prisma" ;
2024-06-29 05:22:22 +00:00
import { randomUUID } from "crypto" ;
import { Resend } from "resend" ;
import { redirect } from "next/navigation" ;
const resend = new Resend ( process . env . NEXT_PUBLIC_RESEND_API_KEY ) ;
const DOMAIN = process . env . DOMAIN || "localhost:3000" ;
const PROTOCOL = process . env . NODE_ENV === "production" ? "https" : "http" ;
const sendEmail = async ( email : string , token : string , userName : string ) = > {
try {
const { data } = await resend . emails . send ( {
from : "Password Reset <security@resend.dev>" ,
to : [ email ] ,
subject : "Reset Password Request" ,
text : ` Hello ${ userName } , someone (hopefully you) requested a password reset for this account. If you did want to reset your password, please click here: ${ PROTOCOL } :// ${ DOMAIN } /password-reset/ ${ token }
For security reasons , this link is only valid for four hours .
If you did not request this reset , please ignore this email . ` ,
} ) ;
console . log ( "Email sent successfully:" , data ) ;
// Handle success
} catch ( error ) {
console . error ( "Error sending email:" , error ) ;
// Handle error
throw error ;
}
} ;
export async function EmailPasswordResetLink ( data : FormData ) {
const email = data . get ( "email" ) ;
if ( ! email || typeof email !== "string" ) {
return {
error : "Invalid email" ,
} ;
}
2024-06-29 06:37:50 +00:00
const user = await daphnis . user . findUnique ( {
2024-06-29 05:22:22 +00:00
where : { email } ,
} ) ;
if ( ! user ) {
return {
error : "This email is not registered" ,
} ;
}
2024-06-29 06:37:50 +00:00
const token = await daphnis . passwordResetToken . create ( {
2024-06-29 05:22:22 +00:00
data : {
id : randomUUID ( ) ,
userId : user.id ,
token : ` ${ randomUUID ( ) } ${ randomUUID ( ) } ` . replace ( /-/g , "" ) ,
} ,
} ) ;
await sendEmail ( user . email , token . token , user . username ) ;
redirect ( "/forgot-password/success" ) ;
}