Membuat Register User dengan Firebase di Android Studio
Selamat
siang Register User berguna untuk mengauthentikasi pengguna aplikasi kita,
Register User biasanya terdapat pada aplikasi jejaring social ataupun
ecommerce. Register User bisa dibuat
dengan menggunakan database MySQL, SQLite, MongoDB dll, tapi kali ini kita akan
menggunakan Firebase, sudah disinggung sebelumnya kalau firebase ini
menyediakan fungsi authentikasi yang lengkap, pada postingan sebelumnya kita
telah membuat login user dengan Firebase, nah sekarang kita akan membuat form
registernya. untuk membuatnya hampir sama dengan membuat form login bedanya
pada register kita memasukan data user ke database kalau login hanya mencocokan
data. langsung saja untuk memulainya buat dulu form registernya
xml version="1.0" encoding="utf-8"?>
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"
tools:context="com.giviews.giviewsmessenger.RegisterActivity">
android:id="@+id/main_page_toolbar"
layout="@layout/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="NotSibling" />
android:id="@+id/reg_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="Name ..."
android:inputType="textPersonName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="26dp"
app:layout_constraintTop_toBottomOf="@+id/textView" />
android:id="@+id/reg_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="Email ..."
android:inputType="textEmailAddress"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/reg_name" />
android:id="@+id/reg_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password ..."
android:inputType="textPassword"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/reg_email"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
android:id="@+id/reg_create_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Acooount"
android:background="@color/colorAccent"
android:padding="10dp"
android:textColor="@color/colorWhite"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/reg_password"
android:layout_marginEnd="8dp" />
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="Create a New Account"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="26dp"
app:layout_constraintTop_toBottomOf="@+id/main_page_toolbar"
android:layout_marginEnd="8dp" />
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"
tools:context="com.giviews.giviewsmessenger.RegisterActivity">
layout="@layout/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="NotSibling" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="Name ..."
android:inputType="textPersonName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="26dp"
app:layout_constraintTop_toBottomOf="@+id/textView" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="Email ..."
android:inputType="textEmailAddress"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/reg_name" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password ..."
android:inputType="textPassword"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/reg_email"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
android:id="@+id/reg_create_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Acooount"
android:background="@color/colorAccent"
android:padding="10dp"
android:textColor="@color/colorWhite"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/reg_password"
android:layout_marginEnd="8dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="Create a New Account"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="26dp"
app:layout_constraintTop_toBottomOf="@+id/main_page_toolbar"
android:layout_marginEnd="8dp" />
Sambungkan
projek android anda dengan firebase, kemudian buka firebase console aktifkan
sigin method mengguanakan email
dan untuk
script proses registernya seperti berikut, pada script berikut jika User
berhasil didaftarkan maka akan diintent ke MainActivity untuk itu buat lagi
Activity Main nya.
package com.giviews.giviewsmessenger; import android.app.ProgressDialog; import android.content.Intent; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; 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; import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException; import com.google.firebase.auth.FirebaseAuthUserCollisionException; import com.google.firebase.auth.FirebaseAuthWeakPasswordException; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.iid.FirebaseInstanceId; import java.util.HashMap; public class RegisterActivity extends AppCompatActivity { private EditText mDisplayName; private EditText mEmail; private EditText mPassword; private Button mCreateBtn; private Toolbar mToolbar; private ProgressDialog mProgress; private FirebaseAuth mAuth; private DatabaseReference mDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); mAuth = FirebaseAuth.getInstance(); mDatabase = FirebaseDatabase.getInstance().getReference().child("Users"); mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setTitle("Register"); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mDisplayName = (EditText) findViewById(R.id.reg_name); mEmail = (EditText) findViewById(R.id.reg_email); mPassword = (EditText) findViewById(R.id.reg_password); mCreateBtn = (Button) findViewById(R.id.reg_create_btn); mProgress = new ProgressDialog(this); mCreateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String displayname = mDisplayName.getText().toString().trim(); String email = mEmail.getText().toString().trim(); String password = mPassword.getText().toString().trim(); if (!TextUtils.isEmpty(displayname) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) { mProgress.setTitle("Registering User"); mProgress.setMessage("Please wait while we create your account..."); mProgress.setCanceledOnTouchOutside(false); mProgress.show(); register_user(displayname, email, password); } } }); } private void register_user(final String displayname, String email, String password) { mAuth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener() { @Override public void onComplete(@NonNull Tasktask) { // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { mProgress.hide(); String error = ""; try { throw task.getException(); } catch (FirebaseAuthWeakPasswordException e) { error = "Weak Password"; } catch (FirebaseAuthInvalidCredentialsException e) { error = "Invalid Email"; } catch (FirebaseAuthUserCollisionException e) { error = "Existing account"; } catch (Exception e) { error = "Unknown Error!"; e.printStackTrace(); } Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show(); }else { FirebaseUser currentUser = mAuth.getCurrentUser(); String uid = currentUser.getUid(); mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(uid); String device_token = FirebaseInstanceId.getInstance().getToken(); HashMap, String> userMap = new HashMap , String>(); userMap.put("name", displayname); userMap.put("status", "Hi there, I'm using giviews"); userMap.put("image", "default"); userMap.put("thumb_image", "default"); userMap.put("device_token", device_token); mDatabase.setValue(userMap).addOnCompleteListener(new OnCompleteListener () { @Override public void onComplete(@NonNull Tasktask) { mProgress.dismiss(); Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class); mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(mainIntent); } }); } } }); } }
Kemudian
pada MainActivity buat sebuah textview dengan tulisan Selamat Anda berhasil
mendaftar untuk memastikan user berhasil didaftarkan
untuk
melihat user sudah berhasil mendaftar silakan lihat pada firebase console pada
bagian authentication
Ok
Sekianlah tutorial kali ini jika ada yang kurang jelas silakan ditanyakan pada
komentar dibawah