Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"null" Session Id when using RedisBackend and InMemoryBackend #7

Closed
Grustamli opened this issue Dec 30, 2021 · 12 comments
Closed

"null" Session Id when using RedisBackend and InMemoryBackend #7

Grustamli opened this issue Dec 30, 2021 · 12 comments

Comments

@Grustamli
Copy link

Session ID is shown as null when I use RedisBackend. It is also same for InMemoryBackend. However, Cookie backend works fine.

@Grustamli
Copy link
Author

The issue seems to arise on this line:

session_id = connection.cookies.get(self.session_cookie, None)

I am using Fastapi, btw.

@alex-oleshkevich
Copy link
Owner

can you share a code example for more context?

@Grustamli
Copy link
Author

Please see below.

app = FastAPI()
app.include_router(router) # Simple API router

app.add_middleware(SessionMiddleware, backend=RedisBackend(settings.redis_url), autoload=True)

I checked the generate_id() function and it is working as intended. However, as I posted above self.session_cookie returns null

@Grustamli
Copy link
Author

Sorrry not the self.session_cookie but connection.cookies.get(self.session_cookie, None) returns null.

@Grustamli Grustamli changed the title Null Session Id when using RedisBackend and InMemoryBackend "null" Session Id when using RedisBackend and InMemoryBackend Jan 5, 2022
@alex-oleshkevich
Copy link
Owner

Also attach an example of your view function where you call this code.

The session id will be generated by the session middleware if empty. This happens when response is being sent.

@Grustamli
Copy link
Author

This is the only view where I write to the session.

@router.post("/password/request", status_code=status.HTTP_202_ACCEPTED)
async def reset_request(
    request: Request,
    background_tasks: BackgroundTasks,
    email: str = Body(..., embed=True),
):
    otp = generate_otp()
    otp_expires_at = dt.datetime.now() + dt.timedelta(seconds=settings.otp_valid_for)

    request.session[SESSION_KEY_OTP] = otp
    request.session[SESSION_KEY_OTP_EXPIRES_AT] = otp_expires_at.timestamp()

    background_tasks.add_task(email_otp, email, otp)

@alex-oleshkevich
Copy link
Owner

Looks like that is all fine. The session ID will be generated once the response is sent and will be available on the next request.
In the browser's dev tools, do you see a random string in place of the "session" cookie or just None?

@Grustamli
Copy link
Author

Grustamli commented Jan 5, 2022

When I use CookieBackend I see session key and value. However, in any of the other backends cookie is returned empty. I use Insomnia to test it.
It should generate an ID on first write but when I debug the RedisBackend, session ID is saved as an actual string with a "null" value. This is because of the line I posted above where connection.cookies.get(self.session_cookie, None) picks null string value from an empty cookie. Could you please check it on your side, too?

@alex-oleshkevich
Copy link
Owner

yes, sure. thanks for report.

@alex-oleshkevich
Copy link
Owner

alex-oleshkevich commented Jan 5, 2022

  1. can you also make sure that is it not related to Starlette 0.15.0 breaks SessionMiddleware by adding ASGI Path for Subroutes using Mount encode/starlette#1261 ?
  2. run examples/redis.py locally on a different port, goto /set, and check if the cookie is set.

I tested locally and all works fine.

When I use CookieBackend I see session key and value

CookieBackend is different. The ID here is encrypted cookie content so it is always available.

@Grustamli
Copy link
Author

I will check and report back. Thank you!

@Grustamli
Copy link
Author

After checking on a different machine, RedisBackend works fine. Session ID is set as expected. Now, I have to investigate why it didn't work before. Sorry for bothering )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants