Skip to content

Instantly share code, notes, and snippets.

@dwijonarko
Created November 11, 2019 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwijonarko/07f6c66ee26ed2266866cad6fd57007d to your computer and use it in GitHub Desktop.
Save dwijonarko/07f6c66ee26ed2266866cad6fd57007d to your computer and use it in GitHub Desktop.
Firebase Email-Password Auth
<?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:layout_margin="10dp"
tools:context=".HomeActivity">
<TextView
android:id="@+id/user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="65dp"
android:text="Home Activity" />
<TextView
android:id="@+id/user_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/user_name"
android:textAlignment="center"
android:text="Home Activity" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/user_email"
android:onClick="logoutProcess"
android:text="Logout" />
</RelativeLayout>
<?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="#ffffff"
tools:context=".MainActivity">
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="45dp"
app:srcCompat="@drawable/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:layout_marginTop="12dp"
android:text="Please login with your account"
android:textAlignment="center"
android:textColor="#cccccc"
android:textSize="24dp"
android:textStyle="bold" />
<EditText
android:id="@+id/emailText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:background="#dddddd"
android:hint="Email address"
android:inputType="textEmailAddress"
android:padding="20dp" />
<EditText
android:id="@+id/passwordText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/emailText"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:background="#dddddd"
android:hint="Password"
android:inputType="textPassword"
android:padding="20dp" />
<Button
android:id="@+id/LoginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/passwordText"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_marginBottom="12dp"
android:background="#8BC34A"
android:onClick="loginProses"
android:text="Login" />
<Button
android:id="@+id/SignUpButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/LoginButton"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_marginBottom="12dp"
android:background="#4CAF50"
android:onClick="signUpProsses"
android:text="Sign Up" />
</RelativeLayout>
<?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="@android:color/background_light"
tools:context=".SignUpActivity">
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="25dp"
app:srcCompat="@drawable/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:layout_marginTop="12dp"
android:text="Fill this form to register"
android:textAlignment="center"
android:textColor="#cccccc"
android:textSize="24dp"
android:textStyle="bold" />
<EditText
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:background="#dddddd"
android:hint="Your Name"
android:inputType="text"
android:padding="20dp" />
<EditText
android:id="@+id/user_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/user_name"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:background="#dddddd"
android:hint="Email address"
android:inputType="textEmailAddress"
android:padding="20dp" />
<EditText
android:id="@+id/user_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/user_email"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:background="#dddddd"
android:hint="Password"
android:inputType="textPassword"
android:padding="20dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/user_password"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:onClick="register"
android:text="Save" />
</RelativeLayout>
package com.vokasi.firebaseauthexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import org.w3c.dom.Text;
public class HomeActivity extends AppCompatActivity {
private TextView user_name;
private TextView user_email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
user_name = (TextView) findViewById(R.id.user_name);
user_email = (TextView) findViewById(R.id.user_email);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
user_name.setText(name);
user_email.setText(email);
}
}
public void logoutProcess(View view){
FirebaseAuth.getInstance().signOut();
Intent loginpage = new Intent(HomeActivity.this, MainActivity.class);
startActivity(loginpage);
}
}
package com.vokasi.firebaseauthexample;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
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.FirebaseUser;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private EditText email;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email = (EditText) findViewById(R.id.emailText);
password = (EditText) findViewById(R.id.passwordText);
mAuth = FirebaseAuth.getInstance();
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser!=null){
Intent homepage = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homepage);
finish();
}
}
public void loginProses(View view){
mAuth.signInWithEmailAndPassword(email.getText().toString(),password.getText().toString()).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(MainActivity.this, "Login Successfull", Toast.LENGTH_LONG).show();
FirebaseUser user = mAuth.getCurrentUser();
Intent homepage = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homepage);
}else{
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_LONG).show();
}
}
});
}
public void signUpProsses(View view){
Intent signupPage = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(signupPage);
}
}
package com.vokasi.firebaseauthexample;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
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.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
public class SignUpActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
EditText user_name;
EditText user_email;
EditText user_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
user_name = (EditText) findViewById(R.id.user_name);
user_email = (EditText) findViewById(R.id.user_email);
user_password = (EditText) findViewById(R.id.user_password);
mAuth = FirebaseAuth.getInstance();
}
public void register(View view){
String email = user_email.getText().toString();
String password = user_password.getText().toString();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(user_name.getText().toString())
.setPhotoUri(Uri.parse("https://randomuser.me/api/portraits/men/76.jpg"))
.build();
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(SignUpActivity.this, "Data updated", Toast.LENGTH_SHORT).show();
Intent homepage = new Intent(SignUpActivity.this, HomeActivity.class);
startActivity(homepage);
finish();
}
}
});
} else {
Toast.makeText(SignUpActivity.this, "Registration failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment