2

Currently, I have enabled 'keep logging in' function if the user log in once successfully. However, I still want to make a 'remember me' checkbox to save the success login information for user. Can anyone please help me with this?

Need: a checkbox that enables the user to store email and password if the user logged in once successfully.

Code is shown below:

  signIn(String email, pass) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String url = ServerDetails.ip +
        ':' +
        ServerDetails.port +
        ServerDetails.api +
        'login';
    Map<String, String> headers = {"Content-type": "application/json"};
    var data = jsonEncode({
      'email': email,
      'password': pass,
      'token': FirebaseNotifications.fcmtoken
    });
    var jsonResponse = null;
    var response = await http.post(url, headers: headers, body: data);

    if (response.statusCode == 200) {
      jsonResponse = json.decode(response.body);
      if (jsonResponse != null) {
        setState(() {
          _isLoading = false;
        });
        sharedPreferences.setString("token", jsonResponse['token']);
        sharedPreferences.setString(
            "token_expire_date", jsonResponse['token_expire_date']);
        Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(builder: (BuildContext context) => MainPage()),
            (Route<dynamic> route) => false);
      }
    } else {
      setState(() {
        _isLoading = false;
      });
      Widget okButton = FlatButton(
          child: Text("OK"),
          onPressed: () {
            Navigator.push(
                context, MaterialPageRoute(builder: (context) => MainPage()));
          });
      setState(() {
        AlertDialog alert = AlertDialog(
          title: Text("Error message"),
          content: Text("Oops! The password is wrong or the email is invalid."),
          actions: [
            okButton,
          ],
        );
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return alert;
          },
        );
      });
      print(response.headers);
      print(response.body);
    }
  }
Fred
  • 73
  • 2
  • 7
  • what kind of info do you want to save? – Creator Oct 18 '20 at 16:34
  • @Creator Save both the username(which is presented as email in this case) and password if the user click the checkbox and loged in successfully once. – Fred Oct 18 '20 at 16:35
  • I would not recommend you to do this because this will make your app highly vulnerable to hackers. And your user will not feel safe to use your app. I recommend you to only save username or email. – Creator Oct 18 '20 at 16:48
  • @Creator Thanks for your remind! But I still want to know how to do it in my case. – Fred Oct 18 '20 at 16:51
  • First create a remember me button. Then A list of maps. which contains user name and passwords of every user login into your app. everytime a new user login save his info. – Creator Oct 18 '20 at 17:06

1 Answers1

0

Of course, you can create a simple checkbox for remember me. In the login button, you can check if this checkbox is checked. If it is, you can set email & password in shared_preferences.

Next time, when the user comes again you can get these fields automatically from shared_preferences.

Here is an example.

Akif
  • 7,098
  • 7
  • 27
  • 53