From 4de306375a33b2abd067e65658a46d45cba5e56e Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Wed, 25 Apr 2018 13:34:54 +0200 Subject: Added Firebase authentication --- .../org/marcinzelent/liberavem/LoginActivity.java | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 app/src/main/java/org/marcinzelent/liberavem/LoginActivity.java (limited to 'app/src/main/java/org/marcinzelent/liberavem/LoginActivity.java') diff --git a/app/src/main/java/org/marcinzelent/liberavem/LoginActivity.java b/app/src/main/java/org/marcinzelent/liberavem/LoginActivity.java new file mode 100644 index 0000000..25acb3d --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/LoginActivity.java @@ -0,0 +1,113 @@ +package org.marcinzelent.liberavem; + +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.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 inputEmail, inputPassword; + private FirebaseAuth auth; + private ProgressBar progressBar; + private Button btnSignup, btnLogin, btnReset; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Get Firebase auth instance + auth = FirebaseAuth.getInstance(); + + if (auth.getCurrentUser() != null) { + startActivity(new Intent(LoginActivity.this, MainActivity.class)); + finish(); + } + + // set the view now + 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 = (Button) findViewById(R.id.btn_login); + btnReset = (Button) findViewById(R.id.btn_reset_password); + + //Get Firebase auth instance + 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); + + //authenticate user + auth.signInWithEmailAndPassword(email, password) + .addOnCompleteListener(LoginActivity.this, new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + // 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. + progressBar.setVisibility(View.GONE); + if (!task.isSuccessful()) { + // there was an error + 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(); + } + } + }); + } + }); + } +} \ No newline at end of file -- cgit v1.2.3