Skip to content

Commit 0123fc8

Browse files
authored
Update auth0.md
Added more detail to the section on working alongside a User model
1 parent 28c4049 commit 0123fc8

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

docs/guides/authentication/auth0.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Integrating with Auth0.
33
sidebar_position: 6
4-
sidebar_label: 🚧 Auth0
4+
sidebar_label: Auth0
55
---
66

77
# Integrating With Auth0
@@ -97,7 +97,7 @@ const currentUser = async (req) => {
9797

9898
return {
9999
id: session.user.sub,
100-
dbUserExists: !isNull(dbUser), // Cause onboarding behavior
100+
dbUserExists: !isNull(dbUser), // If the user doesn't exist in the database, this variable can be set in the session
101101
};
102102
};
103103

@@ -108,6 +108,45 @@ export const getPrisma = async (req) => {
108108
};
109109
```
110110
111+
You can use the result of this token to redirect a user to an onboarding flow, such as a signup form:
112+
113+
```ts
114+
app.get('/', async (req, res) => {
115+
const user = await currentUser(req);
116+
if (!user.dbUserExists) {
117+
res.redirect('/onboarding');
118+
}
119+
res.send('hello user');
120+
});
121+
```
122+
123+
and create a user record using the ID from the Auth0 session, here's a small example using the Auth0 React SDK:
124+
125+
```ts
126+
import React from "react";
127+
import { useAuth0 } from "@auth0/auth0-react";
128+
129+
const Profile = () => {
130+
const { user, isAuthenticated, isLoading } = useAuth0();
131+
const { trigger, isMutating } = useCreateUser();
132+
133+
const createUser = useCallback(async (event: FormEvent<HTMLFormElement>) => {
134+
const formData = new FormData(event.currentTarget);
135+
const name = formData.get('name');
136+
137+
await trigger({
138+
data: {
139+
id: user.sub,
140+
name: name,
141+
},
142+
});
143+
}, [trigger, user])
144+
145+
return <UserForm onSubmit={createUser}/>
146+
};
147+
```
148+
149+
111150
When the client is created, the database is queried using the contents of the Auth0 token.
112151

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

0 commit comments

Comments
 (0)