Hi, How can I rewrite the Set-Cookie to a custom h...
# support-questions-legacy
l
Hi, How can I rewrite the Set-Cookie to a custom header using the python SDK? I used this example for node: https://github.com/supertokens/supertokens-auth-react/blob/master/examples/with-localstorage/api-server/index.ts But how can i access the response in python (fastapi)? Here is my code:
Copy code
python
def override_session_functions(oi: RecipeInterface):
    original_create_new_session = oi.create_new_session
    original_refresh_session = oi.refresh_session
    original_get_session = oi.get_session

    async def create_new_session(request, user_id, access_token_payload, session_data, user_context):
        session = await original_create_new_session(request, user_id, access_token_payload, session_data, user_context)
        _update_headers_in_request(request)
        return session

    async def refresh_session(request, user_context):
        _update_headers_in_request(request)
        session = await original_refresh_session(request, user_context)
        _update_headers_for_response(...) # How to get response to provide it here?
        return session

    oi.create_new_session = create_new_session

    return oi
r
hey! I don't think you can access it in the create_new_session function. Maybe you should look into how you can add a middleware that runs before a response is sent and then modify the cookies / headers in there. Check this out: https://fastapi.tiangolo.com/tutorial/middleware/
l
Ok, I added a middleware to modify the response. Now the cookie is sent inside st-cookie header instead of set-cookie and is saved into the localstroage. But after login, i am getting a 401 status code for /auth/user/email/verify.
I am using the following code to rewrite the request headers and the st-cookie header is sent by the client
Copy code
python
from supertokens_python.framework.fastapi.fastapi_request import BaseRequest
from supertokens_python.recipe.session.interfaces import RecipeInterface
from config2 import SUPERTOKENS_HEADER
from starlette.requests import Request


def _update_headers_in_request(rq: BaseRequest):
    rq: Request = rq.request
    new_headers = rq.headers.mutablecopy()
    if cookie := new_headers.get(SUPERTOKENS_HEADER):
        new_headers['cookie'] = cookie
    rq._headers = new_headers
    rq.scope.update(headers=rq.headers.raw)


def override_session_functions(oi: RecipeInterface):
    original_create_new_session = oi.create_new_session
    original_refresh_session = oi.refresh_session
    original_get_session = oi.get_session

    async def create_new_session(request, user_id, access_token_payload, session_data, user_context):
        _update_headers_in_request(request)
        print('cookie', request.get_header('cookie'))
        session = await original_create_new_session(request, user_id, access_token_payload, session_data, user_context)
        return session

    async def refresh_session(request, user_context):
        _update_headers_in_request(request)
        print('cookie', request.get_header('cookie'))
        session = await original_refresh_session(request, user_context)
        return session

    async def get_session(request, anti_csrf, session_required, user_context):
        _update_headers_in_request(request)
        print('cookie', request.get_header('cookie'))
        session = await original_get_session(request, anti_csrf, session_required, user_context)
        return session

    oi.create_new_session = create_new_session
    oi.refresh_session = refresh_session
    oi.get_session = get_session

    return oi
6 Views