You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/5.blog/2.drawing-app-with-nuxt-and-cloudflare-r2.md
+22-20
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,11 @@ category: Tutorial
16
16
17
17
## Introduction
18
18
19
-
I won't go into each details of the code, but I'll try to explain the main concepts and how to build a drawing app with Nuxt and Cloudflare R2.
19
+
I won't go into each detail of the code, but I'll try to explain the main concepts and how to build a drawing app with Nuxt and Cloudflare R2.
20
20
21
-
Atidraw is a web application that lets you to create and share your drawings with the world. Harnessing the power of OAuth for user authentication and Cloudflare R2 to store and list the drawings.
21
+
Atidraw is a web application that lets you create and share your drawings with the world. Our app uses OAuth for user authentication and Cloudflare R2 to store and list drawings.
22
22
23
-
The application is running with server-side rendering on the edge using Cloudflare Pages on the Workers free plan.
23
+
The application runs with server-side rendering on the edge using Cloudflare Pages on the Workers free plan.
The `blob` option will use Cloudflare platform proxy in development and automatically create a Cloudflare R2 bucket for your project when you deploy it. It also provide helpers to upload and list files.
64
+
The `blob` option will use Cloudflare platform proxy in development and automatically create a Cloudflare R2 bucket for your project when you deploy it. It also provides helpers to upload and list files.
65
65
::
66
66
67
67
::tip
68
-
The project is also using the `future.compatibilityVersion: 4` option to use the [new directory structure](https://nuxt.com/docs/getting-started/upgrade#new-directory-structure).
68
+
The project is also using the `future.compatibilityVersion: 4` option to leverage the [new directory structure](https://nuxt.com/docs/getting-started/upgrade#new-directory-structure).
69
69
::
70
70
71
71
## User Authentication
72
72
73
-
For user authentication, we'll use [`nuxt-auth-utils`](https://github.com/atinux/nuxt-auth-utils), it providers helpers to authenticate users with OAuth providers and storing the user session in encrypted cookies.
73
+
For user authentication, we'll use [`nuxt-auth-utils`](https://github.com/atinux/nuxt-auth-utils). It provides functions to authenticate users with OAuth providers and stores the user session in encrypted cookies.
74
74
75
75
First, we need to set up a session secret (used to encrypt & decrypt the session cookie) and our OAuth application credentials in the `.env` file:
The `.get.ts` suffix is indicating that only GET requests will be handled by this route.
104
+
The `.get.ts` suffix indicates that only GET requests will be handled by this route.
105
105
::
106
106
107
107
When the user hits `/auth/github`:
108
108
1.`oauthGitHubEventHandler` redirects the user to the GitHub OAuth page
109
-
2. The user is redirected back to **/auth/github**
109
+
2. The user is then redirected back to **/auth/github**
110
110
3.`onSuccess()` is called and the user session is set in a cookie
111
-
4. The user is redirected to **/draw**
111
+
4. The user is finally redirected to **/draw**
112
112
113
113
In [`app/pages/draw.vue`](https://github.com/atinux/atidraw/blob/main/app/pages/draw.vue), we can leverage [`useUserSession()`](https://github.com/atinux/nuxt-auth-utils?tab=readme-ov-file#vue-composable) to know if the user is authenticated or not.
The `requireUserSession()`helper is provided by [`nuxt-auth-utils`](https://github.com/atinux/nuxt-auth-utils) and will throw a `401` error if the user is not authenticated.
254
+
The `requireUserSession()`function is provided by [`nuxt-auth-utils`](https://github.com/atinux/nuxt-auth-utils) and will throw a `401` error if the user is not authenticated.
255
255
::
256
256
257
-
As you can see, we don't need to have a database as we store the user metadata in the Cloudflare R2 bucket custom metadata.
257
+
As you can see, we don't need a database as we store the user metadata in the Cloudflare R2 bucket custom metadata.
258
258
259
259
::note
260
-
Learn more about the [`hubBlob()`](/docs/storage/blob) server helper to work with the Cloudflare R2 bucket.
260
+
Learn more about the [`hubBlob()`](/docs/storage/blob) server function to work with the Cloudflare R2 bucket.
261
261
::
262
262
263
263
## List Drawings
264
264
265
-
It's time to list our users drawings, first, we need to create a new API route in `server/api/drawings.get.ts`:
265
+
It's time to list our user drawings! First, however, we need to create a new API route in `server/api/drawings.get.ts`:
That's it! We have a minimal and fully functional drawing application.
298
+
::
297
299
298
300
## Drawings Order
299
301
300
302
You may have noticed that the last drawing is displayed last, this is because Cloudflare R2 is using alphabetical order to list the files and we use the timestamp (using `Date.now()`) as the file name. Also, R2 doesn't support listing files with a custom order.
301
303
302
304
Even though it's easy to add a Cloudflare D1 database with [`hubDatabase()`](/docs/storage/database), I wanted to keep this example as simple as possible.
303
305
304
-
Instead, I had the idea to use the timestamp in 2050 minus the timestamp of the drawing to get a descending order. Yeah, it's a bit hacky but it works, until 2050, it's still a long time 😄.
306
+
Instead, I had the idea to use the timestamp in 2050 minus the timestamp of the drawing to get a descending order. It's not perfect but it works, until 2050, it's still a long time 😄.
305
307
306
308
Let's update our **/api/upload** route to update the filename:
307
309
@@ -465,13 +467,13 @@ Or go to https://admin.hub.nuxt.com and select your project.
465
467
466
468
Congratulations! You've now built a fully functional drawing application using Nuxt and Cloudflare R2 for storage. Users can create drawings, save them to the cloud, and access them from anywhere.
467
469
468
-
Next, we are going to leverage Cloudflare AI to generate the alt text for the user drawings (accessibility & SEO) as well as generating an alternative drawing using AI.
470
+
Next, we are going to leverage Cloudflare AI to generate the alternative text for the user drawings (accessibility & SEO) as well as generating an alternative drawing using AI.
469
471
470
472
Feel free to expand on this foundation and add your own unique features to make Atidraw yours!
0 commit comments