Skip to content

Commit

Permalink
docs: update guide on return urls (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasvotava authored Nov 4, 2024
1 parent 0232a3e commit b240d0d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions docs/how-to-guides/state-return-url.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

State is useful if you want the server to return something back to you to help you understand in what
context the authentication was initiated. It is mostly used to store the url you want your user to be redirected
to after successful login. You may use `.state` property to get the state returned from the server.
to after successful login. You may use `.state` property to get the state returned from the server or access
it from the `state` parameter in the callback function.

Example:

Expand All @@ -19,9 +20,11 @@ async def google_login(return_url: str):
# Send return_url to Google as a state so that Google knows to return it back to us
return await google_sso.get_login_redirect(redirect_uri=request.url_for("google_callback"), state=return_url)

async def google_callback(request: Request):
async def google_callback(request: Request, state: str | None = None):
async with google_sso:
user = await google_sso.verify_and_process(request)
# google_sso.state now holds your return_url (https://example.com/welcome)
return RedirectResponse(google_sso.state)
if state is not None:
return RedirectResponse(state)
else:
return user
```

0 comments on commit b240d0d

Please sign in to comment.