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
Let's create a page for creating a new user and getting an access token. Let's start by creating the query to create a new user which will return the ``AccessToken.id`` which we will use as the access token itself. We will save this access token in a cookie so that we can authenticate requests in other server actions and route handlers.
@@ -151,7 +193,6 @@ Adding Access Control
151
193
);
152
194
}
153
195
154
-
155
196
.. edb:split-section::
156
197
157
198
We should see this page when we navigate to the signup page.
@@ -186,23 +227,74 @@ Limiting access
186
227
187
228
.. edb:split-section::
188
229
189
-
Along with allowing us to take advantage of our access policies in our queries, this will also allow us to redirect unauthenticated users to the signup page from any of our pages which should require authentication. Let's update our ``page.tsx`` file to redirect to the signup page if the user is not authenticated.
230
+
Along with allowing us to take advantage of our access policies in our queries, this will also allow us to redirect unauthenticated users to the signup page from any of our pages which should require authentication. Let's update our ``page.tsx`` file to redirect to the signup page if the user is not authenticated. We will also show the list of decks on this page.
190
231
191
-
.. code-block:: typescript-diff
192
-
:caption: app/page.tsx
232
+
.. tabs::
193
233
194
-
import { ImportForm } from "./form";
195
-
+ import { getAuthenticatedClient } from "@/lib/gel";
196
-
+ import { redirect } from "next/navigation";
234
+
.. code-tab:: typescript-diff
235
+
:caption: app/actions.ts
197
236
198
-
export default async function Page() {
199
-
+ const client = await getAuthenticatedClient();
200
-
+ if (!client) {
201
-
+ redirect("/signup");
202
-
+ }
203
-
+
204
-
return <ImportForm />;
205
-
}
237
+
"use server";
238
+
- import { client } from "@/lib/gel";
239
+
+ import { getAuthenticatedClient } from "@/lib/gel";
240
+
import { createDeck } from "./create-deck.query";
241
+
+ import e from "@/dbschema/edgeql-js";
242
+
243
+
export async function importDeck(formData: FormData) {
244
+
const deck = formData.get("deck");
245
+
if (typeof deck !== "string") {
246
+
return;
247
+
}
248
+
+
249
+
+ const client = await getAuthenticatedClient();
250
+
+ if (!client) {
251
+
+ return;
252
+
+ }
253
+
254
+
await createDeck(client, JSON.parse(deck));
255
+
}
256
+
+
257
+
+ export async function getDecks() {
258
+
+ const client = await getAuthenticatedClient();
259
+
+ if (!client) {
260
+
+ return [];
261
+
+ }
262
+
+
263
+
+ return e.select(e.Deck, (d) => ({
264
+
+ id: true,
265
+
+ name: true,
266
+
+ })).run(client);
267
+
+ }
268
+
269
+
.. code-tab:: typescript-diff
270
+
:caption: app/page.tsx
271
+
272
+
import { ImportForm } from "./form";
273
+
+ import { getAuthenticatedClient } from "@/lib/gel";
274
+
+ import { redirect } from "next/navigation";
275
+
+ import { getDecks } from "./actions";
276
+
277
+
export default async function Page() {
278
+
+ const client = await getAuthenticatedClient();
279
+
+ if (!client) {
280
+
+ redirect("/signup");
281
+
+ }
282
+
+
283
+
+ const decks = await getDecks(client);
284
+
+
285
+
- return <ImportForm />;
286
+
+ return (
287
+
+ <div>
288
+
+ <h1>Decks</h1>
289
+
+ <ul>
290
+
+ {decks.map((deck) => (
291
+
+ <li key={deck.id}>{deck.name}</li>
292
+
+ ))}
293
+
+ </ul>
294
+
+ <ImportForm />
295
+
+ </div>
296
+
+ );
297
+
}
206
298
207
299
.. edb:split-section::
208
300
@@ -274,14 +366,15 @@ Limiting access
274
366
.. code-block:: typescript-diff
275
367
:caption: app/deck/[id]/page.tsx
276
368
277
-
import { redirect } from "next/navigation";
369
+
import { notFound } from "next/navigation";
278
370
- import { client } from "@/lib/gel";
279
371
+ import { getAuthenticatedClient } from "@/lib/gel";
Copy file name to clipboardExpand all lines: docs/intro/quickstart/inheritance.rst
+20-9Lines changed: 20 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,20 +27,20 @@ Adding Shared Properties
27
27
+ }
28
28
+
29
29
- type User {
30
-
+ type User extends Timestamped {
30
+
+ type User extending Timestamped {
31
31
required name: str;
32
32
}
33
33
34
34
- type AccessToken {
35
-
+ type AccessToken extends Timestamped {
35
+
+ type AccessToken extending Timestamped {
36
36
required user: User;
37
37
required token: str {
38
38
constraint exclusive;
39
39
};
40
40
}
41
41
42
42
- type Deck {
43
-
+ type Deck extends Timestamped {
43
+
+ type Deck extending Timestamped {
44
44
required name: str;
45
45
description: str;
46
46
@@ -56,7 +56,7 @@ Adding Shared Properties
56
56
};
57
57
58
58
- type Card {
59
-
+ type Card extends Timestamped {
59
+
+ type Card extending Timestamped {
60
60
required order: int64;
61
61
required front: str;
62
62
required back: str;
@@ -73,19 +73,30 @@ Adding Shared Properties
73
73
74
74
.. edb:split-section::
75
75
76
-
When we create a migration, we need to set initial values for the ``created_at`` and ``updated_at`` properties on all existing objects. Since we don't have historical data for when these objects were actually created or modified, we'll set both timestamps to the current time when the migration runs by using ``datetime_of_statement()``.
76
+
When we create a migration, we need to set initial values for the ``created_at`` and ``updated_at`` properties on all existing objects. Since we don't have historical data for when these objects were actually created or modified, the migration will fall back to the default values we set in the ``Timestamped`` type.
77
77
78
78
.. code-block:: sh
79
79
80
80
$ npx gel migration create
81
-
fill_expr>datetime_of_statement()
81
+
did you create object type'default::Timestamped'? [y,n,l,c,b,s,q,?]
82
+
> y
83
+
did you alter object type'default::AccessToken'? [y,n,l,c,b,s,q,?]
84
+
> y
85
+
did you alter object type'default::User'? [y,n,l,c,b,s,q,?]
86
+
> y
87
+
did you alter object type'default::Card'? [y,n,l,c,b,s,q,?]
88
+
> y
89
+
did you alter object type'default::Deck'? [y,n,l,c,b,s,q,?]
90
+
> y
91
+
Created /home/strinh/projects/flashcards/dbschema/migrations/00004-m1d2m5n.edgeql, id: m1d2m5n5ajkalyijrxdliioyginonqbtfzihvwdfdmfwodunszstya
0 commit comments