Firebase Authentication – التحقق من البريد الإلكتروني- إعادة ضبط كلمة المرور
Android Kotlin – تعلم اضافة firebase ‘الفايربيس’ لتطبيق وإعادة ضبط كلمة المرور والتحقق من البريد الاكتروني
تم تعديل الواجهات لتسجيل الدخول ليكن اكثر وضوح مع اضافة واجهة لتسجيل الدخول وزر لتغير كلمة المرور والتاكد اذا تم تاكيد البريد الاكتروني او لاء
-
اضافة activity جديدة لتسجيل الدخول عن طريقها
-
اضافة زر لاعادة ضبط كلمة المرور
-
اضافة زر لتحقق من البريد الاكتروني اذا تم تأكيده او لاء
-
اضافة زر لارسال بريد لتأكيد البريد الاكتروني
كود واجهة تسجيل الدخول xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".LoginActivity"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="16dp" android:paddingRight="16dp"> <EditText android:id="@+id/edt_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Email" android:inputType="textEmailAddress" /> <EditText android:id="@+id/edt_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Password" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="16dp" android:paddingRight="16dp"> <Button android:id="@+id/btnSignUp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Sign Up" /> <Button android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sign In" android:id="@+id/btnSignIn"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="16dp" android:gravity="center" android:layout_marginTop="20dp" android:paddingRight="16dp"> <EditText android:id="@+id/EditForgotPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Email" android:inputType="textEmailAddress" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Forgot your password..!" android:id="@+id/btnForgotPassword"/> </LinearLayout> </LinearLayout> |
كود واجهة mainActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="16dp" android:gravity="center" android:layout_marginTop="20dp" android:paddingRight="16dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Verify Email..!" android:id="@+id/btnEmailVerification"/> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sign Out" android:id="@+id/btnSignOut"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check if email verify" android:id="@+id/checkEmail"/> </LinearLayout> |
كود كلاس LoginActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
class LoginActivity : AppCompatActivity() { var auth: FirebaseAuth? = null var editForgotPassword :EditText? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) auth = FirebaseAuth.getInstance() val btnSignUp = findViewById<View>(R.id.btnSignUp) as Button val btnSignIn = findViewById<View>(R.id.btnSignIn) as Button val btnForgotPassword = findViewById<View>(R.id.btnForgotPassword) as Button val edtEmail = findViewById<View>(R.id.edt_email) as EditText val edtPassword = findViewById<View>(R.id.edt_password) as EditText editForgotPassword = findViewById<View>(R.id.EditForgotPassword) as EditText btnSignUp.setOnClickListener { view -> if (validateForm(edtEmail.text.toString(), edtPassword.text.toString(), view)) { signUp(view, edtEmail.text.toString(), edtPassword.text.toString()) } } btnSignIn.setOnClickListener { view -> if (validateForm(edtEmail.text.toString(), edtPassword.text.toString(), view)) { signIn(view, edtEmail.text.toString(), edtPassword.text.toString()) } } btnForgotPassword.setOnClickListener { view -> passwordForget(view) } } private fun passwordForget(view :View) { val email = editForgotPassword!!.text.toString() if (email.isBlank()) { showMessage(view, "Enter your email!") return } auth!!.sendPasswordResetEmail(email).addOnCompleteListener(this, OnCompleteListener { task -> if (task.isSuccessful) { showMessage(view, "Check your email ") } else { showMessage(view, "Fail to send reset password ${task.exception?.message}") } }) } private fun signUp(view: View, email: String, pass: String) { showMessage(view, "Authentication... ") auth!!.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(this, OnCompleteListener { task -> if (task.isSuccessful) { showMessage(view, "Successful") startActivity(Intent(this@LoginActivity, MainActivity::class.java)) } else { showMessage(view, "Error ${task.exception?.message}") } }) } private fun signIn(view: View, email: String, pass: String) { showMessage(view, "Authentication... ") auth!!.signInWithEmailAndPassword(email, pass).addOnCompleteListener(this, OnCompleteListener { task -> if (task.isSuccessful) { showMessage(view, "Successful") startActivity(Intent(this@LoginActivity, MainActivity::class.java)) } else { showMessage(view, "Error ${task.exception?.message}") } }) } private fun showMessage(view: View, message: String) { Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE).setAction("FIRE", null).show() } private fun validateForm(email: String, password: String, view: View): Boolean { if (TextUtils.isEmpty(email)) { showMessage(view, "Enter email address!") return false } if (TextUtils.isEmpty(password)) { showMessage(view, "Enter password!") return false } if (password.length < 8) { showMessage(view, "Password too short, enter minimum 8 characters!") return false } return true } } |
كود كلاس mainActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class MainActivity : AppCompatActivity() { var auth :FirebaseAuth? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) auth = FirebaseAuth.getInstance() val btnSignOut = findViewById<View>(R.id.btnSignOut) as Button val checkEmail = findViewById<View>(R.id.checkEmail) as Button val btnEmailVerification = findViewById<View>(R.id.btnEmailVerification) as Button btnSignOut.setOnClickListener { view -> signOut() } btnEmailVerification.setOnClickListener { view -> sendEmailVerification(view) } checkEmail.setOnClickListener { view -> showMessage(view, "Email Verification : ${auth!!.currentUser!!.isEmailVerified}" ) } } private fun signOut() { auth!!.signOut() finish() } private fun sendEmailVerification(view: View) { val user = auth!!.currentUser user!!.sendEmailVerification().addOnCompleteListener(this) { task -> if (task.isSuccessful) { showMessage(view, "Email Send") } } } private fun showMessage(view : View, message:String) { Snackbar.make(view, message , Snackbar.LENGTH_INDEFINITE).setAction("FIRE", null).show() } } |
تم استخدام دالة validateForm لتاكد من edittext اذا كانت فارغة وايضا من عدد احرف كلمة المرور اذا كانت اقل من 8 احرف
الكود الخاص بارسال رابط للايميل لتغير كلمة المرور
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private fun passwordForget(view :View) { val email = editForgotPassword!!.text.toString() if (email.isBlank()) { showMessage(view, "Enter your email!") return } auth!!.sendPasswordResetEmail(email).addOnCompleteListener(this, OnCompleteListener { task -> if (task.isSuccessful) { showMessage(view, "Check your email ") } else { showMessage(view, "Fail to send reset password ${task.exception?.message}") } }) } |
يتم ادخال الايميل من المستخدم نستخدم دالة sendPasswordResetEmail ونمرر لها الايميل المدخل من المستخدم والفايربيس يتكفل في الباقي 🙂
في الدرس القادم سوف يتم شرح تسجيل الدخول عن طريق google
اذا كان هنالك ايا شرح تريده اتركه في التعليقات وسوف يتم شرحه لاحقاً
الكود تجده كاملاً على GitHub