Home > database >  Login from Flutter into Django Framework always showing Invalid Password
Login from Flutter into Django Framework always showing Invalid Password

Time:11-08

I am trying to login from Flutter to my Django Rest Framework but I am always getting the same error Invalid Password although the credentials are correct.

Here is the login_request_model.dart:

class LoginRequestModel {
  LoginRequestModel({
    this.username,
    this.email,
    this.password,
  });
  late final String? username;
  late final String? email;
  late final String? password;

  LoginRequestModel.fromJson(Map<String, dynamic> json) {
    username = json['username'];
    email = json['email'];
    password = json['password'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['username'] = username;
    _data['email'] = email;
    _data['password'] = password;
    return _data;
  }
}

Here is the login_page.dart:


class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool isApiCallProcess = false;
  bool hidePassword = true;
  GlobalKey<FormState> globalFormKey = GlobalKey<FormState>();
  String? userName;
  String? password;

  @override
  void initState() {
    super.initState();
  }

Rest of the login_page.dart where login is inserted:

          Center(
            child: FormHelper.submitButton(
              "Login",
              () {
                if (validateAndSave()) {
                  setState(() {
                    isApiCallProcess = true;
                  });

                  LoginRequestModel model = LoginRequestModel(
                    username: userName,
                    password: password,
                  );

                  APIService.login(model).then(
                    (response) {
                      setState(() {
                        isApiCallProcess = false;
                      });

                      if (response) {
                        Navigator.pushNamedAndRemoveUntil(
                          context,
                          '/home',
                          (route) => false,
                        );
                      } else {
                        FormHelper.showSimpleAlertDialog(
                          context,
                          Config.appName,
                          "Invalid Username/Password !!",
                          "OK",
                          () {
                            Navigator.of(context).pop();
                          },
                        );
                      }
                    },

Here is the login_response_model.dart:

class LoginResponse {
  dynamic? key;
  List<dynamic>? non_field_errors;
  LoginResponse({this.key, this.non_field_errors});

  factory LoginResponse.fromJson(mapOfBody) {
    return LoginResponse(
      key: mapOfBody['key'],
      non_field_errors: mapOfBody['non_field_errors'],
    );
  }
}

LoginResponseModel loginResponseJson(String str) =>
    LoginResponseModel.fromJson(json.decode(str));

class LoginResponseModel {
  dynamic? key;
  List<dynamic>? non_field_errors;
  LoginResponseModel({this.key, this.non_field_errors});

  LoginResponseModel.fromJson(mapOfBody) {
    key:
    mapOfBody['key'];
    non_field_errors:
    mapOfBody['non_field_errors'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['key'] = key;
    _data['non_field_errors'] = non_field_errors;
    return _data;
  }
}

class Data {
  Data({
    required this.username,
    required this.email,
    required this.date,
    required this.id,
    required this.key,
  });
  late final String username;
  late final String email;
  late final String date;
  late final String id;
  late final String key;

  Data.fromJson(Map<String, dynamic> json) {
    username = json['username'];
    email = json['email'];
    date = json['date'];
    id = json['id'];
    key = json['key'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['username'] = username;
    _data['email'] = email;
    _data['date'] = date;
    _data['id'] = id;
    _data['key'] = key;
    return _data;
  }
}

In my django rest framework I am getting Bad Request: /api/dj-rest-auth/login/

My question:

As there is not specific error that I am trying to fix, I want to know how can I spot the error by debugging I need an example of how to try and catch error. Is there something wrong with my LoginRequestModel or LoginResponseModel

CodePudding user response:

it is usually a mismatch error, try to check the following

  1. check if the data you are sending from your flutter side, is being recieved accurately from the django side. like for example, if the backend accepts the data like this..

    {
       "username" : "user",
       "password" : "password"
    }
    

and you are sending the data from flutter side like this..

   {
      "email" : "user",
      "password" : "password"
   }

it expects a key of username and is receiving an email, then its a bad request

  1. I'm not sure about django being case sensitive, but sending the data as Username instead of username.. this results in a bad request response.

so in short, to debug this.. convert the data you are sending to json and see how the data is being sent.. and check from the django side how its being received.

  • Related