Android Login Session System dengan Verifikasi Email (3)

Join Komunitas TipsKoding.com
  • Buka file HomeActivity.java dan lakukan perubahan sesuai kode berikut. Dalam class HomeActivity kita akan mengambil informasi pengguna yang berhasil login dari SQLite dan menampilkannya.

[javascript]
package com.tipskoding.login;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import com.tipskoding.login.helper.DatabaseHandler;
import com.tipskoding.login.helper.Functions;
import com.tipskoding.login.helper.SessionManager;
import java.util.HashMap;
import java.util.Map;
public class HomeActivity extends AppCompatActivity {
private static final String TAG = HomeActivity.class.getSimpleName();
private TextView txtName, txtEmail;
private Button btnChangePass, btnLogout;
private SessionManager session;
private DatabaseHandler db;
private ProgressDialog pDialog;
private HashMap<String,String> user = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
txtName = (TextView) findViewById(R.id.name);
txtEmail = (TextView) findViewById(R.id.email);
btnChangePass = (Button) findViewById(R.id.change_password);
btnLogout = (Button) findViewById(R.id.logout);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
db = new DatabaseHandler(getApplicationContext());
user = db.getUserDetails();
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
// Fetching user details from database
String name = user.get("name");
String email = user.get("email");
// Displaying the user details on the screen
txtName.setText(name);
txtEmail.setText(email);
// Hide Keyboard
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
init();
}
private void init() {
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logoutUser();
}
});
btnChangePass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(HomeActivity.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.change_password, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setTitle("Change Password");
dialogBuilder.setCancelable(false);
final TextInputLayout oldPassword = (TextInputLayout) dialogView.findViewById(R.id.old_password);
final TextInputLayout newPassword = (TextInputLayout) dialogView.findViewById(R.id.new_password);
dialogBuilder.setPositiveButton("Change", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// empty
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog alertDialog = dialogBuilder.create();
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(oldPassword.getEditText().getText().length() > 0 &&
newPassword.getEditText().getText().length() > 0){
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
} else {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
oldPassword.getEditText().addTextChangedListener(textWatcher);
newPassword.getEditText().addTextChangedListener(textWatcher);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
final Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setEnabled(false);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = user.get("email");
String old_pass = oldPassword.getEditText().getText().toString();
String new_pass = newPassword.getEditText().getText().toString();
if (!old_pass.isEmpty() && !new_pass.isEmpty()) {
changePassword(email, old_pass, new_pass);
dialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Fill all values!", Toast.LENGTH_SHORT).show();
}
}
});
}
});
alertDialog.show();
}
});
}
private void logoutUser() {
session.setLogin(false);
// Launching the login activity
Functions logout = new Functions();
logout.logoutUser(getApplicationContext());
Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void changePassword(final String email, final String old_pass, final String new_pass) {
// Tag used to cancel the request
String tag_string_req = "req_reset_pass";
pDialog.setMessage("Please wait…");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
Functions.RESET_PASS_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Reset Password Response: " + response);
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
Toast.makeText(getApplicationContext(), jObj.getString("message"), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), jObj.getString("message"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Reset Password Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<>();
params.put("tag", "change_pass");
params.put("email", email);
params.put("old_password", old_pass);
params.put("password", new_pass);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
// Adding request to volley request queue
strReq.setRetryPolicy(new DefaultRetryPolicy(5 * DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 0, 0));
strReq.setRetryPolicy(new DefaultRetryPolicy(0, 0, 0));
MyApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
[/javascript]

Okay, sekarang saatnya kita coba menjalankan aplikasi. Sebagai catatan untuk menjalankan proses verifikasi melalui email maka kamu harus menyimpan data pengguna dan file php dalam real server atau hosting provider. Jika tidak maka proses hanya dilakukan secara manual dengan melihat kode OTP dalam database MySQL di server lolalhost.

Berikut adalah hasil akhir dari aplikasi login system session dengan verifikasi email.

     

Kalau ada pertanyaan silahkan ajukan di kolom komentar, kunjungi juga channel yotube TipsKoding, Fanspage Facebook TipsKoding dan Instagram @tipskoding

Tetap semangat untuk terus eksplorasi, jangan lupa bahagia hari ini. 🙂
#androidLoginSystem #keepLearning #keepSharing #tipsKoding

Join Komunitas TipsKoding.com

Facebook Comments

POST A COMMENT.