H
Hono4mo ago
MarvinKR

status 200 with empty JSON

I am not logged in when doing the api call from my vs code but it's not returning unauthorized 401 but a 200 with an empty json?
// Fetch all workflows
.get("/", clerkMiddleware(), async (c) => {
const auth = getAuth(c);

if (!auth?.userId) {
return c.json({ error: "Unauthorized" }, 401);
}

// Fetch the user details to get the companyId
const [user] = await db
.select({
companyId: users.companyId,
})
.from(users)
.where(eq(users.id, auth.userId));

if (!user) {
return c.json({ error: "User not found" }, 404);
}

if (!user?.companyId) {
return c.json({ error: "User is not affiliated with a company" }, 401);
}

const data = await db
.select()
.from(workflows)
.where(eq(workflows.companyId, user.companyId));

return c.json({ data });
})
// Fetch all workflows
.get("/", clerkMiddleware(), async (c) => {
const auth = getAuth(c);

if (!auth?.userId) {
return c.json({ error: "Unauthorized" }, 401);
}

// Fetch the user details to get the companyId
const [user] = await db
.select({
companyId: users.companyId,
})
.from(users)
.where(eq(users.id, auth.userId));

if (!user) {
return c.json({ error: "User not found" }, 404);
}

if (!user?.companyId) {
return c.json({ error: "User is not affiliated with a company" }, 401);
}

const data = await db
.select()
.from(workflows)
.where(eq(workflows.companyId, user.companyId));

return c.json({ data });
})
No description
14 Replies
Nico
Nico4mo ago
What does user return? Is it a single object or array? If it's an array you need to do
if (user.length < 1) {
throw new Error()
}
if (user.length < 1) {
throw new Error()
}
It looks like drizzle syntax which always returns an array is you use .select Also this is redundant, you can actually do this in the original db call and only make one database trip. I'd be happy to show you how
const data = await db
.select()
.from(workflows)
.where(eq(workflows.companyId, user.companyId));
const data = await db
.select()
.from(workflows)
.where(eq(workflows.companyId, user.companyId));
MarvinKR
MarvinKR4mo ago
But I'm using the Clerk middleware so since I'm not connected, the code should stop here & throw an error:
if (!auth?.userId) {
return c.json({ error: "Unauthorized" }, 401);
}
if (!auth?.userId) {
return c.json({ error: "Unauthorized" }, 401);
}
Nico
Nico4mo ago
You might need to start adding some console logs check and see what the code sees at each point to find out where it’s going wrong If you want to share you code I can take a look but it won’t be until tomorrow when I get back on my computer
MarvinKR
MarvinKR4mo ago
I added them but I don't see the console logs in my terminal?
Nico
Nico4mo ago
Can you try to do console.log(user + “ here”) you should get that in your terminal If not it’s not hitting that route
MarvinKR
MarvinKR4mo ago
ok seems like there is an issue with the ClerkMiddleware() because I see unauthorized 401 when I remove it (and the console logs works too when I remove it
Nico
Nico4mo ago
Im not familiar with the clerk middleware I have never used Clerk
Nico
Nico4mo ago
If you think there is an issue with the package itself please open an issue here https://github.com/honojs/middleware
GitHub
GitHub - honojs/middleware: monorepo for Hono third-party middlewar...
monorepo for Hono third-party middleware/helpers/wrappers - honojs/middleware
MarvinKR
MarvinKR4mo ago
Found the issue I need 2x the same key "NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY" & "CLERK_PUBLISHABLE_KEY" in a nextjs project Thanks for picking that up! I did an inner join on the workflow table, you're right!
Nico
Nico4mo ago
You can also use the query method. I prefer that because it will return a single object instead of an array. Here is an example from a project of mine
const findUserByKey = async (key: string) => {
return db.query.users.findFirst({
where: eq(users.key, key),
columns: {
password: false,
},
with: {
userRoles: {
columns: {
role: true,
},
});
};
const findUserByKey = async (key: string) => {
return db.query.users.findFirst({
where: eq(users.key, key),
columns: {
password: false,
},
with: {
userRoles: {
columns: {
role: true,
},
});
};
Nico
Nico4mo ago
You just have to setup relationships in your model as shown here https://orm.drizzle.team/docs/rqb#one-to-one
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Nico
Nico4mo ago
Then you don't have to worry about joins
MarvinKR
MarvinKR4mo ago
Yeah I’m not a big fan of the query api, prefer the native sql (that’s why I picked drizzle over prisma) But thanks for this idea! (Also I’m returning an array in this situation)
Nico
Nico4mo ago
I get it I'm used to writing the SQL syntax as well. I recently found the query api and been using it and I like it in the fact is less work to do joins
Want results from more Discord servers?
Add your server