Firebase Send Email Link
Auth with Firebase Email Link (detailed manual)
4 min readJul 6, 2021
0️⃣ Set up the project and plug Firebase:
https://firebase.google.com/docs/android/setup
1️⃣ Add Firebase Auth and Firebase Dynamic Link:
implementation platform('com.google.firebase:firebase-bom:28.2.0')
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-dynamic-links-ktx'
2️⃣ ⬇️
Auth:
Dynamic Link:
3️⃣⬇️
4️⃣ Copy: [ your_name_app.firebaseapp.com ]:
5️⃣ Create a constant and paste the copied URL:
companion object {
const val FIREBASE_DEFAULT_DOMAIN = "https://your_name_app.firebaseapp.com"
}
6️⃣ Create ActionCodeSettings:
private fun getActionCodeSettings() = actionCodeSettings {
url = "$FIREBASE_DEFAULT_DOMAIN"
handleCodeInApp = true
setIOSBundleId("com.example.ios")
setAndroidPackageName(BuildConfig.APPLICATION_ID, true, null)
}
7️⃣ Create Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_email"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="Email"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent=".5" />
<Button
android:id="@+id/btn_send"
android:layout_width="0dp"
android:layout_height="0dp"
android:maxLines="1"
android:text="send"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_email"
app:layout_constraintWidth_percent=".3" />
</androidx.constraintlayout.widget.ConstraintLayout>
8️⃣ Plug in the viewBinding:
https://developer.android.com/topic/libraries/view-binding
9️⃣ Implement code sending signIn link to email:
package com.example.demofirebasesendemaillink
import android.os.Bundle
import android.util.Log
import android.util.Patterns
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.demofirebasesendemaillink.databinding.ActivityMainBinding
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.actionCodeSettings
class MainActivity : AppCompatActivity() {
companion object {
const val FIREBASE_DEFAULT_DOMAIN = "https://demofirebasesendemaillink.firebaseapp.com"
}
private lateinit var binding: ActivityMainBinding
private val auth = FirebaseAuth.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnSend.setOnClickListener {
val email = binding.editEmail.text.toString()
if (Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
auth.sendSignInLinkToEmail(email, getActionCodeSettings())
.addOnSuccessListener {
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show()
}
.addOnFailureListener {
Toast.makeText(this, "Failure: $it", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Not Email", Toast.LENGTH_SHORT).show()
}
}
}
private fun getActionCodeSettings() = actionCodeSettings {
url = "$FIREBASE_DEFAULT_DOMAIN"
handleCodeInApp = true
setIOSBundleId("com.example.ios")
setAndroidPackageName(BuildConfig.APPLICATION_ID, true, null)
}
}
Attention: emails may come in SPAM because this example project
BONUS: If you want to transfer useful data:
Add to Firebase in project settings SHA256:
You can get it like this:
Add the data we need:
private fun getActionCodeSettings() = actionCodeSettings {
url = "$FIREBASE_DEFAULT_DOMAIN" + "/?email=your_email@gmail.com"
handleCodeInApp = true
setIOSBundleId("com.example.ios")
setAndroidPackageName(BuildConfig.APPLICATION_ID, true, null)
}
BONUS: If you want to get data from the link:
var email: String? = nullprivate fun getData(intent: Intent) {
FirebaseDynamicLinks.getInstance().getDynamicLink(intent)
.addOnSuccessListener {
if (it != null && it.link != null) {
email = it.link!!.getEmail()
}
}
}
private fun Uri.getEmail(): String? {
return getQueryParameter("continueUrl")
?.toUri()
?.getQueryParameter("email")
}
PS. Vel_daN: Love what You DO 💚.