Reviewed Next.js SSR tutorial

This commit is contained in:
Vincent (Wen Yu) Ge
2024-02-12 14:41:22 -05:00
parent ed94383b13
commit 0e66c8bb66
12 changed files with 136 additions and 43 deletions

View File

@@ -12,7 +12,9 @@ Create a function to build services you need in a file like `src/lib/server/appw
As part of the function, set the current user's session if they are logged in. This is done by accessing the session cookie from the request and calling the `setSession(session)` with the cookie value.
{% info title="Appwrite client security" %}
We recommend creating a new instance of the Appwrite client for each request. This ensures that the client is not shared between requests and that the session is not shared between users.
Notice that `createAppwriteClient` returns **a new instance** of the Appwrite Client.
When using Appwrite in server-integrations, it's important to **never share a `Client` instance** between two requests.
Doing so could create security vulnerabilities.
{% /info %}
```js
@@ -52,17 +54,37 @@ export function createAppwriteClient(event) {
`APPWRITE_KEY`, `PUBLIC_APPWRITE_ENDPOINT` and `PUBLIC_APPWRITE_PROJECT_ID` are environment variables that are exported in your project's [.env file](https://kit.svelte.dev/docs/modules#$env-dynamic-public).
You can get the values for these variables from the Appwrite console. The `PUBLIC_APPWRITE_ENDPOINT` and `PUBLIC_APPWRITE_PROJECT_ID` are the endpoint and project ID for your Appwrite project.
You can get the values for these variables from the Appwrite console.
The `PUBLIC_APPWRITE_ENDPOINT` is the endpoint of your Appwrite project, and the `PUBLIC_APPWRITE_PROJECT` is the ID of the project you want to use.
You can get the values for these variables from the Appwrite console.
{% only_dark %}
![Create project screen](/images/docs/quick-starts/dark/create-project.png)
{% /only_dark %}
{% only_light %}
![Create project screen](/images/docs/quick-starts/create-project.png)
{% /only_light %}
The `APPWRITE_KEY` is an Appwrite API key with the necessary permissions to read and write accounts and sessions.
For this tutorial you'll need an API key with the following scopes:
| Category {% width=120 %} | Required scopes | Purpose |
|-----------|---------------------|---------|
| Accounts | `accounts.read` | Allows API key to read account information. |
| | `accounts.write` | Allows API key to create, update, and delete account information. |
{% only_dark %}
![Server integrations](/images/docs/quick-starts/dark/integrate-server.png)
{% /only_dark %}
{% only_light %}
![Server integrations](/images/docs/quick-starts/integrate-server.png)
{% /only_light %}
For example, your `.env` might look something similar to this.
```text
APPWRITE_KEY=<YOUR_APPWRITE_KEY>
APPWRITE_KEY=<YOUR_API_KEY>
PUBLIC_APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
PUBLIC_APPWRITE_PROJECT_ID=<YOUR_APPWRITE_PROJECT_ID>
```
For this tutorial you'll need an API key with the following scopes:
- `accounts.read`
- `accounts.write`
- `sessions.write`
PUBLIC_APPWRITE_PROJECT=<YOUR_PROJECT_ID>
```

View File

@@ -1,6 +1,6 @@
---
layout: tutorial
title: Adding OAuth2 authentication with SSR
title: OAuth2 authentication with SSR
description: Add authentication to a SvelteKit project using Appwrite.
step: 7
---