feat: setup basic error handling on server

This commit is contained in:
Henry
2024-07-30 22:36:39 +01:00
parent 9931a585a5
commit 2033f37e72
7 changed files with 190 additions and 34 deletions

View File

@@ -1,4 +1,5 @@
import { z } from "zod";
import { TRPCError } from "@trpc/server";
import {
createTRPCRouter,
@@ -12,9 +13,15 @@ import * as userRepo from "~/server/db/repository/user.repo";
export const authRouter = createTRPCRouter({
getUser: protectedProcedure.query(async ({ ctx }) => {
if (!ctx.user?.id) return;
const userId = ctx.user?.id;
const result = await userRepo.getById(ctx.db, ctx.user.id);
if (!userId)
throw new TRPCError({
message: `User not authenticated`,
code: "UNAUTHORIZED",
});
const result = await userRepo.getById(ctx.db, userId);
return result;
}),
@@ -33,7 +40,11 @@ export const authRouter = createTRPCRouter({
loginWithOAuth: publicProcedure
.input(z.object({ provider: z.string() }))
.mutation(async ({ ctx, input }) => {
if (input.provider !== "google") return null;
if (input.provider !== "google")
throw new TRPCError({
message: `Unsupported OAuth provider: ${input.provider}`,
code: "BAD_REQUEST",
});
const { data } = await ctx.db.auth.signInWithOAuth({
provider: "google",