aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcinzelent <zelent.marcin@gmail.com>2018-04-25 13:34:54 +0200
committermarcinzelent <zelent.marcin@gmail.com>2018-04-25 13:34:54 +0200
commit4de306375a33b2abd067e65658a46d45cba5e56e (patch)
tree00a734df4dcd1d94d0916eee6a6cc1cd98aafa7d /app/src/main/java/org/marcinzelent
parent3812f6ccf9d5796f25f81aeea942c25b58296d01 (diff)
Added Firebase authentication
Diffstat (limited to 'app/src/main/java/org/marcinzelent')
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java10
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/LoginActivity.java113
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/MainActivity.java13
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java3
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/ResetPasswordActivity.java72
-rw-r--r--app/src/main/java/org/marcinzelent/liberavem/SignupActivity.java107
6 files changed, 310 insertions, 8 deletions
diff --git a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java
index 5fa975b..172713d 100644
--- a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java
+++ b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java
@@ -9,6 +9,7 @@ import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
+import com.google.firebase.auth.FirebaseAuth;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -50,7 +51,7 @@ public class DataKeeper {
}
public void downloadData(final Activity activity) {
- downloadBirds(activity);
+ if (birds == null) downloadBirds(activity);
downloadObservations(activity);
}
@@ -115,9 +116,10 @@ public class DataKeeper {
((AllObservationsFragment) fragment).populateList(observations, birds);
else {
List<Observation> myObservationsList = new ArrayList<>();
- for (Observation o : observations)
- if (o.getUserId().equals("Sminem")) myObservationsList.add(o);
-
+ for (Observation o : observations) {
+ String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
+ if (o.getUserId().equals(uid)) myObservationsList.add(o);
+ }
Observation[] myObservations = new Observation[myObservationsList.size()];
myObservations = myObservationsList.toArray(myObservations);
((MyObservationsFragment) fragment).populateList(myObservations, birds);
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<AuthResult>() {
+ @Override
+ public void onComplete(@NonNull Task<AuthResult> 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
diff --git a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java
index b32358a..e53a05c 100644
--- a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java
+++ b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java
@@ -27,6 +27,7 @@ import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
+import com.google.firebase.auth.FirebaseAuth;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -37,7 +38,6 @@ public class MainActivity extends AppCompatActivity
ObservationsFragment.OnFragmentInteractionListener,
AtlasFragment.OnFragmentInteractionListener {
- Fragment observationsFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -47,16 +47,17 @@ public class MainActivity extends AppCompatActivity
setSupportActionBar(toolbar);
if (savedInstanceState ==null) {
+ Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = ObservationsFragment.class;
try {
- observationsFragment = (Fragment) fragmentClass.newInstance();
+ fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
- fragmentManager.beginTransaction().replace(R.id.flContent, observationsFragment).commit();
+ fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
@@ -119,6 +120,12 @@ public class MainActivity extends AppCompatActivity
startActivity(settingsIntent);
} else if (id == R.id.nav_about) {
+ } else if (id == R.id.nav_signout) {
+ FirebaseAuth auth = FirebaseAuth.getInstance();
+ auth.signOut();
+ finish();
+ Intent intent = new Intent(this, LoginActivity.class);
+ startActivity(intent);
}
if (fragmentClass != null) {
try {
diff --git a/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java b/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java
index e69eaba..f6488e4 100644
--- a/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java
+++ b/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java
@@ -28,6 +28,7 @@ import com.android.volley.VolleyLog;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
+import com.google.firebase.auth.FirebaseAuth;
import org.json.JSONException;
import org.json.JSONObject;
@@ -128,7 +129,7 @@ public class NewObservationActivity extends AppCompatActivity {
jsonBody.put("Longitude", ((EditText)findViewById(R.id.new_longitude)).getText());
jsonBody.put("Placename", ((EditText)findViewById(R.id.new_place)).getText());
jsonBody.put("Population", ((EditText)findViewById(R.id.new_population)).getText());
- jsonBody.put("UserId", "Sminem");
+ jsonBody.put("UserId", FirebaseAuth.getInstance().getCurrentUser().getUid());
jsonBody.put("NameDanish", selectedBird.getNameDanish());
jsonBody.put("NameEnglish", selectedBird.getNameEnglish());
final String requestBody = jsonBody.toString();
diff --git a/app/src/main/java/org/marcinzelent/liberavem/ResetPasswordActivity.java b/app/src/main/java/org/marcinzelent/liberavem/ResetPasswordActivity.java
new file mode 100644
index 0000000..cd53374
--- /dev/null
+++ b/app/src/main/java/org/marcinzelent/liberavem/ResetPasswordActivity.java
@@ -0,0 +1,72 @@
+package org.marcinzelent.liberavem;
+
+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.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;
+
+public class ResetPasswordActivity extends AppCompatActivity {
+
+ private EditText inputEmail;
+ private Button btnReset, btnBack;
+ private FirebaseAuth auth;
+ private ProgressBar progressBar;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_reset_password);
+
+ inputEmail = (EditText) findViewById(R.id.email);
+ btnReset = (Button) 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 id", Toast.LENGTH_SHORT).show();
+ return;
+ }
+
+ progressBar.setVisibility(View.VISIBLE);
+ auth.sendPasswordResetEmail(email)
+ .addOnCompleteListener(new OnCompleteListener<Void>() {
+ @Override
+ public void onComplete(@NonNull Task<Void> 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);
+ }
+ });
+ }
+ });
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/org/marcinzelent/liberavem/SignupActivity.java b/app/src/main/java/org/marcinzelent/liberavem/SignupActivity.java
new file mode 100644
index 0000000..280072a
--- /dev/null
+++ b/app/src/main/java/org/marcinzelent/liberavem/SignupActivity.java
@@ -0,0 +1,107 @@
+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.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 SignupActivity extends AppCompatActivity {
+
+ private EditText inputEmail, inputPassword;
+ private Button btnSignIn, btnSignUp, btnResetPassword;
+ private ProgressBar progressBar;
+ private FirebaseAuth auth;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_signup);
+
+ //Get Firebase auth instance
+ auth = FirebaseAuth.getInstance();
+
+ btnSignIn = (Button) findViewById(R.id.sign_in_button);
+ btnSignUp = (Button) findViewById(R.id.sign_up_button);
+ inputEmail = (EditText) findViewById(R.id.email);
+ inputPassword = (EditText) findViewById(R.id.password);
+ progressBar = (ProgressBar) findViewById(R.id.progressBar);
+ btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
+
+ btnResetPassword.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ startActivity(new Intent(SignupActivity.this, ResetPasswordActivity.class));
+ }
+ });
+
+ 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<AuthResult>() {
+ @Override
+ public void onComplete(@NonNull Task<AuthResult> task) {
+ Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
+ progressBar.setVisibility(View.GONE);
+ // 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()) {
+ 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);
+ }
+}