Skip to content

Commit d9a62e4

Browse files
committed
fix: move access token to env variables
1 parent 610a164 commit d9a62e4

File tree

7 files changed

+45
-26
lines changed

7 files changed

+45
-26
lines changed

examples/sync-demo-bare/.env.example

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
# Get this from: https://dashboard.sqlitecloud.io/ > Your Database > OffSync / Configuration
66
SQLITE_CLOUD_DATABASE_ID=
77

8-
# Your SQLite Cloud API Key
9-
# Get this from: https://dashboard.sqlitecloud.io/ > Your Database > Configuration
8+
# Authentication (provide ONE of the following):
9+
# Access token for user-level authentication (when using RLS)
10+
# In a real app, this comes from your auth system (e.g. Supabase Auth)
11+
# For testing, use a token from the user you want to test with
12+
ACCESS_TOKEN=
13+
# API key for simple authentication (if not using RLS)
14+
# Get this from: https://dashboard.sqlitecloud.io/ > Your Project > Settings > API Keys
1015
SQLITE_CLOUD_API_KEY=
1116

1217
# Local database name (can be any name you prefer)

examples/sync-demo-bare/src/App.tsx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ import {
1818
useSyncStatus,
1919
useSqliteTransaction,
2020
} from '@sqliteai/sqlite-sync-react-native';
21-
import { SQLITE_CLOUD_DATABASE_ID, DATABASE_NAME, TABLE_NAME } from '@env';
22-
23-
let HARDCODED_ACCESS_TOKEN: string;
24-
HARDCODED_ACCESS_TOKEN = 'replace-with-access-token';
21+
import {
22+
SQLITE_CLOUD_DATABASE_ID,
23+
DATABASE_NAME,
24+
TABLE_NAME,
25+
ACCESS_TOKEN,
26+
SQLITE_CLOUD_API_KEY,
27+
} from '@env';
2528

2629
/**
2730
* Demo app showcasing the reactive hooks and dual connection architecture:
@@ -276,7 +279,12 @@ function TestApp() {
276279
}
277280

278281
export default function App() {
279-
if (!SQLITE_CLOUD_DATABASE_ID || !DATABASE_NAME || !TABLE_NAME) {
282+
if (
283+
!SQLITE_CLOUD_DATABASE_ID ||
284+
!DATABASE_NAME ||
285+
!TABLE_NAME ||
286+
(!ACCESS_TOKEN && !SQLITE_CLOUD_API_KEY)
287+
) {
280288
return (
281289
<View style={styles.container}>
282290
<Text style={styles.error}>
@@ -290,19 +298,10 @@ export default function App() {
290298
);
291299
}
292300

293-
if (HARDCODED_ACCESS_TOKEN === 'replace-with-access-token') {
294-
return (
295-
<View style={styles.container}>
296-
<Text style={styles.error}>
297-
Replace the hardcoded access token in src/App.tsx before running the
298-
example.
299-
</Text>
300-
<Text style={styles.errorDetails}>
301-
See README.md for setup instructions.
302-
</Text>
303-
</View>
304-
);
305-
}
301+
// Auth: provide either ACCESS_TOKEN (for RLS) or SQLITE_CLOUD_API_KEY (without RLS)
302+
const authProps = ACCESS_TOKEN
303+
? { accessToken: ACCESS_TOKEN }
304+
: { apiKey: SQLITE_CLOUD_API_KEY };
306305

307306
return (
308307
<SQLiteSyncProvider
@@ -322,7 +321,7 @@ export default function App() {
322321
]}
323322
syncMode="polling"
324323
adaptivePolling={{ baseInterval: 3000 }}
325-
accessToken={HARDCODED_ACCESS_TOKEN}
324+
{...authProps}
326325
debug={true}
327326
>
328327
<TestApp />

examples/sync-demo-bare/types/env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ declare module '@env' {
22
export const SQLITE_CLOUD_DATABASE_ID: string;
33
export const DATABASE_NAME: string;
44
export const TABLE_NAME: string;
5+
export const ACCESS_TOKEN: string;
6+
export const SQLITE_CLOUD_API_KEY: string;
57
}

examples/sync-demo-expo/.env.example

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
# Get this from: https://dashboard.sqlitecloud.io/ > Your Database > OffSync / Configuration
66
SQLITE_CLOUD_DATABASE_ID=
77

8-
# Your SQLite Cloud API Key
9-
# Get this from: https://dashboard.sqlitecloud.io/
8+
# Authentication (provide ONE of the following):
9+
# Access token for user-level authentication (when using RLS)
10+
# In a real app, this comes from your auth system (e.g. Supabase Auth)
11+
# For testing, use a token from the user you want to test with
12+
ACCESS_TOKEN=
13+
# API key for simple authentication (if not using RLS)
14+
# Get this from: https://dashboard.sqlitecloud.io/ > Your Project > Settings > API Keys
1015
SQLITE_CLOUD_API_KEY=
1116

1217
# Local database name (can be any name you prefer)

examples/sync-demo-expo/app.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default {
1212
extra: {
1313
sqliteCloudDatabaseId: process.env.SQLITE_CLOUD_DATABASE_ID,
1414
sqliteCloudApiKey: process.env.SQLITE_CLOUD_API_KEY,
15+
accessToken: process.env.ACCESS_TOKEN,
1516
databaseName: process.env.DATABASE_NAME,
1617
tableName: process.env.TABLE_NAME,
1718
eas: {

examples/sync-demo-expo/src/App.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Constants from 'expo-constants';
2828
const SQLITE_CLOUD_DATABASE_ID =
2929
Constants.expoConfig?.extra?.sqliteCloudDatabaseId;
3030
const SQLITE_CLOUD_API_KEY = Constants.expoConfig?.extra?.sqliteCloudApiKey;
31+
const ACCESS_TOKEN = Constants.expoConfig?.extra?.accessToken;
3132
const DATABASE_NAME = Constants.expoConfig?.extra?.databaseName;
3233
const TABLE_NAME = Constants.expoConfig?.extra?.tableName;
3334

@@ -394,9 +395,9 @@ export default function App() {
394395

395396
if (
396397
!SQLITE_CLOUD_DATABASE_ID ||
397-
!SQLITE_CLOUD_API_KEY ||
398398
!DATABASE_NAME ||
399-
!TABLE_NAME
399+
!TABLE_NAME ||
400+
(!ACCESS_TOKEN && !SQLITE_CLOUD_API_KEY)
400401
) {
401402
return (
402403
<View style={styles.container}>
@@ -411,6 +412,11 @@ export default function App() {
411412
);
412413
}
413414

415+
// Auth: provide either ACCESS_TOKEN (for RLS) or SQLITE_CLOUD_API_KEY (without RLS)
416+
const authProps = ACCESS_TOKEN
417+
? { accessToken: ACCESS_TOKEN }
418+
: { apiKey: SQLITE_CLOUD_API_KEY };
419+
414420
return (
415421
<SQLiteSyncProvider
416422
databaseId={SQLITE_CLOUD_DATABASE_ID}
@@ -432,7 +438,7 @@ export default function App() {
432438
renderPushPermissionPrompt={({ allow, deny }) => (
433439
<PermissionDialog visible onAllow={allow} onDeny={deny} />
434440
)}
435-
apiKey={SQLITE_CLOUD_API_KEY}
441+
{...authProps}
436442
debug={true}
437443
>
438444
<TestApp deviceToken={deviceToken} />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare module '@env' {
22
export const SQLITE_CLOUD_DATABASE_ID: string;
33
export const SQLITE_CLOUD_API_KEY: string;
4+
export const ACCESS_TOKEN: string;
45
export const DATABASE_NAME: string;
56
export const TABLE_NAME: string;
67
}

0 commit comments

Comments
 (0)