-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathauth.ts
More file actions
30 lines (25 loc) · 1021 Bytes
/
auth.ts
File metadata and controls
30 lines (25 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import db from '@codebuff/internal/db'
import * as schema from '@codebuff/internal/db/schema'
import { eq } from 'drizzle-orm'
import type {
GetUserInfoFromApiKeyInput,
GetUserInfoFromApiKeyOutput,
UserColumn,
} from '@codebuff/common/types/contracts/database'
export async function getUserInfoFromApiKey<T extends UserColumn>(
params: GetUserInfoFromApiKeyInput<T>,
): GetUserInfoFromApiKeyOutput<T> {
const { apiKey, fields } = params
// Build a typed selection object for user columns
const userSelection = Object.fromEntries(
fields.map((field) => [field, schema.user[field]]),
) as { [K in T]: (typeof schema.user)[K] }
const rows = await db
.select({ user: userSelection }) // <-- important: nest under 'user'
.from(schema.user)
.leftJoin(schema.session, eq(schema.user.id, schema.session.userId))
.where(eq(schema.session.sessionToken, apiKey))
.limit(1)
// Drizzle returns { user: ..., session: ... }, we return only the user part
return rows[0]?.user ?? null
}