add sub and did fields to db
This commit is contained in:
parent
8575f38db9
commit
767a1c2fab
5 changed files with 35 additions and 21 deletions
|
|
@ -5,6 +5,6 @@ export default defineConfig({
|
|||
schema: "./src/db/schema.ts",
|
||||
dialect: "sqlite",
|
||||
dbCredentials: {
|
||||
url: process.env.DB_FILE_NAME!,
|
||||
url: process.env.DB_FILE_NAME ?? "./users.sqlite",
|
||||
},
|
||||
});
|
||||
|
|
|
|||
1
index.ts
1
index.ts
|
|
@ -1 +0,0 @@
|
|||
console.log("Hello via Bun!");
|
||||
|
|
@ -2,7 +2,6 @@ import { int, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|||
|
||||
export const usersTable = sqliteTable("users_table", {
|
||||
id: int().primaryKey({ autoIncrement: true }),
|
||||
name: text().notNull(),
|
||||
age: int().notNull(),
|
||||
email: text().notNull().unique(),
|
||||
sub: int().notNull(),
|
||||
did: text().notNull().unique(),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,16 +6,22 @@ import { usersTable } from "./schema";
|
|||
|
||||
export const db = drizzle(new Database(process.env.DB_FILE_NAME!));
|
||||
|
||||
export async function createUser(name: string, age: number, email: string) {
|
||||
export async function createUser(sub: number, did: string) {
|
||||
const user: typeof usersTable.$inferInsert = {
|
||||
name,
|
||||
age,
|
||||
email,
|
||||
sub,
|
||||
did,
|
||||
};
|
||||
await db.insert(usersTable).values(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
export async function getUser(email: string) {
|
||||
return await db.select().from(usersTable).where(eq(usersTable.email, email));
|
||||
export async function getUser(did: string) {
|
||||
return await db
|
||||
.select({ did: usersTable.did, sub: usersTable.sub })
|
||||
.from(usersTable)
|
||||
.where(eq(usersTable.did, did))
|
||||
.limit(1)
|
||||
.then((rows) => {
|
||||
return rows[0] || null;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
30
src/index.ts
30
src/index.ts
|
|
@ -11,19 +11,29 @@ Bun.serve({
|
|||
return Response.redirect(redirectUrl.toString(), 302);
|
||||
},
|
||||
"/auth/:provider/callback": async (req) => {
|
||||
const did = "did:plc:lgb2xgd64n3swnh7jbtgpvhl";
|
||||
|
||||
const { callback } = await import(`./auth/${req.params.provider}/client`);
|
||||
let claims = await callback(new URL(req.url));
|
||||
return Response.json(claims);
|
||||
if (!claims) {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
} else {
|
||||
const userInfo = await getUser(did);
|
||||
if (userInfo) {
|
||||
// user exists
|
||||
return Response.json(userInfo);
|
||||
} else {
|
||||
// create new user
|
||||
return Response.json(
|
||||
await createUser(claims.sub, did).then((user) => user)
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/users/:email": async (req) => {
|
||||
return Response.json(await getUser(req.params.email));
|
||||
},
|
||||
"/api/create": async () => {
|
||||
return Response.json(
|
||||
await createUser("Badtz", 27, "me@badtz.dev").then((user) =>
|
||||
JSON.stringify(user)
|
||||
)
|
||||
);
|
||||
"/api/users/:did": async (req) => {
|
||||
console.log("Fetching user with DID:", req.params.did);
|
||||
|
||||
return Response.json(await getUser(req.params.did));
|
||||
},
|
||||
},
|
||||
websocket: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue