fix(auth): make things work
This commit is contained in:
@@ -1,42 +1,43 @@
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
export function badRequest(body: unknown | string = "Bad request") {
|
||||
export function badRequest(cause?: unknown) {
|
||||
const resInit = {
|
||||
status: 400,
|
||||
statusText: "Bad request",
|
||||
}
|
||||
|
||||
if (typeof body === "string") {
|
||||
return new NextResponse(body, resInit)
|
||||
}
|
||||
|
||||
return NextResponse.json(body, resInit)
|
||||
return NextResponse.json(
|
||||
{
|
||||
cause,
|
||||
},
|
||||
resInit
|
||||
)
|
||||
}
|
||||
|
||||
export function notFound(body: unknown | string = "Not found") {
|
||||
export function notFound(cause?: unknown) {
|
||||
const resInit = {
|
||||
status: 404,
|
||||
statusText: "Not found",
|
||||
}
|
||||
|
||||
if (typeof body === "string") {
|
||||
return new NextResponse(body, resInit)
|
||||
}
|
||||
|
||||
return NextResponse.json(body, resInit)
|
||||
return NextResponse.json(
|
||||
{
|
||||
cause,
|
||||
},
|
||||
resInit
|
||||
)
|
||||
}
|
||||
|
||||
export function internalServerError(
|
||||
body: unknown | string = "Internal Server Error"
|
||||
) {
|
||||
export function internalServerError(cause?: unknown) {
|
||||
const resInit = {
|
||||
status: 500,
|
||||
statusText: "Internal Server Error",
|
||||
}
|
||||
|
||||
if (typeof body === "string") {
|
||||
return new NextResponse(body, resInit)
|
||||
}
|
||||
|
||||
return NextResponse.json(body, resInit)
|
||||
return NextResponse.json(
|
||||
{
|
||||
cause,
|
||||
},
|
||||
resInit
|
||||
)
|
||||
}
|
||||
|
||||
@@ -14,12 +14,14 @@ export const getUserSchema = z.object({
|
||||
name: z.string(),
|
||||
language: z.string(),
|
||||
lastName: z.string(),
|
||||
membership: z.object({
|
||||
currentPoints: z.number(),
|
||||
expirationDate: z.string(),
|
||||
membershipNumber: z.string(),
|
||||
memberSince: z.string(),
|
||||
}),
|
||||
membership: z
|
||||
.object({
|
||||
currentPoints: z.number(),
|
||||
expirationDate: z.string(),
|
||||
membershipNumber: z.string(),
|
||||
memberSince: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
phoneNumber: z.string(),
|
||||
profileId: z.string(),
|
||||
})
|
||||
|
||||
@@ -23,6 +23,7 @@ function fakingRequest<T>(payload: T): Promise<T> {
|
||||
export const userQueryRouter = router({
|
||||
get: protectedProcedure.query(async function ({ ctx }) {
|
||||
const apiResponse = await api.get(api.endpoints.v0.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
@@ -164,10 +165,6 @@ export const userQueryRouter = router({
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.length) {
|
||||
throw notFound(apiJson)
|
||||
}
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
throw internalServerError(verifiedData.error)
|
||||
|
||||
@@ -2,7 +2,11 @@ import { initTRPC } from "@trpc/server"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
|
||||
import { badRequestError, unauthorizedError } from "./errors/trpc"
|
||||
import {
|
||||
badRequestError,
|
||||
sessionExpiredError,
|
||||
unauthorizedError,
|
||||
} from "./errors/trpc"
|
||||
import { transformer } from "./transformer"
|
||||
|
||||
import type { Meta } from "@/types/trpc/meta"
|
||||
@@ -35,7 +39,7 @@ export const protectedProcedure = t.procedure.use(async function (opts) {
|
||||
}
|
||||
|
||||
if (session?.error === "RefreshAccessTokenError") {
|
||||
throw unauthorizedError()
|
||||
throw sessionExpiredError()
|
||||
}
|
||||
|
||||
if (!session?.user) {
|
||||
|
||||
Reference in New Issue
Block a user