feat: create and delete api keys
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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 });
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { magicLinkClient } from "better-auth/client/plugins";
|
||||
import { apiKeyClient, magicLinkClient } from "better-auth/client/plugins";
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
|
||||
export const authClient = createAuthClient({
|
||||
plugins: [magicLinkClient()],
|
||||
plugins: [magicLinkClient(), apiKeyClient()],
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import type { dbClient } from "@kan/db/client";
|
||||
import { users } from "@kan/db/schema";
|
||||
import { apiKey, users } from "@kan/db/schema";
|
||||
|
||||
export const getById = async (db: dbClient, userId: string) => {
|
||||
return await db.query.users.findFirst({
|
||||
@@ -13,6 +13,17 @@ export const getById = async (db: dbClient, userId: string) => {
|
||||
image: true,
|
||||
stripeCustomerId: true,
|
||||
},
|
||||
with: {
|
||||
apiKeys: {
|
||||
columns: {
|
||||
id: true,
|
||||
prefix: true,
|
||||
key: true,
|
||||
},
|
||||
orderBy: desc(apiKey.createdAt),
|
||||
limit: 1,
|
||||
},
|
||||
},
|
||||
where: eq(users.id, userId),
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { relations } from "drizzle-orm";
|
||||
import {
|
||||
bigserial,
|
||||
boolean,
|
||||
@@ -50,7 +51,7 @@ export const verification = pgTable("verification", {
|
||||
updatedAt: timestamp("updatedAt"),
|
||||
}).enableRLS();
|
||||
|
||||
export const apiKey = pgTable("apiKey", {
|
||||
export const apiKey = pgTable("apikey", {
|
||||
id: bigserial("id", { mode: "number" }).primaryKey(),
|
||||
name: text("name"),
|
||||
start: text("start"),
|
||||
@@ -75,3 +76,10 @@ export const apiKey = pgTable("apiKey", {
|
||||
permissions: text("permissions"),
|
||||
metadata: text("metadata"),
|
||||
}).enableRLS();
|
||||
|
||||
export const apiKeyRelations = relations(apiKey, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [apiKey.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -8,3 +8,5 @@ export * from "./labels";
|
||||
export * from "./lists";
|
||||
export * from "./users";
|
||||
export * from "./workspaces";
|
||||
|
||||
export { apiKey as apikey } from "./auth";
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
varchar,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
import { apiKey } from "./auth";
|
||||
import { boards } from "./boards";
|
||||
import { cards } from "./cards";
|
||||
import { imports } from "./imports";
|
||||
@@ -33,6 +34,7 @@ export const usersRelations = relations(users, ({ many }) => ({
|
||||
imports: many(imports),
|
||||
lists: many(lists),
|
||||
workspaces: many(workspaces),
|
||||
apiKeys: many(apiKey),
|
||||
}));
|
||||
|
||||
export const usersToWorkspacesRelations = relations(
|
||||
|
||||
Reference in New Issue
Block a user