Skip to content

Commit b8c0d5c

Browse files
docs: add user unlinking example to UserLinking.md
1 parent 32bbb00 commit b8c0d5c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

examples/UserLinking.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,103 @@ link_user_url = await server_client.start_link_user(options, store_options=store
127127
```
128128

129129
Read more above in [Configuring the Transaction and State Store](./ConfigureStore.md).
130+
131+
## Start Unlinking The User
132+
133+
User unlinking allows you to remove a previously linked identity from a user account. The process is similar to linking and begins by calling `start_unlink_user()` to obtain an authorization URL.
134+
135+
```python
136+
# Start the unlink user flow by providing the connection to unlink.
137+
options = {
138+
"connection": "google-oauth2", # The connection to unlink
139+
"authorization_params": {"redirect_uri": "http://localhost:3000/auth/callback"},
140+
"app_state": {"returnTo": "http://localhost:3000/profile"}
141+
}
142+
143+
# Assume store_options contains Request/Response objects required by the state store.
144+
store_options = {"request": request, "response": response}
145+
146+
unlink_user_url = await server_client.start_unlink_user(options, store_options=store_options)
147+
148+
# Redirect the user to unlink_user_url
149+
# (In a FastAPI route, you would return a RedirectResponse with unlink_user_url)
150+
```
151+
152+
Once the unlink user flow is completed, the user will be redirected back to the `redirect_uri` specified in the `authorization_params`. At that point, it's required to call `complete_unlink_user()` to finalize the user-unlinking process. Read more below in [Complete Unlinking The User](#complete-unlinking-the-user).
153+
154+
### Passing `authorization_params`
155+
156+
Just like `start_link_user()`, you can customize the parameters passed to the `/authorize` endpoint:
157+
158+
1. **Globally:**
159+
Configure them when instantiating the `ServerClient`.
160+
161+
2. **Per-call Override:**
162+
Supply them when calling `start_unlink_user()`.
163+
164+
```python
165+
options = {
166+
"connection": "google-oauth2",
167+
"authorization_params": {
168+
"redirect_uri": "http://localhost:3000/auth/callback",
169+
"audience": "urn:custom:api"
170+
}
171+
}
172+
unlink_user_url = await server_client.start_unlink_user(options, store_options=store_options)
173+
```
174+
175+
>[!NOTE]
176+
> Any `authorization_params` property specified when calling `start_unlink_user()` will override the same, statically configured, `authorization_params` property on `ServerClient`.
177+
178+
### Passing App State
179+
180+
The `app_state` parameter allows you to pass custom data (for example, a return URL) that will be returned when the unlinking process is complete.
181+
182+
```python
183+
options = {
184+
"connection": "google-oauth2",
185+
"app_state": {"return_to": "http://localhost:3000/profile"}
186+
}
187+
unlink_user_url = await server_client.start_unlink_user(options, store_options=store_options)
188+
189+
# Later, when completing unlinking:
190+
result = await server_client.complete_unlink_user(callback_url, store_options=store_options)
191+
print(result.get("app_state").get("return_to")) # Should output "http://localhost:3000/profile"
192+
```
193+
194+
### Passing Store Options
195+
196+
Every method that interacts with the state or transaction store accepts a second parameter, `store_options`. This parameter should include the HTTP request and response objects (or equivalents) needed to manage cookies or sessions.
197+
198+
```python
199+
store_options = {"request": request, "response": response}
200+
unlink_user_url = await server_client.start_unlink_user(options, store_options=store_options)
201+
```
202+
203+
Read more above in [Configuring the Transaction and State Store](./ConfigureStore.md).
204+
205+
## Complete Unlinking The User
206+
207+
After the user has been redirected back to your application (at the `redirect_uri`), you need to complete the unlinking process. This is done by calling `complete_unlink_user()`, which extracts the necessary parameters from the callback URL and returns the `app_state`.
208+
209+
```python
210+
# Complete the unlinking process:
211+
result = await server_client.complete_unlink_user(callback_url, store_options=store_options)
212+
213+
# Retrieve the app_state:
214+
print(result.get("app_state").get("return_to"))
215+
```
216+
217+
> [!NOTE]
218+
> The URL passed to `complete_unlink_user()` should be the full callback URL from Auth0, including the `state` and `code` parameters.
219+
220+
### Passing Store Options
221+
222+
Just like most methods, `complete_unlink_user()` accepts a second argument that is used to pass to the configured Transaction and State Store:
223+
224+
```python
225+
store_options = {"request": request, "response": response}
226+
result = await server_client.complete_unlink_user(callback_url, store_options=store_options)
227+
```
228+
229+
Read more above in [Configuring the Transaction and State Store](./ConfigureStore.md).

0 commit comments

Comments
 (0)