setelah kita menghubungkan projek android kita denga firebase, langkah selanjutnya adalah
mengenable Email/Password yang tedapat pada Autentication – sign in method pada
console firebase.
private FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
@Override public void onStart() { super.onStart(); FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); } Langkah selanjutnya adalah membuat signin method (letakkan dibawah onCreate Method) private void signIn(String email, String password) { Log.d(TAG, "signIn:" + email); if (!validateForm()) { return; } showProgressDialog(); mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { Log.d(TAG, "signInWithEmail:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { Log.w(TAG, "signInWithEmail:failure", task.getException()); Toast.makeText(EmailPasswordActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } if (!task.isSuccessful()) { mStatusTextView.setText(R.string.auth_failed); } hideProgressDialog(); } }); } private boolean validateForm() { boolean valid = true; String email = mEmailField.getText().toString(); if (TextUtils.isEmpty(email)) { mEmailField.setError("Required."); valid = false; } else { mEmailField.setError(null); } String password = mPasswordField.getText().toString(); if (TextUtils.isEmpty(password)) { mPasswordField.setError("Required."); valid = false; } else { mPasswordField.setError(null); } return valid; }
@Override public void onClick(View v) { int i = v.getId(); if (i == R.id.email_create_account_button) { createAccount(mEmailField.getText().toString(), mPasswordField.getText().toString()); } else if (i == R.id.email_sign_in_button) { try { Class aClass = Class.forName("com.google.firebase.quickstart.auth.GoogleSignInActivity"); Object o = aClass.newInstance(); Method method = aClass.getMethod("signIn"); method.setAccessible(true); method.invoke(o); } catch (Exception e) { e.printStackTrace(); } } else if (i == R.id.sign_out_button) { signOut(); } else if (i == R.id.verify_email_button) { sendEmailVerification(); } }
0 Comment:
Post a Comment