Spaces:
				
			
			
	
			
			
		Build error
		
	
	
	
			
			
	
	
	
	
		
		
		Build error
		
	File size: 1,921 Bytes
			
			| 7e5cb25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | "use server"
import { sendEmail } from "@/utils/email"
import db from "@/utils/db"
import jwt from "jsonwebtoken"
export async function submitPrompt(prevState, formData) {
  try {
    // await createItem(formData.get('todo'))
    const prompt = formData.get("prompt")
    const email = formData.get("email")
    const recaptchaValue = formData.get("g-recaptcha-response")
    console.log({ prompt, email, recaptchaValue })
    return revalidatePath("/")
  } catch (e) {
    return { message: "Failed to create" }
  }
}
export async function submitLogin(prevState, formData) {
  const email = formData.get("email")
  const recaptcha = formData.get("g-recaptcha-response")
  // check captcha
  console.log({ email, recaptcha })
  const captchaResponse = await fetch(
    `https://www.google.com/recaptcha/api/siteverify`,
    {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: `secret=${process.env.RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
    }
  )
  const captchaData = await captchaResponse.json()
  console.log({ captchaData })
  if (!captchaData.success) {
    console.log("failed captcha")
    return { error: "Failed to verify captcha" }
  }
  console.log("passed captcha")
  // upsert
  const [userObj] =
    await db`INSERT INTO users (email) VALUES (${email}) ON CONFLICT (email) DO UPDATE SET email = ${email} RETURNING *`
  const token = jwt.sign(
    {
      userId: userObj.id,
      exp: Math.floor(Date.now() / 1000) + 60 * 60, // 1 hour
    },
    process.env.JWT_SECRET
  )
  await sendEmail({
    subject: `please confirm your email`,
    to: [email],
    from: process.env.EMAIL_FROM,
    text: `Hi,
For anti-spam measures please confirm your email before upvoting or submitting:
${process.env.NEXT_PUBLIC_SITE_URL}/api/confirm?token=${token}`,
  })
  console.log("sent email")
  return { message: "Check your mailbox" }
}
 | 
