0

Here's my scenario:

A user signs up to the app by simply choosing a username that's not taken and putting a pass When the user signs in to the app, his userID (primary key of user in my database) is stored locally and is used for all my services.

I am using SQL server for the database.

What i am trying to accomplish is that once a user signs in. I want to keep him signed in but i don't know how since once the app starts up again after being closed, userid won't even be initialized (until the user logs in) so I can't know which user should i sign in automatically in the app. How can i get around this (i prefer to do this from the frontend side if possible, if not then how can i do it backend wise?)

1 Answers1

1

You can use shared preferences to save the user ID, because you are using an SQL Server DB perhaps you will provide also a token and need to store the token too.

On the first login of the user save the data after the login is done successfully.

import 'package:shared_preferences/shared_preferences.dart';

 var _prefs = SharedPreferences.getInstance();

  /// ----------------------------------------------------------
  /// Method that saves the token (if you need the token) and userID in Shared Preferences
  /// ----------------------------------------------------------
  Future<bool> _setMobileToken(String token , String username, String userID) async {
    final SharedPreferences prefs = await _prefs;
    prefs.setString("username", username);
    prefs.setString("userID", userID);
    return prefs.setString(_storageKeyMobileToken, token);
  }

After that when you close the app and open in again you just need to read your data from Shared Preferences.

    // Async function that calls getSharedPreferences
  awaitSharedPref() async {
    await getSharedPreferences();
  }


  // Get the username and token from SharedPreferences and check if the token is valid
  awaitToken() async {
    await functions.getMobileToken();

    if (token != "" && token != null && username!= "" && username!= null ) {

        // If you use any token try to make a validation if was expired or not 
        //_presenter.validateToken(token, username.toString());

        // If not redirect the user directly on the home page 
        Navigator.of(_ctx).pushReplacementNamed("/home");

    } else {
      Navigator.of(context).pushNamed("/login");
    }

  }

If you want you can also add a "Remember me" checkbox, so even if he log out from the app the next time he will open it, can click directly the "Login" button with username and pass autocomplete.

    final rememberMe = CheckboxListTile(
  value: checkValue,
  onChanged: _onChanged,
  title: new Text("Remember me", style: new TextStyle(
      color: Colors.blueGrey[600], fontStyle: FontStyle.italic),),
  controlAffinity: ListTileControlAffinity.leading,
);


_onChanged(bool value) async {
    sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      checkValue = value;
      sharedPreferences.setBool("check", checkValue);
      sharedPreferences.setString("username", username.text);
      sharedPreferences.setString("password", password.text);
      getCredential();
    });
}
nike
  • 715
  • 2
  • 8
  • 14