how to display the wrong password or username with these conditions

Viewed 49

I have a problem, to determine the condition when the email and username entered is wrong it will display "username/password is wrong". Guess how to determine the conditions that match my source below how?

void login(BuildContext context) async {
    AppFocus.unfocus(context);
    if (loginFormKey.currentState!.validate()) {
      final res = await apiRepository.login(
        LoginRequest(
          email: loginEmailController.text,
          password: loginPasswordController.text,
        ),
      );

      final prefs = Get.find<SharedPreferences>();

      if (res!.accessToken.isNotEmpty) {
        prefs.setString(StorageConstants.token, res.accessToken);
        Get.toNamed(Routes.HOME);
        Get.snackbar("Berhasil", "Selamat Berhasil Login");
      }
    }
  }

Postman Success Login enter image description here

Postman Failed Login enter image description here Hope you guys can help me, thanks

2 Answers

You can manage it on the basis of the response.statusCode , like when the status code is 200 you can redirect it to next screen and when the status code is not 200 you can show the error in flutter toast "Username/Password are incorrect".

if(response.statusCode == 200){
// code here for correct credential 
} else{
// code here for wrong credential like show toast that wrong password
}

this api is not good because of it showing password error in 404 bad request

Related