reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Read and Write Cookie with httpOnly=true.

Open Fabricio-T opened this issue 1 year ago β€’ 0 comments

All Submissions:

  • [ ] Have you followed the guidelines stated in CONTRIBUTING.md file? A little bit, but not all.
  • [X] Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

Please delete options that are not relevant.

  • [X] New feature (non-breaking change which adds functionality)

New Feature Submission:

  • [ ] Does your submission pass the tests?
  • [X] Have you linted your code locally prior to submission?

Changes To Core Features:

  • [X] Have you added an explanation of what your changes do and why you'd like us to include them?
  • [ ] Have you written new tests for your core changes, as applicable?
  • [ ] Have you successfully ran tests with your changes locally?

Description

Now its possible to set and read httpOnly Cookies.

What I did?

  • Added 'cookie' to HeaderData(state.py).
  • Added new default_endpoint(app.py) as _/set_cookie/
  • Added new set_http_only_cookie event(event.py) to easy create a httpOnly cookie.

How to use it?

  1. Create a State
  2. Use 'self.router.headers.cookie'
  3. Create a function and 'return rx.set_http_only_cookie(...)'

Like that:

class MyCustomState(rx.State):
    def get_my_cookies(self):
        myCookies = self.router.headers.cookie
        logger.info(myCookies)

    def set_cookie(self):
        return rx.set_http_only_cookie(
            key="Test",
            value="5Test5",
            max_age=1500,
        )

...

rx.button(
            "Get All Cookie",
            width="100%",
            size="3",
            radius="large",
            variant="solid",
            on_click=MyCustomState.get_my_cookies,
        ),

rx.button(
            "Set A Cookie",
            width="100%",
            size="3",
            radius="large",
            variant="solid",
            on_click=MyCustomState.set_cookie,
        ),

What can be improved?

  1. self.router.headers.cookie should be a dictionary... Possible but need more work...
  2. self.router.headers.cookie doesnt returns any cookie that has a 'path' different to '/'. It only works with a path='/'. I dont know why...
  3. Maybe can it be built-in with rx.Cookie in state.py? I couldnt find a good way to integrate with it...

Fabricio-T avatar Mar 14 '24 22:03 Fabricio-T