| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import type { AuthService |
| 3 | import { and, eq |
| 4 | |
| 5 | import type { Auth |
| 6 | |
| 7 | /** The `account` row a user linked GitHub with, which Better Auth reads the token from. */ |
| 8 | export async function githubAccountIdOf |
| 9 | db: Pick |
| 10 | userId: string, |
| 11 | ) { |
| 12 | const [account] |
| 13 | .select({ id: |
| 14 | .from(schema.account |
| 15 | .where( |
| 16 | and( |
| 17 | eq(schema.account |
| 18 | eq(schema.account |
| 19 | ), |
| 20 | ); |
| 21 | return account?.id |
| 22 | } |
| 23 | |
| 24 | /** The user's GitHub token, refreshed by Better Auth when it can be; null when GitHub is not linked. */ |
| 25 | export async function githubAccessToken |
| 26 | db: Pick |
| 27 | auth: AuthService |
| 28 | userId: string, |
| 29 | ) { |
| 30 | const accountId = |
| 31 | if (!accountId) |
| 32 | const { accessToken |
| 33 | body: { accountId, |
| 34 | }); |
| 35 | return accessToken || |
| 36 | } |