Skip to content

Commit

Permalink
Added password reset logic. Fixes #5 and #6
Browse files Browse the repository at this point in the history
  • Loading branch information
petermcneil committed May 7, 2019
1 parent ca5ad7c commit d1e8470
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mcneil.peter.drop.activities.login

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.preference.PreferenceManager
Expand All @@ -18,13 +19,26 @@ import mcneil.peter.drop.util.Validate

class LoginActivity : AppCompatActivity(), View.OnClickListener {
private val TAG = this.javaClass.simpleName
private lateinit var resetPasswordDialog: AlertDialog

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)

a_login_create.setOnClickListener(this)
a_login_button.setOnClickListener(this)
a_forgot_button.setOnClickListener(this)

resetPasswordDialog = AlertDialog.Builder(this)
.setPositiveButton("Reset") { _, _ ->
run {
val email = input_email.text.toString()
Log.d(TAG, "passwordDialog: Email $email")
DropApp.auth.sendPasswordResetEmail(email)
Toast.makeText(this, getString(R.string.toast_password_reset, email), Toast.LENGTH_SHORT).show()
}
}
.setMessage(getString(R.string.reset_pass_email, input_email.text.toString())).create()
updateUI()
}

Expand All @@ -38,16 +52,11 @@ class LoginActivity : AppCompatActivity(), View.OnClickListener {
updateUI()
}

override fun onBackPressed() {
super.onBackPressed()
Log.d(TAG, "onBackPressed: Closing app")
finish()
}

override fun onClick(v: View?) {
when (v!!.id) {
R.id.a_login_create -> openSignUp()
R.id.a_login_button -> signIn(input_email.text.toString(), input_password.text.toString())
R.id.a_forgot_button -> resetPassword()
}
}

Expand All @@ -68,7 +77,7 @@ class LoginActivity : AppCompatActivity(), View.OnClickListener {
if (task.exception is FirebaseAuthInvalidCredentialsException) {
Toast.makeText(this, getString(R.string.toast_password_wrong), Toast.LENGTH_SHORT).show()
Log.w(TAG, "signInWithEmail:failure:passwordWrong")

a_forgot_button.visibility = View.VISIBLE
} else {
Toast.makeText(this, getString(R.string.toast_auth_failed), Toast.LENGTH_SHORT).show()
Log.w(TAG, "signInWithEmail:failure:unknown", task.exception)
Expand All @@ -77,6 +86,12 @@ class LoginActivity : AppCompatActivity(), View.OnClickListener {
}
}

private fun resetPassword() {
Log.d(TAG, "resetPassword: Showing dialog")
resetPasswordDialog.setMessage(getString(R.string.reset_pass_email, input_email.text.toString()))
resetPasswordDialog.show()
}

private fun updateUI() {
val user = DropApp.auth.currentUser
if (user != null) {
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
android:textAlignment="center" android:textSize="16sp"/>



<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText android:id="@+id/input_email"
Expand All @@ -65,7 +64,16 @@
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="@string/login_button"
android:id="@+id/a_login_button"/>
android:id="@+id/a_login_button"
android:background="#4CAF50"/>

<androidx.appcompat.widget.AppCompatButton android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="@string/forgot_button"
android:id="@+id/a_forgot_button"
android:background="#F44336"
android:visibility="gone"/>

<androidx.appcompat.widget.AppCompatTextView android:id="@+id/a_login_create"
android:layout_width="fill_parent"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,8 @@
<string name="welcome_message_1">Welcome to Drop!</string>
<string name="welcome_message_2">The app where you go on a treasure hunt for messages</string>
<string name="welcome_message_3">Log in or create an account below</string>
<string name="forgot_button">Forgotten password?</string>
<string name="reset_pass_email">Password reset will be sent to the email address: %s</string>
<string name="toast_password_reset">Email reset sent to %s</string>

</resources>

0 comments on commit d1e8470

Please sign in to comment.