how to create a session for remember me (login authentication) in android


Last Updated:

  1. Amalthomas

    Amalthomas Member This Topic's Starter

    Joined:
    Mar 18, 2013
    Messages:
    10
    Likes Received:
    0
    I have developed a code for login/register authentication. I want to add the remember me feature in my app. I am new to android.i want to know clearly about session for remember me. can you guide me right direction...?
    Here is my code for login:

    public class LoginActivity extends Activity {
    Button btnLogin;
    Button btnLinkToRegister;
    EditText inputEmail;
    EditText inputPassword;
    TextView loginErrorMsg;
    //CheckBox chkRemember;
    // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_UID = "uid";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_CREATED_AT = "created_at";

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    // Importing all assets like buttons, text fields
    inputEmail = (EditText) findViewById(R.id.loginEmail);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    //chkRemember =(CheckBox)findViewById(R.id.checkBox1);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
    String email = inputEmail.getText().toString();
    String password = inputPassword.getText().toString();
    UserFunctions userFunction = new UserFunctions();
    Log.d("Button", "Login");
    JSONObject json = userFunction.loginUser(email, password);

    // check for login response
    try {
    if (json.getString(KEY_SUCCESS) != null) {
    loginErrorMsg.setText("");
    String res = json.getString(KEY_SUCCESS);
    if(Integer.parseInt(res) == 1){
    // user successfully logged in
    // Store user details in SQLite Database
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    JSONObject json_user = json.getJSONObject("user");

    // Clear all previous data in database
    userFunction.logoutUser(getApplicationContext());
    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));

    // Launch Dashboard Screen
    Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

    // Close all views before launching Dashboard
    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(dashboard);

    // Close Login Screen
    finish();
    }else{
    // Error in login
    loginErrorMsg.setText("Incorrect username/password");
    }
    }
    } catch (JSONException e) {
    e.printStackTrace();
    }
    }
    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
    Intent i = new Intent(getApplicationContext(),
    RegisterActivity.class);
    startActivity(i);
    finish();
    }
    });
    }
     

    Advertisement
  2. ayazshakeeb

    ayazshakeeb New Member

    Joined:
    Apr 10, 2013
    Messages:
    1
    Likes Received:
    0
    use sharedPreference Tutorial u'll get the answer
     
  3. Amalthomas

    Amalthomas Member This Topic's Starter

    Joined:
    Mar 18, 2013
    Messages:
    10
    Likes Received:
    0
    @ayazshakeeb, can u guide me to use sharedPreference. i am new to android.
     
  4. iowabowtech

    iowabowtech root@android:/ #

    Joined:
    May 2, 2010
    Messages:
    12,051
    Likes Received:
    8,512
    Thread moved to the App Development forum. Hopefully you can find the answer you seek here Amalthomas. :)

    Cheers,
    IBT
     
  5. shredcode

    shredcode Active Member

    Joined:
    Mar 31, 2013
    Messages:
    30
    Likes Received:
    3
    I agree, sharedPreferences is the answer to your problem. Here is a link to the api docs: link

    Here is also an easy code sample for you to refer to for setting and retrieving a String preference:

    [HIGH]
    public void setPassword(Context c, String password) {
    SharedPreferences settings = c.getSharedPreferences(YOUR_TAG, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("appPassword", password);
    editor.commit();
    }

    public String getPassword(Context c) {
    SharedPreferences settings = c.getSharedPreferences(YOUR_TAG, Context.MODE_PRIVATE);
    String password = settings.getString("appPassword", "");
    return password;
    }
    [/HIGH]
     
  6. Amalthomas

    Amalthomas Member This Topic's Starter

    Joined:
    Mar 18, 2013
    Messages:
    10
    Likes Received:
    0
    @shredcode, Thank you
     
  7. Amalthomas

    Amalthomas Member This Topic's Starter

    Joined:
    Mar 18, 2013
    Messages:
    10
    Likes Received:
    0
    i wrote the above code for login/registration, i store all my details like name, email , password in MySQL by using PHP. i want to add "remember me" feature in my code. Any one help me to add remember me (my email and password) when i enable the checkbox. but here want to retrieve the data from MySQL. guide me. thanks in advance
    .....
     
  8. shredcode

    shredcode Active Member

    Joined:
    Mar 31, 2013
    Messages:
    30
    Likes Received:
    3
    What is your table structure? Are you asking for the SQL / php code to query the db?
     

Share This Page

Loading...