Skip to content

Commit

Permalink
Add email checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoSette committed Dec 4, 2024
1 parent 87cfe82 commit a80fd4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/(login)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ const signUpSchema = z.object({

export const signUp = validatedAction(signUpSchema, async (data, formData) => {
const { email, password, inviteId } = data;
const lowerCaseEmail = email.toLowerCase(); // Convert email to lowercase
const lowerCaseEmail = email.toLowerCase();
const emailOptIn = formData.get('emailOptIn') === 'on'; // Get checkbox value

const existingUser = await db
.select()
.from(users)
.where(eq(users.email, lowerCaseEmail)) // Use lowercase email
.where(eq(users.email, lowerCaseEmail))
.limit(1);

if (existingUser.length > 0) {
Expand All @@ -132,22 +133,21 @@ export const signUp = validatedAction(signUpSchema, async (data, formData) => {
const passwordHash = await hashPassword(password);

const newUser: NewUser = {
email: lowerCaseEmail, // Store email in lowercase
email: lowerCaseEmail,
passwordHash,
role: "owner", // Default role, will be overridden if there's an invitation
role: "owner",
};

const [createdUser] = await db.insert(users).values(newUser).returning();

// Create Resend contact after successful sign-up
if (createdUser) {
// Only create Resend contact if emailOptIn is true
if (createdUser && emailOptIn) {
await resend.contacts.create({
email: lowerCaseEmail,
unsubscribed: false,
audienceId: "7c43c8a9-1840-4029-b11f-496cd7e75a6e",
});

// Directly call sendWelcomeEmail instead of using fetch
await sendWelcomeEmail(lowerCaseEmail);
}

Expand Down
11 changes: 11 additions & 0 deletions app/(login)/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Label } from "@/components/ui/label";
import { CircleIcon, Loader2 } from "lucide-react";
import { signIn, signUp, forgotPassword, resetPassword } from "./actions";
import { ActionState } from "@/lib/auth/middleware";
import { Checkbox } from "@/components/ui/checkbox";

const actionMap = {
signin: signIn,
Expand Down Expand Up @@ -148,6 +149,16 @@ export function Login({
</div>
)}

{mode === "signup" && (
<div className="flex items-center space-x-2">
<Checkbox id="emailOptIn" name="emailOptIn" />
<Label htmlFor="emailOptIn" className="text-sm text-gray-600">
I want to sign up to receive updates, exclusive offers, and
helpful resources straight to my inbox.
</Label>
</div>
)}

{state?.error && (
<div className="text-red-500 text-sm">{state.error}</div>
)}
Expand Down
Binary file removed bun.lockb
Binary file not shown.

0 comments on commit a80fd4f

Please sign in to comment.