-
Notifications
You must be signed in to change notification settings - Fork 315
THU-506: Move integration secrets to local-only table #859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raivieiraadriano92
merged 10 commits into
main
from
raivieiraadriano92/thu-506-integration-secrets-local-only-table
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e0787bf
feat: add integrations_secrets local-only table and DAL
raivieiraadriano92 ba3fcf9
refactor: move OAuth flow state from synced settings to sessionStorage
raivieiraadriano92 10e9b99
refactor: move integration credentials and enabled flags to local-onl…
raivieiraadriano92 a950c6b
fix: invalidate integration status query after saving credentials
raivieiraadriano92 2d8046e
fix: preserve enabled flag on refresh and surface integration email
raivieiraadriano92 36ba88a
fix: surface disconnect error in integrations settings
raivieiraadriano92 470508a
fix: invalidate integration status after onboarding disconnect
raivieiraadriano92 c4f1326
refactor: derive provider-connected from query in onboarding state
raivieiraadriano92 e78f551
refactor: extract shared OAuth credentials helper for google and micr…
raivieiraadriano92 f46a0cb
fix: persist OAuth flow state in localStorage to survive mobile app t…
raivieiraadriano92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { eq } from 'drizzle-orm' | ||
| import type { AnyDrizzleDatabase } from '../db/database-interface' | ||
| import { integrationsSecretsTable } from '../db/tables' | ||
| import type { OAuthProvider } from '../lib/auth' | ||
|
|
||
| type IntegrationCredentials = { | ||
| access_token: string | ||
| refresh_token?: string | ||
| expires_at?: number | ||
| profile?: { | ||
| email: string | ||
| name: string | ||
| picture?: string | ||
| } | ||
| } | ||
|
|
||
| type IntegrationRow = { | ||
| credentials: IntegrationCredentials | ||
| enabled: boolean | ||
| } | ||
|
|
||
| /** Get credentials and enabled flag for a provider. Returns null if no row exists. */ | ||
| export const getIntegrationCredentials = async ( | ||
| db: AnyDrizzleDatabase, | ||
| provider: OAuthProvider, | ||
| ): Promise<IntegrationRow | null> => { | ||
| const row = await db | ||
| .select() | ||
| .from(integrationsSecretsTable) | ||
| .where(eq(integrationsSecretsTable.provider, provider)) | ||
| .get() | ||
|
|
||
| if (!row?.credentials) { | ||
| return null | ||
| } | ||
|
|
||
| try { | ||
| return { | ||
| credentials: JSON.parse(row.credentials) as IntegrationCredentials, | ||
| enabled: row.enabled === 1, | ||
| } | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Save credentials for a provider (insert or update). | ||
| * Uses SELECT-then-INSERT-or-UPDATE because PowerSync local-only tables are views that don't support UPSERT. | ||
| */ | ||
| export const saveIntegrationCredentials = async ( | ||
| db: AnyDrizzleDatabase, | ||
| provider: OAuthProvider, | ||
| credentials: IntegrationCredentials, | ||
| enabled: boolean, | ||
| ): Promise<void> => { | ||
| const json = JSON.stringify(credentials) | ||
| const existing = await db | ||
| .select() | ||
| .from(integrationsSecretsTable) | ||
| .where(eq(integrationsSecretsTable.provider, provider)) | ||
| .get() | ||
|
|
||
| if (existing) { | ||
| await db | ||
| .update(integrationsSecretsTable) | ||
| .set({ credentials: json, enabled: enabled ? 1 : 0 }) | ||
| .where(eq(integrationsSecretsTable.provider, provider)) | ||
| } else { | ||
| await db.insert(integrationsSecretsTable).values({ | ||
| provider, | ||
| credentials: json, | ||
| enabled: enabled ? 1 : 0, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update credentials for a provider without changing the enabled flag. | ||
| * No-op if the provider has no existing row (only callable after a connect). | ||
| */ | ||
| export const updateIntegrationCredentials = async ( | ||
| db: AnyDrizzleDatabase, | ||
| provider: OAuthProvider, | ||
| credentials: IntegrationCredentials, | ||
| ): Promise<void> => { | ||
| await db | ||
| .update(integrationsSecretsTable) | ||
| .set({ credentials: JSON.stringify(credentials) }) | ||
| .where(eq(integrationsSecretsTable.provider, provider)) | ||
| } | ||
|
|
||
| /** Toggle the enabled flag for a provider without changing credentials. */ | ||
| export const setIntegrationEnabled = async ( | ||
| db: AnyDrizzleDatabase, | ||
| provider: OAuthProvider, | ||
| enabled: boolean, | ||
| ): Promise<void> => { | ||
| await db | ||
| .update(integrationsSecretsTable) | ||
| .set({ enabled: enabled ? 1 : 0 }) | ||
| .where(eq(integrationsSecretsTable.provider, provider)) | ||
| } | ||
|
|
||
| /** Delete credentials for a provider (disconnect). */ | ||
| export const deleteIntegrationCredentials = async (db: AnyDrizzleDatabase, provider: OAuthProvider): Promise<void> => { | ||
| await db.delete(integrationsSecretsTable).where(eq(integrationsSecretsTable.provider, provider)) | ||
| } | ||
|
|
||
| const parseEmail = (raw: string | null | undefined): string | null => { | ||
| if (!raw) { | ||
| return null | ||
| } | ||
| try { | ||
| return (JSON.parse(raw) as IntegrationCredentials).profile?.email ?? null | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** Get connection/enabled status for all integration providers. */ | ||
| export const getIntegrationStatus = async ( | ||
| db: AnyDrizzleDatabase, | ||
| ): Promise<{ | ||
| googleConnected: boolean | ||
| googleEnabled: boolean | ||
| googleEmail: string | null | ||
| microsoftConnected: boolean | ||
| microsoftEnabled: boolean | ||
| microsoftEmail: string | null | ||
| }> => { | ||
| const rows = await db.select().from(integrationsSecretsTable).all() | ||
|
|
||
| const google = rows.find((r) => r.provider === 'google') | ||
| const microsoft = rows.find((r) => r.provider === 'microsoft') | ||
|
|
||
| return { | ||
| googleConnected: !!google?.credentials, | ||
| googleEnabled: google?.enabled === 1, | ||
| googleEmail: parseEmail(google?.credentials), | ||
| microsoftConnected: !!microsoft?.credentials, | ||
| microsoftEnabled: microsoft?.enabled === 1, | ||
| microsoftEmail: parseEmail(microsoft?.credentials), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.