Login Register Firebase |
Perlu kalian tahu bahwa Firebase secara otomatis menyimpan data penggunanya secara aman (menggunakan bcrypt). Ini memisahkan data pengguna yang sensitif dari data aplikasi yang kalian kelola. Firebase hadir dengan banyak fitur penting untuk setiap aplikasi Android mulai dari otentikasi hingga membuat hosting aplikasi.
Fitur Firebase |
- Sangat mudah dan cepat untuk diimplementasikan.
- Tidak diperlukan konfigurasi server. Tidak ada Script PHP dan Tidak ada Desain Basis Data.
- Pembaruan waktu nyata tanpa menggunakan GCM.
- Dapat mulai secara gratis.
- Robust APIs untuk Javascript (termasuk beberapa kerangka kerja seperti Angular), iOS, dan Android.
- Dukungan bawaan untuk layanan otentikasi seperti Facebook, Google, dan Twitter.
- Model aturan keamanan deklaratif yang memungkinkan kita untuk menerapkan hak baca / tulis dan validasi data di seluruh struktur pohon.
- Perlu membuat indeks secara manual.
- Implementasi REST API mungkin sulit.
- Aturan validasi data tidak mendukung objek kompleks secara langsung (kalian harus memvalidasi masing-masing data secara terpisah).
Login Register Firebase |
Login Register Firebase |
#1. Mengaktifkan Firebase Auth
1. Hal pertama yang perlu kalian lakukan adalah membuka https://firebase.google.com/ dan membuat akun untuk mendapatkan akses ke console Firebase. Setelah kalian mendapatkan akses ke console, kalian bisa mulai dengan membuat project pertama.
Login Register Firebase |
2. Berikan nama package project aplikasi kalian (kalau saya com.azhar.loginfirebase) dimana kalian akan mengintegrasikan Firebase. Disini file google-services.json harus kalian unduh ketika kalian menekan tombol tambah project.
Login Register Firebase |
Login Register Firebase |
Login Register Firebase |
Login Register Firebase |
Login Register Firebase |
#2. Membuat Aplikasi Android
1. Buat project baru di Android Studio dengan cara klik File ⇒ Project Baru. Ketika diminta untuk memilih Default Activity, pilih Empty Activity dan klik next. Untuk minSDK, minimal API 21 ya. Saat kalian mengisi detail project, gunakan nama paket yang sama yang kalian berikan di console Firebase. Disini saya menggunakan package com.azhar.loginfirebase seperti pada langkah nomor 1 bagian 2.
2. Buka AndroidManifest.xml dan ubah menjadi seperti ini :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.azhar.loginfirebase">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_profile"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".SignupActivity"
android:label="@string/title_activity_login"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".ResetPasswordActivity"
android:label="@string/title_activity_reset_password"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
3. Copy & Paste file google-services.json yang sudah kalian download tadi ke folder app. Langkah ini sangat penting karena project kalian tidak akan jalan tanpa file ini.
4. Sekarang buka build.gradle yang terletak di direktori home project dan tambahkan firebase dependency :
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
5. Buka app / build.gradle dan tambahkan firebase auth dependency. Di bagian paling bawah file, tambahkan plugin terapkan: ‘com.google.gms.google-services’ :
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.1'
}
apply plugin: 'com.google.gms.google-services'
6. Tambahkan dimens.xml, colors.xml, styles.xml dan strings.xml :
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="logo_w_h">100dp</dimen>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#2196F3</color>
<color name="colorPrimaryDark">#2196F3</color>
<color name="colorAccent">#D81B60</color>
<color name="bg_login">#26ae90</color>
<color name="bg_register">#2e3237</color>
<color name="bg_main">#428bca</color>
<color name="white">#ffffff</color>
<color name="input_login">#222222</color>
<color name="input_login_hint">#999999</color>
<color name="input_register">#888888</color>
<color name="input_register_bg">#3b4148</color>
<color name="input_register_hint">#5e6266</color>
<color name="btn_login">#26ae90</color>
<color name="btn_login_bg">#eceef1</color>
<color name="lbl_name">#333333</color>
<color name="btn_logut_bg">#ff6861</color>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="app_name">Login Firebase</string>
<string name="action_sign_in_short">Register</string>
<string name="email">Email</string>
<string name="minimum_password">Password too short, enter minimum 6 characters!</string>
<string name="auth_failed">Authentication failed, check your email and password or sign up</string>
<string name="change_email">Change Email</string>
<string name="change_password">Change Password</string>
<string name="send_password_reset_email">Reset Email</string>
<string name="remove_user">Remove user</string>
<string name="new_pass">New Password</string>
<string name="title_activity_profile">Firebase</string>
<string name="title_activity_login">Sign in</string>
<string name="hint_email">Email</string>
<string name="hint_password">Password</string>
<string name="hint_name">Fullname</string>
<string name="btn_login">LOGIN</string>
<string name="btn_link_to_login">Already registered. Login Me!</string>
<string name="title_activity_reset_password">ResetPasswordActivity</string>
<string name="btn_forgot_password">Forgot your password?</string>
<string name="btn_reset_password">Reset Password</string>
<string name="btn_back"><![CDATA[<< Back]]></string>
<string name="hint_new_email">New Email</string>
<string name="btn_change">Change</string>
<string name="btn_send">Send</string>
<string name="btn_remove">Remove</string>
<string name="btn_sign_out">Sign Out</string>
<string name="lbl_forgot_password">Forgot password?</string>
<string name="forgot_password_msg">We just need your registered Email Id to sent you password reset instructions.</string>
</resources>
7. Buat activity bernama SignUpActivity.java dan tambahkan kode berikut ke layout activity_signup.xml :
package com.azhar.loginfirebase;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
/**
* Created by Azhar Rivaldi on 26/03/2018.
*/
public class SignUpActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private Button btnSignIn;
private ProgressBar progressBar;
private FirebaseAuth auth;
private ImageButton btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
auth = FirebaseAuth.getInstance();
btnSignIn = (Button) findViewById(R.id.sign_in_button);
btnSignUp = (ImageButton) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
Toast.makeText(SignUpActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(SignUpActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignUpActivity.this, MainActivity.class));
finish();
}
}
});
}
});
}
@Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_login"
tools:context=".SignUpActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="REGISTER"
android:textAlignment="center"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/layout_background"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:orientation="vertical">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:drawableLeft="@drawable/ic_mail_outline_black_24dp"
android:drawablePadding="10dp"
android:hint="EMAIL"
android:inputType="textEmailAddress"
android:padding="20dp"
android:textColorHint="#606060"
android:textSize="14sp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:drawableLeft="@drawable/ic_lock_outline_black_24dp"
android:drawablePadding="10dp"
android:hint="PASSWORD"
android:inputType="textPassword"
android:padding="20dp"
android:textColorHint="#606060"
android:textSize="14sp" />
<ImageButton
android:id="@+id/sign_up_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="end"
android:layout_marginTop="90dp"
android:layout_marginRight="30dp"
android:background="@drawable/button_background"
android:src="@drawable/ic_arrow_forward_black_24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:text="Already have an Account? "
android:textAlignment="center"
android:textColor="@android:color/black"
android:textStyle="bold" />
<Button
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_marginRight="40dp"
android:background="@drawable/button_background"
android:text="Login Here!"
android:textColor="@android:color/white" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
8. Buat activity lain bernama LoginActivity.java dan tambahkan kode berikut ke layout activity_login.xml :
package com.azhar.loginfirebase;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
/**
* Created by Azhar Rivaldi on 26/03/2018.
*/
public class LoginActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnReset;
private ImageButton btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignup = (Button) findViewById(R.id.btn_signup);
btnLogin = (ImageButton) findViewById(R.id.btn_login);
btnReset = (Button) findViewById(R.id.btn_reset_password);
auth = FirebaseAuth.getInstance();
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
}
});
btnReset.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
}
});
btnLogin.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
if (password.length() < 6) {
inputPassword.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_login"
tools:context=".LoginActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN"
android:textAlignment="center"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/layout_background"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:orientation="vertical">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:drawableLeft="@drawable/ic_mail_outline_black_24dp"
android:drawablePadding="10dp"
android:hint="EMAIL"
android:inputType="textEmailAddress"
android:padding="20dp"
android:textColorHint="#606060"
android:textSize="14sp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:drawableLeft="@drawable/ic_lock_outline_black_24dp"
android:drawablePadding="10dp"
android:hint="PASSWORD"
android:inputType="textPassword"
android:padding="20dp"
android:textColorHint="#606060"
android:textSize="14sp" />
<Button
android:id="@+id/btn_reset_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:text="@string/btn_forgot_password"
android:textColor="@color/colorPrimary" />
<ImageButton
android:id="@+id/btn_login"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="end"
android:layout_marginTop="70dp"
android:layout_marginRight="30dp"
android:background="@drawable/button_background"
android:src="@drawable/ic_arrow_forward_black_24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:text="Don't have an Account?"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textStyle="bold" />
<Button
android:id="@+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_marginRight="40dp"
android:background="@drawable/button_background"
android:text="SIGN UP"
android:textColor="@android:color/white" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
9. Buat activity lagi bernama MainActivity.java dan tambahkan kode berikut ke layout activity_main.xml :
package com.azhar.loginfirebase;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
/**
* Created by Azhar Rivaldi on 26/03/2018.
*/
public class MainActivity extends AppCompatActivity {
private Button btnChangeEmail, btnChangePassword, btnSendResetEmail, btnRemoveUser,
changeEmail, changePassword, sendEmail, remove, signOut;
private EditText oldEmail, newEmail, password, newPassword;
private ProgressBar progressBar;
private FirebaseAuth.AuthStateListener authListener;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.app_name));
setSupportActionBar(toolbar);
auth = FirebaseAuth.getInstance();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}
}
};
btnChangeEmail = (Button) findViewById(R.id.change_email_button);
btnChangePassword = (Button) findViewById(R.id.change_password_button);
btnSendResetEmail = (Button) findViewById(R.id.sending_pass_reset_button);
btnRemoveUser = (Button) findViewById(R.id.remove_user_button);
changeEmail = (Button) findViewById(R.id.changeEmail);
changePassword = (Button) findViewById(R.id.changePass);
sendEmail = (Button) findViewById(R.id.send);
remove = (Button) findViewById(R.id.remove);
signOut = (Button) findViewById(R.id.sign_out);
oldEmail = (EditText) findViewById(R.id.old_email);
newEmail = (EditText) findViewById(R.id.new_email);
password = (EditText) findViewById(R.id.password);
newPassword = (EditText) findViewById(R.id.newPassword);
oldEmail.setVisibility(View.GONE);
newEmail.setVisibility(View.GONE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.GONE);
changeEmail.setVisibility(View.GONE);
changePassword.setVisibility(View.GONE);
sendEmail.setVisibility(View.GONE);
remove.setVisibility(View.GONE);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
btnChangeEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
oldEmail.setVisibility(View.GONE);
newEmail.setVisibility(View.VISIBLE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.GONE);
changeEmail.setVisibility(View.VISIBLE);
changePassword.setVisibility(View.GONE);
sendEmail.setVisibility(View.GONE);
remove.setVisibility(View.GONE);
}
});
changeEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (user != null && !newEmail.getText().toString().trim().equals("")) {
user.updateEmail(newEmail.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Email address is updated. Please sign in with new email id!", Toast.LENGTH_LONG).show();
signOut();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to update email!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
} else if (newEmail.getText().toString().trim().equals("")) {
newEmail.setError("Enter email");
progressBar.setVisibility(View.GONE);
}
}
});
btnChangePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
oldEmail.setVisibility(View.GONE);
newEmail.setVisibility(View.GONE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.VISIBLE);
changeEmail.setVisibility(View.GONE);
changePassword.setVisibility(View.VISIBLE);
sendEmail.setVisibility(View.GONE);
remove.setVisibility(View.GONE);
}
});
changePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (user != null && !newPassword.getText().toString().trim().equals("")) {
if (newPassword.getText().toString().trim().length() < 6) {
newPassword.setError("Password too short, enter minimum 6 characters");
progressBar.setVisibility(View.GONE);
} else {
user.updatePassword(newPassword.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Password is updated, sign in with new password!", Toast.LENGTH_SHORT).show();
signOut();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to update password!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
} else if (newPassword.getText().toString().trim().equals("")) {
newPassword.setError("Enter password");
progressBar.setVisibility(View.GONE);
}
}
});
btnSendResetEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
oldEmail.setVisibility(View.VISIBLE);
newEmail.setVisibility(View.GONE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.GONE);
changeEmail.setVisibility(View.GONE);
changePassword.setVisibility(View.GONE);
sendEmail.setVisibility(View.VISIBLE);
remove.setVisibility(View.GONE);
}
});
sendEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (!oldEmail.getText().toString().trim().equals("")) {
auth.sendPasswordResetEmail(oldEmail.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Reset password email is sent!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
} else {
oldEmail.setError("Enter email");
progressBar.setVisibility(View.GONE);
}
}
});
btnRemoveUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (user != null) {
user.delete()
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Your profile is deleted:( Create a account now!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, SignUpActivity.class));
finish();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to delete your account!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
}
});
signOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});
}
//sign out method
public void signOut() {
auth.signOut();
}
@Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
@Override
public void onStart() {
super.onStart();
auth.addAuthStateListener(authListener);
}
@Override
public void onStop() {
super.onStop();
if (authListener != null) {
auth.removeAuthStateListener(authListener);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<EditText
android:id="@+id/old_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="@+id/new_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_new_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/hint_password"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
tools:ignore="InvalidImeActionId" />
<EditText
android:id="@+id/newPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/new_pass"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
tools:ignore="InvalidImeActionId" />
<Button
android:id="@+id/changeEmail"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/button_background"
android:text="@string/btn_change"
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/changePass"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/button_background"
android:text="@string/btn_change"
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/send"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/button_background"
android:text="@string/btn_send"
android:textColor="@android:color/white"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:visibility="gone" />
<Button
android:id="@+id/remove"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/button_background"
android:text="@string/btn_remove"
android:textColor="@android:color/white"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal">
<FrameLayout
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="10.0dip"
android:clickable="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/change_email_button"
style="?android:textAppearanceSmall"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="@string/change_email"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="10.0dip"
android:clickable="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/change_password_button"
style="?android:textAppearanceSmall"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="@string/change_password"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal">
<FrameLayout
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="10.0dip"
android:clickable="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/sending_pass_reset_button"
style="?android:textAppearanceSmall"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="@string/send_password_reset_email"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="10.0dip"
android:clickable="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/remove_user_button"
style="?android:textAppearanceSmall"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="@string/remove_user"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<Button
android:id="@+id/sign_out"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/button_background"
android:text="@string/btn_sign_out"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
11. Yang terakhir buat activity bernama ResetPasswordActivity.java dan tambahkan kode berikut ke layout activity_reset_password.xml :
package com.azhar.loginfirebase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
/**
* Created by Azhar Rivaldi on 26/03/2018.
*/
public class ResetPasswordActivity extends AppCompatActivity {
private EditText inputEmail;
private Button btnBack;
private FirebaseAuth auth;
private ProgressBar progressBar;
private ImageButton btnReset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reset_password);
inputEmail = (EditText) findViewById(R.id.email);
btnReset = (ImageButton) findViewById(R.id.btn_reset_password);
btnBack = (Button) findViewById(R.id.btn_back);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
auth = FirebaseAuth.getInstance();
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplication(), "Enter your registered email", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
auth.sendPasswordResetEmail(email)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Toast.makeText(ResetPasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ResetPasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
}
progressBar.setVisibility(View.GONE);
}
});
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_login"
tools:context=".ResetPasswordActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RESET PASSWORD"
android:textStyle="bold"
android:textColor="#ffff"
android:textSize="20sp"
android:textAlignment="center"
/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:orientation="vertical"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:background="@drawable/layout_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_marginBottom="100dp"
android:orientation="vertical">
<ImageView
android:layout_width="@dimen/logo_w_h"
android:layout_height="@dimen/logo_w_h"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/lbl_forgot_password"
android:textColor="@color/colorPrimary"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="@dimen/activity_horizontal_margin"
android:text="@string/forgot_password_msg"
android:textColor="@color/colorPrimary"
android:textSize="14dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_mail_outline_black_24dp"
android:layout_marginEnd="20dp"
android:id="@+id/email"
android:layout_marginStart="20dp"
android:textSize="14sp"
android:drawablePadding="10dp"
android:hint="EMAIL"
android:padding="20dp"
android:textColorHint="#606060"
android:inputType="textEmailAddress"/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="end"
android:id="@+id/btn_reset_password"
android:layout_marginTop="30dp"
android:src="@drawable/ic_arrow_forward_black_24dp"
android:background="@drawable/button_background"
android:layout_marginRight="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="20dp"
android:id="@+id/btn_back"
android:background="@drawable/button_background"
android:text="@string/btn_back"
android:textColor="@android:color/white" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Untuk file yang kurang, kalian bisa mengunduhnya melalui Github saya DISINI. Jangan lupa subscribe channel youtube saya Azhar Rivaldi, karena disana juga banyak sekali tutorial-tutorial yang tidak saya bagikan di blog ini.Silahkan kalian RUN. Ini adalah hasil permintaan ganti kata sandi yang akan diterima pengguna jika menekan tombol Reset Password di menu main :
Reset Password |
Demikian informasi yang saya bagikan untuk kalian. Jangan lupa bagikan artikel ini ke teman-teman kalian agar ikut membaca Tutorial Login, Register dan Forgot Password dengan Firebase menggunakan Android Studio ini. Subscribe juga di blog Rivaldi 48 ini agar kalian mendapatkan notifikasi saat Admin update artikel terbaru. Follow Instagram Admin @azhardvls_. Semoga kalian lebih nyaman dan mudah dalam mengakses Blog Rivaldi 48 dimanapun kalian berada. Terima Kasih...