Cookies are not stored properly in ios simulator

Viewed 415

I am using Flask, flask-jwt-extended, and React-native (with Expo) for a mobile application. When I use web-browser simulation or Postman, all my cookies are setup correctly. However, when I use the ios simulator from Xcode, none of the cookies are stored.

Environment:
  Flask Backend: 127.0.0.1:5000
  React-Native Front: 127.0.0.1:19006

Here is my Flask config:

    JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", 'local-secret')
    JWT_TOKEN_LOCATION = ['cookies']
    JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(seconds=1800)
    JWT_COOKIE_SECURE = False
    CORS_HEADERS = "Content-Type"
    JWT_REFRESH_TOKEN_EXPIRES = datetime.timedelta(days=15)
    JWT_COOKIE_CSRF_PROTECT = True  # set_refresh_cookies() will now also set the non-httponly CSRF cookies
    JWT_CSRF_CHECK_FORM = True
    JWT_ACCESS_CSRF_HEADER_NAME = "X-CSRF-TOKEN-ACCESS"
    JWT_REFRESH_CSRF_HEADER_NAME = "X-CSRF-TOKEN-REFRESH"
    SSL_REDIRECT = False
jwt = JWTManager()
app = Flask(__name__)
app.config.from_object(APP_CONFIG[CONFIG_ENV])
cors = CORS(app, resources={r"/*": {"origins": "http://127.0.0.1:19006"}}, supports_credentials=True)
APP_CONFIG[CONFIG_ENV].init_app(app)
jwt.init_app(app)

Here is how I store cookies (classic, and working with postman or normal web browser):

access_token = create_access_token(identity = token_identity)
refresh_token = create_refresh_token(identity = token_identity)
resp = jsonify({"access_token": access_token, "refresh_token": refresh_token})
set_access_cookies(resp, access_token)
set_refresh_cookies(resp, refresh_token)

Here is how I make requests with the Fetch lirbary:

let response = await fetch(URL + "/login", {
      body: form_data,
      method: "post",
      credentials: "include",
    })

However, whenever I am using the ios Simulator (127.0.0.1:19006) with react-native to make requests, cookies are never stored. Any ideas where the problem can come from?

1 Answers

Hope this will help you save some time.

While I am in "http://127.0.0.1:19006" with my classic browser, I am using the following domain with my device: "http://192.168.0.13:19006". But, adding this domain was not enough. I also add to add "http://192.168.0.13" in the domains. I'm not too sure why, but it's now working: the cache works properly in my device.

Here is the line of code that I modified:

cors = CORS(app, resources={"/*": {"origins": ["http://127.0.0.1:19006", "http://192.168.0.13:19006", "http://192.168.0.13"]}}, supports_credentials=True)
Related