Skip to content

Commit fc245c9

Browse files
committed
Bump versions, add migrations and improve README.md
1 parent 8d8726c commit fc245c9

11 files changed

+103
-52
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,4 @@ home-assistant.db
133133
node_modules
134134
.idea
135135
test.credentials
136+
.wrangler

README.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
# Example code for implementing Register and Login in Cloudflare Workers using D1
22

3-
This is the example code for the article "Implementing Register and Login in Cloudflare Workers with D1"
4-
that you can [read here](https://massadas.com/posts/implementing-register-and-login-in-workers-d1/)
3+
This is the example code for the article `Implementing Register and Login in Cloudflare Workers with D1`
4+
that you can [read here](https://massadas.com/posts/implementing-register-and-login-in-workers-d1/).
55

6+
## Getting started with this project
7+
8+
Install the dependencies
9+
10+
```bash
11+
npm install
12+
```
13+
14+
Create a new D1 database
15+
16+
```bash
17+
wrangler d1 create <db-name> --experimental-backend
18+
```
19+
20+
Copy the `database_id` and place it in the `wrangler.toml` file
21+
22+
```toml
23+
[[d1_databases]]
24+
binding = "DB"
25+
database_name = "<your-db-name>"
26+
database_id = "<your-db-id>"
27+
```
28+
29+
Apply initial migrations, that include the `users` and `users_sessions` tables
30+
31+
```bash
32+
# Remote development
33+
wrangler d1 migrations apply DB
34+
35+
# Local development
36+
wrangler d1 migrations apply DB --local
37+
```
38+
39+
Start the project
40+
41+
```bash
42+
npm run serve
43+
```
44+
45+
46+
## Images
47+
48+
Swagger interface
49+
![Swagger interface](https://github.com/G4brym/authentication-using-d1-example/raw/main/docs/swagger.png)
50+
51+
Unauthenticated
52+
![Unauthenticated](https://github.com/G4brym/authentication-using-d1-example/raw/main/docs/unauthenticated.png)
53+
54+
Authentication
55+
![Authentication](https://github.com/G4brym/authentication-using-d1-example/raw/main/docs/authentication.png)
56+
57+
Endpoint results
58+
![Endpoint results](https://github.com/G4brym/authentication-using-d1-example/raw/main/docs/results.png)

docs/authentication.png

66.8 KB
Loading

docs/results.png

149 KB
Loading

docs/swagger.png

62 KB
Loading

docs/unauthenticated.png

114 KB
Loading

migrations/0000_initial_setup.sql

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Migration number: 0000 2023-06-23T18:27:14.127Z
2+
3+
create table users
4+
(
5+
id integer primary key autoincrement,
6+
email text not null unique,
7+
password text not null,
8+
name text not null
9+
);
10+
11+
create table users_sessions
12+
(
13+
session_id integer primary key autoincrement,
14+
user_id integer not null
15+
constraint users_sessions_users_id_fk
16+
references users
17+
on update cascade on delete cascade,
18+
token text not null,
19+
expires_at integer not null
20+
);

package-lock.json

+17-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"version": "0.0.1",
44
"private": true,
55
"scripts": {
6-
"deploy": "wrangler publish",
7-
"start": "wrangler dev"
6+
"deploy": "wrangler deploy",
7+
"serve": "wrangler dev"
88
},
99
"dependencies": {
10-
"@cloudflare/itty-router-openapi": "^0.1.2",
11-
"workers-qb": "file:../../../Projects/workers-qb"
10+
"@cloudflare/itty-router-openapi": "^0.1.9",
11+
"workers-qb": "^0.1.12"
1212
},
1313
"devDependencies": {
1414
"@cloudflare/workers-types": "^4.20230404.0",
15-
"wrangler": "^3.0.0"
15+
"wrangler": "^3.1.1"
1616
}
1717
}

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export default {
4646
fetch: async (request, env, ctx) => {
4747
// Inject query builder in every endpoint
4848
const qb = new D1QB(env.DB)
49-
qb.setDebugger(true)
49+
// qb.setDebugger(true)
50+
5051
return router.handle(request, env, {...ctx, qb: qb})
5152
},
5253
};

wrangler.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
name = "d1-auth-example"
22
main = "src/index.ts"
3-
compatibility_date = "2023-05-27"
3+
compatibility_date = "2023-05-18"
44

55

66
[[d1_databases]]
77
binding = "DB"
8-
database_name = "test-v2"
9-
database_id = "b7909b0b-1a66-40a2-8233-66cccbfd584f"
8+
database_name = "<your-db-name>"
9+
database_id = "<your-db-id>"
1010

1111

1212
[vars]

0 commit comments

Comments
 (0)