Skip to content

Commit c916f70

Browse files
committed
fix(db): don't query database if user isn't logged in
1 parent f9a8d9c commit c916f70

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

src/pages/api/me.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as schema from "$drizzle/schema";
22
import { withDb } from "@/db";
3+
import { getCookie } from "cookies-next";
34
import { eq } from "drizzle-orm";
45
import type { NextApiRequest, NextApiResponse } from "next";
56
import { getUID } from "./saves";
@@ -9,6 +10,14 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
910
const uid = await getUID(req, res, db);
1011
if (!uid) return res.status(401).end();
1112

13+
// getUID already verified the token. Re-use the DB query it made
14+
// by checking if there's a token — if so, the user record exists.
15+
const token = getCookie("token", { req, res });
16+
if (!token) {
17+
// Anonymous user — no user record
18+
return res.json(undefined);
19+
}
20+
1221
const [user] = await db
1322
.select()
1423
.from(schema.users)

src/pages/api/saves/index.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,30 @@ export async function getUID(
5656
): Promise<string> {
5757
let uid = getCookie("uid", { req, res });
5858
if (uid && typeof uid === "string") {
59-
// uids can be anonymous, so we need to check if the user exists
60-
61-
const [user] = await db
62-
.select()
63-
.from(schema.users)
64-
.where(eq(schema.users.id, uid))
65-
.limit(1);
66-
67-
if (user) {
68-
// user exists, so we check if the user is authenticated
69-
// verify that the user has a stored token
70-
let token = getCookie("token", { req, res });
71-
if (!token) {
72-
res.status(400);
73-
throw new Error("User is not authenticated (1)");
74-
}
75-
// verify that the token is valid
76-
const { valid, userId } = verifyToken(
77-
token as string,
78-
user.cookie_secret,
79-
);
80-
if (!valid || userId !== uid) {
81-
res.status(400);
82-
throw new Error(`User is not authenticated (valid token: ${valid})`);
59+
// Only authenticated users (those with a token cookie) need a DB lookup.
60+
// Anonymous users will never be in the users table, so skip the query.
61+
const token = getCookie("token", { req, res });
62+
if (token) {
63+
const [user] = await db
64+
.select()
65+
.from(schema.users)
66+
.where(eq(schema.users.id, uid))
67+
.limit(1);
68+
69+
if (user) {
70+
const { valid, userId } = verifyToken(
71+
token as string,
72+
user.cookie_secret,
73+
);
74+
if (!valid || userId !== uid) {
75+
res.status(400);
76+
throw new Error(`User is not authenticated (valid token: ${valid})`);
77+
}
8378
}
8479
}
85-
// everything is ok, so we return the uid
8680
return uid as string;
8781
} else {
8882
console.log("Generating new UID...");
89-
// no uid, so we create an anonymous one
9083
uid = crypto.randomBytes(16).toString("hex");
9184
setCookie("uid", uid, {
9285
req,

0 commit comments

Comments
 (0)