feat: create and delete api keys

This commit is contained in:
Henry
2025-05-31 23:59:48 +01:00
parent e61dd4e27e
commit 1d00aa78b4
11 changed files with 153 additions and 24 deletions

View File

@@ -26,6 +26,13 @@ export const userRouter = createTRPCRouter({
name: z.string().nullable(),
image: z.string().nullable(),
stripeCustomerId: z.string().nullable(),
apiKey: z
.object({
id: z.number(),
prefix: z.string().nullable(),
key: z.string(),
})
.nullable(),
}),
)
.query(async ({ ctx }) => {
@@ -46,7 +53,12 @@ export const userRouter = createTRPCRouter({
});
}
return result;
const apiKey = result.apiKeys[0];
return {
...result,
apiKey: apiKey ?? null,
};
}),
update: protectedProcedure
.meta({

View File

@@ -57,23 +57,20 @@ export const createNextApiContext = async (req: NextApiRequest) => {
};
export const createRESTContext = async ({ req }: CreateNextContextOptions) => {
const authHeader = req.headers.authorization;
const accessToken = authHeader?.startsWith("Bearer ")
? authHeader.substring(7)
: null;
const db = createDrizzleClient();
const auth = initAuth(db);
if (!accessToken) {
return createInnerTRPCContext({ db, user: null });
let session;
try {
session = await auth.api.getSession({
// @ts-expect-error
headers: new Headers(req.headers),
});
} catch (error) {
console.error("Error getting session, ", error);
throw error;
}
const session = await auth.api.getSession({
// @ts-expect-error
headers: new Headers(req.headers),
});
return createInnerTRPCContext({ db, user: session?.user });
};