Created
February 1, 2019 00:56
-
-
Save buildbro/f5968fa3ad84a6ddfd4889067987cb6e to your computer and use it in GitHub Desktop.
Implementation of login for Firebase email/password authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.firebaseemailpasswordexample; | |
import android.content.Intent; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
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.AuthResult; | |
import com.google.firebase.auth.FirebaseAuth; | |
public class LoginActivity extends AppCompatActivity { | |
private EditText emailTV, passwordTV; | |
private Button loginBtn; | |
private ProgressBar progressBar; | |
private FirebaseAuth mAuth; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
mAuth = FirebaseAuth.getInstance(); | |
initializeUI(); | |
loginBtn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
loginUserAccount(); | |
} | |
}); | |
} | |
private void loginUserAccount() { | |
progressBar.setVisibility(View.VISIBLE); | |
String email, password; | |
email = emailTV.getText().toString(); | |
password = passwordTV.getText().toString(); | |
if (TextUtils.isEmpty(email)) { | |
Toast.makeText(getApplicationContext(), "Please enter email...", Toast.LENGTH_LONG).show(); | |
return; | |
} | |
if (TextUtils.isEmpty(password)) { | |
Toast.makeText(getApplicationContext(), "Please enter password!", Toast.LENGTH_LONG).show(); | |
return; | |
} | |
mAuth.signInWithEmailAndPassword(email, password) | |
.addOnCompleteListener(new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
Toast.makeText(getApplicationContext(), "Login successful!", Toast.LENGTH_LONG).show(); | |
progressBar.setVisibility(View.GONE); | |
Intent intent = new Intent(LoginActivity.this, DashboardActivity.class); | |
startActivity(intent); | |
} | |
else { | |
Toast.makeText(getApplicationContext(), "Login failed! Please try again later", Toast.LENGTH_LONG).show(); | |
progressBar.setVisibility(View.GONE); | |
} | |
} | |
}); | |
} | |
private void initializeUI() { | |
emailTV = findViewById(R.id.email); | |
passwordTV = findViewById(R.id.password); | |
loginBtn = findViewById(R.id.login); | |
progressBar = findViewById(R.id.progressBar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment