Skip to content

Commit 07df893

Browse files
authored
doc: Auth0 guide touch up (#404)
1 parent 5cd5114 commit 07df893

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

docs/guides/authentication/auth0.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ sidebar_label: Auth0
66

77
# Integrating With Auth0
88

9-
This guide provides simple examples of using Auth0 authentication with ZenStack. While Auth0 offers various authentication methods, this guide assumes you understand Auth0's authentication basics and can obtain a user object after authentication.
9+
> This guide is contributed by [Sinan Güçlü](https://github.com/Driptap).
10+
11+
This guide provides simple examples of using [Auth0](https://auth0.com/) authentication with ZenStack. While Auth0 offers various authentication methods, this guide assumes you understand Auth0's authentication basics and can obtain a user object after authentication.
1012

1113
## The basic premise of applying a custom session object to ZenStack.
1214

1315
This section explains how to apply a custom session object to ZenStack by creating a user object and providing it to the enhance function when creating the Prisma client.
1416

15-
Create a user object and provide it to the enhance function when creating the Prisma client.
17+
Create a user object and provide it to the `enhance` function when creating the Prisma client.
1618

1719
```ts
1820
export const getPrisma = async (req) => {
@@ -23,10 +25,10 @@ export const getPrisma = async (req) => {
2325

2426
You can provide a type in the ZModel to express what the contents of the user object is.
2527

26-
```prisma
28+
```zmodel
2729
type Auth {
28-
id String @id
29-
specialKey String
30+
id String @id
31+
specialKey String
3032
@@auth
3133
}
3234
```
@@ -63,13 +65,13 @@ export const getPrismaJWT = async (req) => {
6365
6466
This would populate your `auth()` in the Zmodel with the object you've just created from auth0; enabling checks like:
6567
66-
```prisma
68+
```zmodel
6769
@@allow('read, update, create', auth().id == this.id)
6870
```
6971
7072
or
7173
72-
```prisma
74+
```zmodel
7375
@@allow('read, update, create', auth().specialKey == 'SUPERMAN')
7476
```
7577
@@ -84,19 +86,24 @@ You can create your application in such a way that a lack of the user existing i
8486
8587
```ts
8688
const currentUser = async (req) => {
87-
const session = await getSession(req); // get your auth0 auth session
89+
// Get your auth0 auth session
90+
const session = await getSession(req);
8891

8992
if (!session?.user.sub) {
90-
throw new Error('UNAUTHENTICATED'); // Throw an error if the user isn't authenticated
93+
// Throw an error if the user isn't authenticated
94+
throw new Error('UNAUTHENTICATED');
9195
}
9296

93-
const dbUser = await prisma.user.findUnique({ // Find the user in the db
97+
// Find the user in the db
98+
const dbUser = await prisma.user.findUnique({
9499
where: { id: session.user.sub },
95100
});
96101

97102
return {
98103
id: session.user.sub,
99-
dbUserExists: !isNull(dbUser), // If the user doesn't exist in the database, this variable can be set in the session
104+
// If the user doesn't exist in the database, this variable
105+
// can be set in the session
106+
dbUserExists: !isNull(dbUser),
100107
};
101108
};
102109

@@ -150,11 +157,13 @@ When the client is created, the database is queried using the contents of the Au
150157

151158
In this case, the Auth type is what provide authentication, not the User model, for example:
152159

153-
```prisma
160+
```zmodel
154161
// Specify the auth type
155162
type Auth {
156163
id String @id
157-
@@auth // And decorate it with @@auth to tell ZenStack to use this as the session object
164+
// And decorate it with @@auth to tell ZenStack to use this
165+
// as the session object
166+
@@auth
158167
}
159168

160169
// add your user model as a regular model

0 commit comments

Comments
 (0)