Checking for auth userId in only one place or in multiple places
Unanswered
Amorph posted this in #help-forum
AmorphOP
import { createStore } from "@/server/queries";
import { auth } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
export async function POST(request: Request) {
try {
const { userId } = auth();
const body = await request.json();
const { name } = body;
if (!userId) return new NextResponse("Unathorized", { status: 401 });
if (!name) return new NextResponse("Name is required", { status: 400 });
const store = await createStore(userId, name);
return NextResponse.json(store);
} catch (error) {
console.log("[STORES_POST]", error);
return new NextResponse("Internal error", { status: 500 });
}
}export async function createStore(userId: string, name: string) {
if (!userId) throw new Error("Unathorized");
return await prisma.store.create({
data: {
name,
userId,
},
});
}let's say i have this route handler where i check if i have an userId, then do other checks and then call my createStore function
in my queries file, i have defined the createStore function where it also checks if there's an userId
is the double checking redundant? my question is not only about this route handler example but also if i was in a layout file or page file
if i only had the userId checks in my queries then in terms of clean code i'd say the check for userid could get removed from all the files except my queries files but then if i have a file where i define all my queries then should i not add this check for userId, and only only have db queries in that file and nothing more?
1 Reply
AmorphOP
bump