| 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. Signing in with GitHub links it without `repo`, which private repositories need, so such a row counts as not linked until the import form links it again. */ |
| 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 | sql`'repo' = any(string_to_array(${schema. |
| 20 | ), |
| 21 | ); |
| 22 | return account?.id |
| 23 | } |
| 24 | |
| 25 | /** The user's GitHub token, refreshed by Better Auth when it can be; null when GitHub is not linked. */ |
| 26 | export async function githubAccessToken |
| 27 | db: Pick |
| 28 | auth: AuthService |
| 29 | userId: string, |
| 30 | ) { |
| 31 | const accountId = |
| 32 | if (!accountId) |
| 33 | const { accessToken |
| 34 | body: { accountId, |
| 35 | }); |
| 36 | return accessToken || |
| 37 | } |