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: templates/auth/src/README.md
+48-13Lines changed: 48 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,46 @@ user authentication.
7
7
8
8
## Step-by-step Instructions
9
9
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
+
CREATETABLEIF NOT EXISTS auth_users (
15
+
id INTEGERPRIMARY KEY AUTOINCREMENT,
16
+
username TEXT COLLATE NOCASE,
17
+
password TEXTNOT 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 NULLAND email IS NULL) OR (username IS NULLAND email IS NOT NULL) OR (username IS NOT NULLAND email IS NOT NULL))
25
+
);
26
+
27
+
CREATETRIGGERIF NOT EXISTS prevent_username_email_overlap
28
+
BEFORE INSERT ON auth_users
29
+
BEGIN
30
+
SELECT CASE
31
+
WHEN EXISTS (
32
+
SELECT1FROM auth_users
33
+
WHERE (NEW.usernameIS NOT NULLAND (NEW.username= username ORNEW.username= email))
34
+
OR (NEW.emailIS NOT NULLAND (NEW.email= username ORNEW.email= email))
35
+
)
36
+
THEN RAISE(ABORT, 'Username or email already exists')
37
+
END;
38
+
END;
39
+
40
+
CREATETABLEIF NOT EXISTS auth_sessions (
41
+
id INTEGERPRIMARY KEY AUTOINCREMENT,
42
+
user_id INTEGERNOT NULL,
43
+
session_token TEXTNOT 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
+
10
50
### Add service bindings to your StarbaseDB wrangler.toml
11
51
This will let your StarbaseDB instance know that we are deploying another Worker
12
52
and it should use that for our authentication application routing logic.
@@ -41,25 +81,20 @@ if (pathname.startsWith('/auth')) {
41
81
}
42
82
```
43
83
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
-
55
84
### Deploy template project to Cloudflare
56
85
Next, we will deploy our new authentication logic to a new Cloudflare Worker instance.
57
86
```
58
87
cd ./templates/auth
59
-
npm run deploy
88
+
npm i && npm run deploy
60
89
```
61
90
62
91
### Deploy updates in our main StarbaseDB
63
92
With all of the changes we have made to our StarbaseDB instance we can now deploy
64
93
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).
0 commit comments