Flutter web cors issue access to api

Viewed 124

So i have this request

Future fetchMembership(String member_id) async {
    var token = await session.get_token();
    final response = await http.get(
      Uri.parse(
          ApiConst.get_membership_v1.toString() + "member_id/" + member_id),
      headers: {
        "Token": token,
      },
    );
    if (response.statusCode == 200) {
      return response;
    } else {
      throw Exception('error fetching posts');
    }
  }

and here is my api. I'm using codeigniter

use chriskacerguis\RestServer\RestController;

class Subscription extends RestController
{

    function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
        if (!isset($headers['Token'])) {
            $this->response([
                'status' => false,
                'message' => "Token is required",
                'header' => $headers
            ], 401);
        }
        if (array_key_exists('Token', $headers) && !empty($headers['Token'])) {
            $decodedToken = AUTHORIZATION::validateToken($headers['Token']);
            if ($decodedToken == false || empty($decodedToken) || $decodedToken != "false") {
                $this->response([
                    'status' => false,
                    'message' => "Token is required",
                    'header' => $headers
                ], 401);
            }
        }

        
    }

    function subscription_get()
    {
        $result = "...";

        if ($result) {
            $this->response([
                'status' => true,
                'data' => $result,
                'count' => count($result)
            ], 200);
        }
        $this->response([
            'status' => true,
            'data' => "Something went wrong",
            $member_id
        ], 500);
    }
}

I get this error in console

enter image description here

As you can see from my screnshot above, there is no CORS error when i do a login. But when i do another api request after login to get additional data, i get the erorr , Did i miss something ?

1 Answers
if ($decodedToken == false || empty($decodedToken) || $decodedToken != "false")

I'm wondering, is that last condition ($decodedToken != "false") really meant to be that? A successfully decoded token will always equal the string "false"?

Related