Skip to content

Commit fde4fd3

Browse files
authored
Merge pull request #2062 from appwrite/update-nuxt-auth-tutorial
Fix Nuxt SSR Auth tutorial
2 parents c0e854d + 8be6b6d commit fde4fd3

File tree

7 files changed

+78
-33
lines changed

7 files changed

+78
-33
lines changed

src/routes/docs/tutorials/nuxt-ssr-auth/step-1/+page.markdoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ description: Add SSR authentication to your Nuxt app with Appwrite
55
step: 1
66
difficulty: beginner
77
back: /docs/tutorials
8-
draft: true
98
readtime: 20
109
framework: Nuxt SSR
1110
category: Auth
@@ -22,4 +21,4 @@ Before following this tutorial, have the following prepared:
2221
- A basic knowledge of Vue and Nuxt.
2322

2423
If you're inspired and wish to follow along, make sure you've followed [Start with Nuxt](https://appwrite.io/docs/quick-starts/nuxt) first.
25-
Clone the [demos-for-vue](https://github.com/appwrite/demos-for-vue) examples and follow along with the source code.
24+
Clone the [nuxt-ssr-auth](https://github.com/appwrite-community/nuxt-ssr-auth) repository and follow along with the source code.

src/routes/docs/tutorials/nuxt-ssr-auth/step-3/+page.markdoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ step: 3
77
Before you can use Appwrite, you need to create the Appwrite `Client` and set the project ID and endpoint.
88
The client is then used to create services like `Databases` and `Account`, so they all point to the same Appwrite project.
99

10-
Create a function to build services you need in a file like `src/lib/server/appwrite.js` and **exporting the instances**.
10+
Create a function to the build services you need in a file like `server/lib/appwrite.js` and **exporting the instances**.
1111

1212
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.
1313

@@ -25,9 +25,9 @@ export const SESSION_COOKIE = "my-custom-session";
2525

2626
export function createAdminClient() {
2727
const client = new Client()
28-
.setEndpoint(process.env.APPWRITE_ENDPOINT!)
29-
.setProject(process.env.APPWRITE_PROJECT!)
30-
.setKey(process.env.APPWRITE_KEY!);
28+
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
29+
.setProject(process.env.PUBLIC_APPWRITE_PROJECT)
30+
.setKey(process.env.APPWRITE_KEY);
3131

3232
return {
3333
get account() {

src/routes/docs/tutorials/nuxt-ssr-auth/step-5/+page.markdoc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Add authentication to a Nuxt project using Appwrite.
55
step: 5
66
---
77

8-
We can now implement our sign up page. Create a `signin.vue` file in the `pages` directory.
8+
We can now implement our sign up page. Create a `signup.vue` file in the `pages` directory.
99

1010
```vue
1111
<!-- pages/signup.vue -->
@@ -23,21 +23,25 @@ We can now implement our sign up page. Create a `signin.vue` file in the `pages`
2323
</template>
2424
```
2525

26-
This is an HTML form with an email and password input. When the form is submitted, we want to send the email and password to Appwrite to authenticate the user. To use Nuxt form actions we create a `signin.post.js` file in the `server/api` directory:
26+
This is an HTML form with an email and password input. When the form is submitted, we want to send the email and password to Appwrite to authenticate the user. To use Nuxt form actions we create a `signup.post.js` file in the `server/api` directory:
2727

2828
```javascript
2929
// server/api/signup.post.js
30+
import { ID } from "node-appwrite";
3031
import { SESSION_COOKIE, createAdminClient } from "~/server/lib/appwrite";
3132

3233
export default defineEventHandler(async (event) => {
3334
// Extract the form data
3435
const formData = await readFormData(event);
35-
const email = formData.get("email") as string;
36-
const password = formData.get("password") as string;
36+
const email = formData.get("email");
37+
const password = formData.get("password");
3738

3839
// Create the Appwrite client.
3940
const { account } = createAdminClient(event);
4041

42+
// Create a new user with email and password
43+
await account.create(ID.unique(), email, password);
44+
4145
// Create the session using the client
4246
const session = await account.createEmailPasswordSession(email, password);
4347

src/routes/docs/tutorials/nuxt-ssr-auth/step-6/+page.markdoc

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,53 @@ description: Add authentication to a Nuxt project using Appwrite.
55
step: 6
66
---
77

8-
Now the end-user is able to sign up, we can create the account page. This page will display basic information about the user, and allow the user to log out. Create a new file in the `pages` directory called `account.vue` and add the following code:
8+
Now the end-user is able to sign up, we can create the account page. This page will display basic information about the user, and allow the user to log out.
9+
10+
Before creating the account page, the route should fetch the user data. Create a new `user.get.js` file in the `server/routes/api` directory and add the following code:
11+
12+
```js
13+
// server/routes/api/user.get.js
14+
export default defineEventHandler(async (event) => {
15+
const user = event.context.user;
16+
17+
if (!user) {
18+
return false;
19+
}
20+
21+
return user;
22+
})
23+
```
24+
25+
Create a new file in the `pages` directory called `account.vue` and add the following code:
926

1027
```vue
1128
<!-- pages/account.vue -->
1229
<script setup>
13-
const { context } = useEvent();
14-
const { user } = context;
30+
// Fetch user data from your API endpoint
31+
const { data: user } = await useFetch('/api/user')
32+
33+
if (user.value === false) {
34+
await navigateTo('/signup')
35+
}
1536
</script>
1637

1738
<template>
18-
<ul>
19-
<li>
20-
<strong>Email:</strong> {{ user.email }}
21-
</li>
22-
<li>
23-
<strong>Name:</strong> {{ user.name }}
24-
</li>
25-
<li>
26-
<strong>ID: </strong> {{ user.$id }}
27-
</li>
28-
</ul>
29-
<form method="post" action="/api/signout">
30-
<button type="submit">Log out</button>
31-
</form>
39+
<div v-if="user">
40+
<ul>
41+
<li>
42+
<strong>Email:</strong> {{ user.email }}
43+
</li>
44+
<li>
45+
<strong>Name:</strong> {{ user.name }}
46+
</li>
47+
<li>
48+
<strong>ID: </strong> {{ user.$id }}
49+
</li>
50+
</ul>
51+
<form method="post" action="/api/signout">
52+
<button type="submit">Log out</button>
53+
</form>
54+
</div>
3255
</template>
3356
```
3457

src/routes/docs/tutorials/nuxt-ssr-auth/step-7/+page.markdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The `createOAuth2Token` method redirects the user to the OAuth provider, and the
4646
Handle the callback and create a session for the user. Create a new server route at `server/routes/api/oauth.get.js`.
4747

4848
```js
49-
// server/routes/api/oauth.post.js
49+
// server/routes/api/oauth.get.js
5050
import { SESSION_COOKIE, createAdminClient } from "~/server/lib/appwrite";
5151

5252
export default defineEventHandler(async (event) => {
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
---
22
layout: tutorial
3-
title: All set
3+
title: Enable the sign up and account pages
44
description: Add authentication to a Nuxt project using Appwrite.
55
step: 8
66
---
7-
If you want to see the complete source code with styling, see the [demos-for-vue](https://github.com/appwrite/demos-for-vue/tree/main/server-side-rendering) repository.
7+
For the last step, we must remove the welcome page and enable the pages we have created so far. For that, head to the `app.vue` file, and replace `<NuxtWelcome />` with `<NuxtPage />` so that code looks as follows:
88

9-
# Other authentication methods {% #other-authentication-methods %}
10-
Appwrite also supports OAuth, passwordless login, anonymous login, and phone login.
11-
Learn more about them in the [authentication guide](https://appwrite.io/docs/products/auth).
9+
```vue
10+
<!-- app.vue -->
11+
<template>
12+
<div>
13+
<NuxtRouteAnnouncer />
14+
<NuxtPage />
15+
</div>
16+
</template>
17+
```
18+
19+
Replace `<NuxtWelcome />` with `<NuxtPage />` to allow the user to navigate to the pages created so far, such as the sign-up and account pages, instead of the default Nuxt welcome page.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
layout: tutorial
3+
title: All set
4+
description: Add authentication to a Nuxt project using Appwrite.
5+
step: 9
6+
---
7+
If you want to see the complete source code with styling, see the [nuxt-ssr-auth](https://github.com/appwrite-community/nuxt-ssr-auth) repository.
8+
9+
# Other authentication methods {% #other-authentication-methods %}
10+
Appwrite also supports OAuth, passwordless login, anonymous login, and phone login.
11+
Learn more about them in the [authentication guide](https://appwrite.io/docs/products/auth).

0 commit comments

Comments
 (0)