feat: setup basic error handling on server
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user