Skip to content

Commit 93ea188

Browse files
committed
Fixed some typos
1 parent 36e63db commit 93ea188

File tree

8 files changed

+62
-32
lines changed

8 files changed

+62
-32
lines changed

app/(app)/settings/_client.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { saveSettingsSchema } from "@/schema/profile";
1313
import { uploadFile } from "@/utils/s3helpers";
1414
import type { user } from "@/server/db/schema";
1515
import { Button } from "@/components/ui-components/button";
16-
import { CheckCheck, Loader } from "lucide-react";
16+
import { CheckCheck, Loader2 } from "lucide-react";
1717

1818
function classNames(...classes: string[]) {
1919
return classes.filter(Boolean).join(" ");
@@ -49,7 +49,6 @@ const Settings = ({ profile }: { profile: User }) => {
4949
defaultValues: {
5050
...profile,
5151
username: profile.username || "",
52-
email: profile.email || "",
5352
},
5453
});
5554

@@ -140,22 +139,23 @@ const Settings = ({ profile }: { profile: User }) => {
140139

141140
const handleNewEmailUpdate = async () => {
142141
setLoading(true);
143-
try {
144-
await updateEmail({ newEmail });
145-
146-
toast.success("Verification link sent to your email.");
147-
setSendForVerification(true);
148-
} catch (error: any) {
149-
if (
150-
error.data.code === "BAD_REQUEST" ||
151-
error.data?.code === "INTERNAL_SERVER_ERROR"
152-
) {
153-
toast.error(error.data.message);
154-
} else {
155-
toast.error("Something went wrong");
156-
}
157-
}
158-
setLoading(false);
142+
await updateEmail(
143+
{ newEmail },
144+
{
145+
onError(error) {
146+
setLoading(false);
147+
if (error) return toast.error(error.message);
148+
return toast.error(
149+
"Something went wrong sending the verification link.",
150+
);
151+
},
152+
onSuccess() {
153+
setLoading(false);
154+
toast.success("Verification link sent to your email.");
155+
setSendForVerification(true);
156+
},
157+
},
158+
);
159159
};
160160

161161
return (
@@ -381,7 +381,7 @@ const Settings = ({ profile }: { profile: User }) => {
381381
<input
382382
type="email"
383383
id="currEmail"
384-
{...register("email")}
384+
value={profile.email!}
385385
disabled
386386
/>
387387
</div>
@@ -404,7 +404,7 @@ const Settings = ({ profile }: { profile: User }) => {
404404
onClick={handleNewEmailUpdate}
405405
>
406406
{loading && (
407-
<Loader className="text-primary h-4 w-4 animate-spin" />
407+
<Loader2 className="text-primary h-6 w-6 animate-spin" />
408408
)}
409409
Send verification link
410410
</Button>
@@ -415,12 +415,12 @@ const Settings = ({ profile }: { profile: User }) => {
415415
Verification link sent
416416
</h2>
417417
<Button
418-
className="w-[200px]"
418+
className="w-[250px]"
419419
disabled={!(newEmail || loading)}
420420
onClick={handleNewEmailUpdate}
421421
>
422422
{loading && (
423-
<Loader className="text-primary h-8 w-8 animate-spin" />
423+
<Loader2 className="text-primary h-6 w-6 animate-spin" />
424424
)}
425425
Resend verification link
426426
</Button>

drizzle/0007_added-emailtokenverification-schema.sql renamed to drizzle/0007_added_emailtokenverification_schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ CREATE TABLE IF NOT EXISTS "EmailVerificationToken" (
55
"expiresAt" timestamp NOT NULL,
66
"email" text NOT NULL,
77
"userId" text NOT NULL,
8-
CONSTRAINT "EmailVerificationToken_token_unique" UNIQUE("token")
8+
CONSTRAINT "EmailVerificationToken_token_unique" UNIQUE("token"),
9+
CONSTRAINT "EmailVerificationToken_email_unique" UNIQUE("email")
910
);
1011
--> statement-breakpoint
1112
DROP INDEX IF EXISTS "BannedUsers_userId_key";--> statement-breakpoint

drizzle/meta/0007_snapshot.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "450f331b-d62b-44e2-b764-2002398417d7",
2+
"id": "91f7060d-273a-41ac-9ab5-c1822239be6b",
33
"prevId": "73711620-aa4c-4f03-bfbe-08098e17e1ac",
44
"version": "7",
55
"dialect": "postgresql",
@@ -552,6 +552,13 @@
552552
"columns": [
553553
"token"
554554
]
555+
},
556+
"EmailVerificationToken_email_unique": {
557+
"name": "EmailVerificationToken_email_unique",
558+
"nullsNotDistinct": false,
559+
"columns": [
560+
"email"
561+
]
555562
}
556563
}
557564
},

drizzle/meta/_journal.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
{
5555
"idx": 7,
5656
"version": "7",
57-
"when": 1728229457525,
58-
"tag": "0007_added-emailtokenverification-schema",
57+
"when": 1728398381909,
58+
"tag": "0007_added_emailtokenverification_schema",
5959
"breakpoints": true
6060
}
6161
]

schema/profile.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const saveSettingsSchema = z.object({
1818
websiteUrl: z.string().url().optional().or(z.literal("")),
1919
emailNotifications: z.boolean().optional(),
2020
newsletter: z.boolean().optional(),
21-
email: z.string().email().optional().or(z.literal("")),
2221
});
2322

2423
export const getProfileSchema = z.object({

server/api/router/profile.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import { nanoid } from "nanoid";
1818
import { eq } from "drizzle-orm";
1919
import { emailTokenReqSchema } from "@/schema/token";
2020
import {
21+
checkIfEmailExists,
2122
generateEmailToken,
2223
sendVerificationEmail,
2324
storeTokenInDb,
2425
} from "@/utils/emailToken";
25-
import { TOKEN_EXPIRATION_TIME } from "@/config/contants";
26+
import { TOKEN_EXPIRATION_TIME } from "@/config/constants";
2627

2728
export const profileRouter = createTRPCRouter({
2829
edit: protectedProcedure
@@ -146,6 +147,15 @@ export const profileRouter = createTRPCRouter({
146147
});
147148
}
148149

150+
const ifEmailExists = await checkIfEmailExists(newEmail);
151+
152+
if (ifEmailExists) {
153+
throw new TRPCError({
154+
code: "BAD_REQUEST",
155+
message: "Email already exists",
156+
});
157+
}
158+
149159
const userId = ctx.session.user.id;
150160

151161
const token = generateEmailToken();
@@ -155,10 +165,10 @@ export const profileRouter = createTRPCRouter({
155165
await sendVerificationEmail(newEmail, token);
156166

157167
return { message: "Verification email sent" };
158-
} catch (error) {
168+
} catch (error: any) {
159169
throw new TRPCError({
160-
code: "INTERNAL_SERVER_ERROR",
161-
message: "Internal server error",
170+
code: error.code || "INTERNAL_SERVER_ERROR",
171+
message: error.message || "Internal server error",
162172
});
163173
}
164174
}),

server/db/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const emailVerificationToken = pgTable("EmailVerificationToken", {
118118
token: text("token").unique().notNull(),
119119
createdAt: timestamp("createdAt").defaultNow().notNull(),
120120
expiresAt: timestamp("expiresAt").notNull(),
121-
email: text("email").notNull(),
121+
email: text("email").notNull().unique(),
122122
userId: text("userId")
123123
.notNull()
124124
.references(() => user.id),

utils/emailToken.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,16 @@ export const deleteTokenFromDb = async (token: string) => {
144144
throw new Error("Failed to delete email verification token");
145145
}
146146
};
147+
148+
export const checkIfEmailExists = async (email: string) => {
149+
try {
150+
const existingUser = await db.query.user.findFirst({
151+
where: (users, { eq }) => eq(users.email, email),
152+
});
153+
154+
return !!existingUser;
155+
} catch (error) {
156+
console.error("Error checking if email exists:", error);
157+
throw new Error("Failed to check if email exists");
158+
}
159+
};

0 commit comments

Comments
 (0)