vorsV
Convex Community3y ago
1 reply
vors

Checking auth with decorators

This is great, thank you for the reference! Question about that -- why do this middle-ware magic instead of simple function call like
async function checkAuth({db, auth}) {
  const identity = await auth.getUserIdentity();
  if (!identity) {
    throw new Error("Unauthenticated call to mutation");
  }
  const user = await db
    .query("users")
    .withIndex("by_token", q =>
      q.eq("tokenIdentifier", identity.tokenIdentifier)
    )
    .unique();
  if (!user) {
    throw new Error("Unauthenticated call to mutation");
  }
}

export default mutation(async ({ db, auth }, body) => {
  checkAuth({ db, auth });
  const message = { body, user: user._id };
  await db.insert("messages", message);
});
Was this page helpful?