|
| 1 | +--- |
| 2 | +title: Access Tokens |
| 3 | +description: Grant to your users, devices, tenant, access to SQLite Cloud database and services. |
| 4 | +category: platform |
| 5 | +status: publish |
| 6 | +slug: access-tokens |
| 7 | +--- |
| 8 | + |
| 9 | +Access Tokens let backend systems securely grant users, devices, tenants, etc. access to SQLite Cloud database and services (SQLite Sync, Weblite, etc.). These endpoints enable full token lifecycle management: creation, inspection, validation, update, and revocation. All endpoints require authentication. Use an **API Key** or an **Access Token** via the `Authorization` header. |
| 10 | + |
| 11 | +The API Documentation for the Access Tokens API can be found in the **Weblite** section in the [Dashboard](https://dashboard.sqlitecloud.io). |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Example Using SQLite Cloud Access Tokens with Google Login |
| 16 | + |
| 17 | +In the repository on GitHub [sqlitecloud/examples](https://github.com/sqlitecloud/examples/tree/main/access-tokens-and-social-login), we created a simple app to demonstrate how to generate and use Access Tokens. |
| 18 | + |
| 19 | +We’ll log in with Google, grab a token, and use it to interact with SQLite Cloud Weblite APIs. Here’s how it works. |
| 20 | + |
| 21 | +In the snippet below, we handle the Google Login callback when the user has completed the login on Google. Here, you can exchange the `code` with the Google Access Token and then decide what to do with it as needed. |
| 22 | + |
| 23 | +```typescript |
| 24 | +if (pathname === "/auth/callback") { |
| 25 | + const q = query; |
| 26 | + if (q.state !== STATE || !q.code) { |
| 27 | + return send(res, 400, "Invalid state or missing code"); |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + // Exchange code for tokens |
| 32 | + // Store the Google Token in the database |
| 33 | + const googleToken = await getGoogleTokens(q.code as string); |
| 34 | + ... |
| 35 | +``` |
| 36 | +
|
| 37 | +Now we have authenticated the user, we are ready to request SQLite Cloud to create a new SQLite Cloud Access Token assigned to this user. |
| 38 | +
|
| 39 | +```typescript |
| 40 | +async function getSQLiteCloudToken(userId: string) { |
| 41 | + const payload = { |
| 42 | + name: "test-user-token", // A name for the token, can be anything you want |
| 43 | + userId, |
| 44 | + expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // expires in 24 hours |
| 45 | + }; |
| 46 | + |
| 47 | + const res = await fetch("https://<your-project-url>/v2/tokens", { |
| 48 | + method: "POST", |
| 49 | + headers: { |
| 50 | + Authorization: `Bearer ${SQLITE_CLOUD_API_KEY}`, |
| 51 | + "Content-Type": "application/json", |
| 52 | + }, |
| 53 | + body: JSON.stringify(payload), |
| 54 | + }); |
| 55 | + if (!res.ok) { |
| 56 | + throw new Error(`Failed to create SQLite Cloud token: ${res.statusText}`); |
| 57 | + } |
| 58 | + |
| 59 | + return res.json(); |
| 60 | +} |
| 61 | +``` |
| 62 | +
|
| 63 | +In the response JSON, the `data.token` field contains the Access Token. |
| 64 | +
|
| 65 | +Finally, the user is authorized to securely access SQLite Cloud services like the Weblite API to perform a query on the database: |
| 66 | +
|
| 67 | +```typescript |
| 68 | +const res = await fetch("https://<your-project-url>/v2/weblite/sql", { |
| 69 | + method: "POST", |
| 70 | + headers: { |
| 71 | + Authorization: "Bearer " + sqliteCloudToken, |
| 72 | + "Content-Type": "application/json", |
| 73 | + }, |
| 74 | + body: JSON.stringify({ |
| 75 | + sql: "USE DATABASE chinook.sqlite;SELECT * FROM artists LIMIT 10;", |
| 76 | + }), |
| 77 | +}); |
| 78 | +... |
| 79 | +``` |
| 80 | +
|
| 81 | +The result depends on the [Row Level Security](https://docs.sqlitecloud.io/docs/rls) policies you enabled for the tables. |
0 commit comments