Skip to content

Commit f5c32f8

Browse files
committed
Update auth README
1 parent 62cb886 commit f5c32f8

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

templates/auth/src/README.md

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,46 @@ user authentication.
77

88
## Step-by-step Instructions
99

10+
### Execute SQL statements in `migration.sql` to create required tables
11+
This will create the tables and constraints for user signup/login, and sessions. You can do this in the Studio user interface or by hitting your query endpoint in your StarbaseDB instance.
12+
13+
```sql
14+
CREATE TABLE IF NOT EXISTS auth_users (
15+
id INTEGER PRIMARY KEY AUTOINCREMENT,
16+
username TEXT COLLATE NOCASE,
17+
password TEXT NOT NULL,
18+
email TEXT COLLATE NOCASE,
19+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
20+
deleted_at TIMESTAMP DEFAULT NULL,
21+
email_confirmed_at TIMESTAMP DEFAULT NULL,
22+
UNIQUE(username),
23+
UNIQUE(email),
24+
CHECK ((username IS NOT NULL AND email IS NULL) OR (username IS NULL AND email IS NOT NULL) OR (username IS NOT NULL AND email IS NOT NULL))
25+
);
26+
27+
CREATE TRIGGER IF NOT EXISTS prevent_username_email_overlap
28+
BEFORE INSERT ON auth_users
29+
BEGIN
30+
SELECT CASE
31+
WHEN EXISTS (
32+
SELECT 1 FROM auth_users
33+
WHERE (NEW.username IS NOT NULL AND (NEW.username = username OR NEW.username = email))
34+
OR (NEW.email IS NOT NULL AND (NEW.email = username OR NEW.email = email))
35+
)
36+
THEN RAISE(ABORT, 'Username or email already exists')
37+
END;
38+
END;
39+
40+
CREATE TABLE IF NOT EXISTS auth_sessions (
41+
id INTEGER PRIMARY KEY AUTOINCREMENT,
42+
user_id INTEGER NOT NULL,
43+
session_token TEXT NOT NULL UNIQUE,
44+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
45+
deleted_at TIMESTAMP DEFAULT NULL,
46+
FOREIGN KEY (user_id) REFERENCES auth_users (id)
47+
);
48+
```
49+
1050
### Add service bindings to your StarbaseDB wrangler.toml
1151
This will let your StarbaseDB instance know that we are deploying another Worker
1252
and it should use that for our authentication application routing logic.
@@ -41,25 +81,20 @@ if (pathname.startsWith('/auth')) {
4181
}
4282
```
4383

44-
### Execute SQL statements in `migration.sql` to create required tables
45-
This will create the tables and constraints for user signup/login, and sessions
46-
required for the authentication operations to succeed.
47-
48-
### Run typegen in main project
49-
With our newly added service bindings in our StarbaseDB `wrangler.toml` file we can
50-
now generate an updated typegen output so our project knows that `AUTH` exists.
51-
```
52-
npm run cf-typegen
53-
```
54-
5584
### Deploy template project to Cloudflare
5685
Next, we will deploy our new authentication logic to a new Cloudflare Worker instance.
5786
```
5887
cd ./templates/auth
59-
npm run deploy
88+
npm i && npm run deploy
6089
```
6190

6291
### Deploy updates in our main StarbaseDB
6392
With all of the changes we have made to our StarbaseDB instance we can now deploy
6493
the updates so that all of the new authentication application logic can exist and
65-
be accessible.
94+
be accessible.
95+
```
96+
cd ../..
97+
npm run deploy
98+
```
99+
100+
**NOTE:** You will want to deploy your new service worker for authentication before deploying updates to your StarbaseDB instance, because the StarbaseDB instance will rely on the authentication worker being available (see the service bindings we added in the wrangler.toml file for reference).

worker-configuration.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
interface Env {
44
AUTHORIZATION_TOKEN: "ABC123";
55
DATABASE_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").DatabaseDurableObject>;
6-
AUTH: Fetcher;
76
}

0 commit comments

Comments
 (0)