Add typed context and middleware for oRPC procedures

Use implement(contract).$context<APIContext>() for proper type safety
in all procedure handlers. Create authMiddleware and loginRequestMiddleware
using os.middleware() and apply with .use() on routes requiring auth.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
RevIQ
2026-01-09 15:36:26 +08:00
parent 829d365e80
commit a4d1f28f3d
12 changed files with 483 additions and 334 deletions

View File

@@ -1,9 +1,3 @@
import type { APIContext } from "../../context.js";
import { implement, ORPCError } from "@orpc/server";
import { contract } from "@reviq/api-contract";
const os = implement(contract);
/**
* Verify user email with token from URL
* Public procedure - no authentication required
@@ -14,13 +8,16 @@ const os = implement(contract);
* 3. Update user's email_verified_at timestamp
* 4. Delete the verification record
*/
import { ORPCError } from "@orpc/server";
import { os } from "../base.js";
export const verifyEmail = os.auth.verifyEmail.handler(
async ({ input, context }) => {
const ctx = context as APIContext;
const { token } = input;
// Find the verification record
const verification = await ctx.db
const verification = await context.db
.selectFrom("email_verifications")
.select(["id", "user_id", "expires_at"])
.where("token", "=", token)
@@ -35,7 +32,7 @@ export const verifyEmail = os.auth.verifyEmail.handler(
// Check if token is expired
if (new Date() > verification.expires_at) {
// Clean up expired token
await ctx.db
await context.db
.deleteFrom("email_verifications")
.where("id", "=", verification.id)
.execute();
@@ -46,14 +43,14 @@ export const verifyEmail = os.auth.verifyEmail.handler(
}
// Update user's email_verified_at
await ctx.db
await context.db
.updateTable("users")
.set({ email_verified_at: new Date() })
.where("id", "=", verification.user_id)
.execute();
// Delete the verification record
await ctx.db
await context.db
.deleteFrom("email_verifications")
.where("id", "=", verification.id)
.execute();