Fix SSR for sveltekit

This commit is contained in:
Vincent (Wen Yu) Ge
2024-03-10 11:52:34 +00:00
parent b3304ce5a1
commit 0ac9ca7fe6
5 changed files with 30 additions and 19 deletions

View File

@@ -58,6 +58,8 @@ Doing so could create security vulnerabilities.
# Environment variables {% #environment-variables %}
`NEXT_APPWRITE_KEY`, `NEXT_PUBLIC_APPWRITE_ENDPOINT` and `NEXT_PUBLIC_APPWRITE_PROJECT` are environment variables that are exported in your project's [.env file](https://kit.svelte.dev/docs/modules#$env-dynamic-public).
For example, your `.env` might look something similar to this.
```env
@@ -66,7 +68,7 @@ NEXT_PUBLIC_APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
NEXT_PUBLIC_APPWRITE_PROJECT=<YOUR_PROJECT_ID>
```
The `PUBLIC_APPWRITE_ENDPOINT` is the endpoint of your appwrite instance , and the `PUBLIC_APPWRITE_PROJECT` is the ID of the project you want to use.
The `NEXT_PUBLIC_APPWRITE_ENDPOINT` is the endpoint of your appwrite instance , and the `NEXT_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)
@@ -75,7 +77,7 @@ You can get the values for these variables from the Appwrite console.
![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 create new sessions.
The `NEXT_APPWRITE_KEY` is an Appwrite API key with the necessary permissions to create new sessions.
For this tutorial you'll need an API key with the following scopes:

View File

@@ -21,14 +21,14 @@ Doing so could create security vulnerabilities.
// src/lib/server/appwrite.js
import { Client, Account } from 'node-appwrite';
import { APPWRITE_KEY } from '$env/static/private';
import { PUBLIC_APPWRITE_ENDPOINT, PUBLIC_APPWRITE_PROJECT_ID } from '$env/static/public';
import { PUBLIC_APPWRITE_ENDPOINT, PUBLIC_APPWRITE_PROJECT } from '$env/static/public';
export const SESSION_COOKIE = 'my-custom-session';
export function createAdminClient() {
const client = new Client()
.setEndpoint(PUBLIC_APPWRITE_ENDPOINT)
.setProject(PUBLIC_APPWRITE_PROJECT_ID)
.setProject(PUBLIC_APPWRITE_PROJECT)
.setKey(APPWRITE_KEY); // Set the Appwrite API key!
// Return the services we want to use.
@@ -42,11 +42,15 @@ export function createAdminClient() {
export function createSessionClient(event) {
const client = new Client()
.setEndpoint(PUBLIC_APPWRITE_ENDPOINT)
.setProject(PUBLIC_APPWRITE_PROJECT_ID);
.setProject(PUBLIC_APPWRITE_PROJECT);
// Extract our custom domain's session cookie from the request
const session = event.cookies.get(SESSION_COOKIE);
if (session) client.setSession(session);
if (!session) {
throw new Error("No user session");
}
client.setSession(session);
// Return the services we want to use.
return {
@@ -57,9 +61,17 @@ export function createSessionClient(event) {
}
```
# Environment variables {% #environment-variables %}
`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.
For example, your `.env` might look something similar to this.
```text
APPWRITE_KEY=<YOUR_API_KEY>
PUBLIC_APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
PUBLIC_APPWRITE_PROJECT=<YOUR_PROJECT_ID>
```
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.
@@ -83,12 +95,4 @@ For this tutorial you'll need an API key with the following scopes:
{% /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_API_KEY>
PUBLIC_APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
PUBLIC_APPWRITE_PROJECT=<YOUR_PROJECT_ID>
```
{% /only_light %}

View File

@@ -30,6 +30,8 @@ export async function handle({ event, resolve }) {
To ensure the `locals` object is typed correctly, we can add a type definition for it in the `env.d.ts` file:
```ts
// env.d.ts
import type { Models } from "node-appwrite";
// See https://kit.svelte.dev/docs/types#app
@@ -48,9 +50,12 @@ declare global {
export {};
```
Now, use the `locals` object in the home page to redirect based on the user's login status. Create a new file in the `src/routes` directory called `+page.server.js`:
Now, use the `locals` object in the home page to redirect based on the user's login status.
Create a new file in the `src/routes` directory called `+page.server.js`:
```js
// src/routes/+page.server.js
import { redirect } from '@sveltejs/kit';
export async function load({ locals }) {

View File

@@ -25,7 +25,7 @@ This is an HTML form with an email and password input. When the form is submitte
import { SESSION_COOKIE, createAdminClient } from "$lib/server/appwrite.js";
import { redirect } from "@sveltejs/kit";
import { ID } from "node-appwrite";
import { ID, OAuthProvider } from "node-appwrite";
export const actions = {
signup: async ({ request, cookies }) => {

View File

@@ -24,7 +24,7 @@ Add a new server route to handle the redirect.
```js
// src/routes/signup/+page.server.js
import { SESSION_COOKIE, createAdminClient } from "$lib/server/appwrite.js";
import { SESSION_COOKIE, createAdminClient, createSessionClient } from "$lib/server/appwrite.js";
import { redirect } from "@sveltejs/kit";
import { ID, OAuthProvider } from "node-appwrite";