skip to Main Content

I’m building this multi-platform front-end using flutter for my webpage and I get an error when connecting to my Laravel back-end.

I’m tracing the issue to the request on flutter, which I’m using Dio ^5.1, since I can log in 200 status code on my Laravel back-end, so all I’m missing is to confirm 200 on flutter and proceed with login operation.

My login_controller.dart is

void submitLogin() async {
    try {
      bool isValid = await validateEmail(inputUser.text);
      if (isValid) {
        await api.call(
          {
            "email": inputUser.text,
            "password": inputPass.text,
          },
          endpoint: '/login',
          method: 'POST',
        );
      } else {
        showSnackbar(msg: "Algo errado, favor validar os dados inseridos.", type: 0);
      }
    } catch (e) {
      showSnackbar(msg: e.toString(), type: 0);
      Get.back();
    }
  }

Nothing wrong in here as far as I’m aware,

my api.call function is where lies the problem:
print(postResponse) can never be reached and I’m getting 419 bad request on catch DioError everytime, even with successful laravel login.

Future<Response?> call(Map<String, dynamic> data, {required String endpoint, required String method}) async {
    try {
      if (token.value == "") {
        await getToken();
      }
      options.headers.addIf(token.value != "", 'token', token.value);
      if (method == "GET") {
        Response getResponse = await dio.get(endpoint);
        print(getResponse.statusCode);
        return getResponse;
      }
      if (method == "POST") {
        Response postResponse = await dio.post(endpoint, data: jsonEncode(data.values));
        print(postResponse.statusCode);
        return postResponse;
      }
    } on DioError catch (e) {
      print(e.response);
      showSnackbar(msg: e.toString(), type: 0);
    }
  }

Is this enough for the question? Did I miss something? Thanks in advance.

I’m running local laravel app with –host = IPv4 address
and my BaseUrl on Flutter api is IPv4:8000

did this to be sure there is no error related to addresses like 127.0.0.1 and 10.0.0.2, but still Dio returns 419 bad request..

I can also log in successfully with POSTMAN.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, I managed to work around my issue, as I'm using sanctum authentication in Laravel, it was yielding an error on HttpControllersAuthAuthenticatedSessionController.php

    So I reworked my store function to return custom status `

    public function store(LoginRequest $request): Response
    {
        try{
            $request->authenticate();
            return response(['status' => 'success']);
        } catch(e){
            return response(['status' => 'failure']);
        }
    
        
    }`
    

    this way I can work with my postResponse in flutter, reminding that both will yield a 200 http status code, so I'll rely on text return 'status' to check for authentication status instead


  2. When extracting routes from Laravel, put them in the api file. 419 error occurs because you do not send csrf tokens. If you are already on api.php route, remove the web middleware from the app>providers>routeserviceprovider file, it will work +1 I would appreciate if you do

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search