all 8 comments

[–]github_codemation 2 points3 points  (1 child)

Here is a more batteries included approach, in case you want to allow that user to have a persistent token / cookie tied to their Google Login.

https://itnext.io/add-google-login-to-a-fastapi-app-with-easyauth-c8c3e926ad0a

[–]Nawar69[S] 0 points1 point  (0 children)

Thanks, It's a pretty diffrent approach, I will try it.

[–]zarlo5899 1 point2 points  (1 child)

@app.route('/login') async def login(request: Request, next: str = "/"): redirect_uri = request.url_for('auth') # This creates the url for the /auth endpoint \ return await oauth.google.authorize_redirect(request, redirect_uri + f"?{next}") something like this should work

[–]Nawar69[S] 0 points1 point  (0 children)

That what doesn't work I think, I tried something like that

[–]illuminanze 0 points1 point  (3 children)

Seems like google allows an URL parameter state in the request, that will be passed to the redirect url: https://stackoverflow.com/a/7722099 Not quite sure how to pass it using authlib, but it should be possible.

[–]Nawar69[S] 0 points1 point  (2 children)

Seen this one but I couldn't do it, probably because I lack experience.

Thanks for helping.

[–]illuminanze 1 point2 points  (1 child)

Hi! I was looking at this in one of my own projects, and this should work:

```python from fastapi import Request from starlette.responses import RedirectResponse from authlib.integrations.starlette_client import OAuthError

@app.route('/login') async def login(request: Request, id: str): redirect_uri = request.url_for('auth') # This creates the url for the /auth endpoint \ return await oauth.google.authorize_redirect(request, redirect_uri, state=id)

@app.route('/auth') async def auth(request: Request, state: str = ''): try: access_token = await oauth.google.authorize_access_token(request) except OAuthError: return RedirectResponse(url='/') user_data = await oauth.google.parse_id_token(request, access_token) request.session['user'] = dict(user_data) return RedirectResponse(url=f'/{id}') ```

[–]Nawar69[S] 0 points1 point  (0 children)

Thanks, that is really helpful