/
FirebaseUIActivity.kt
159 lines (143 loc) · 5.37 KB
/
FirebaseUIActivity.kt
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.google.firebase.quickstart.auth.kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.firebase.ui.auth.AuthUI
import com.firebase.ui.auth.AuthUI.IdpConfig
import com.firebase.ui.auth.AuthUI.IdpConfig.EmailBuilder
import com.firebase.ui.auth.FirebaseAuthUIActivityResultContract
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult
import com.google.firebase.auth.ActionCodeSettings
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.quickstart.auth.R
abstract class FirebaseUIActivity : AppCompatActivity() {
// [START auth_fui_create_launcher]
// See: https://developer.android.com/training/basics/intents/result
private val signInLauncher = registerForActivityResult(
FirebaseAuthUIActivityResultContract(),
) { res ->
this.onSignInResult(res)
}
// [END auth_fui_create_launcher]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_firebase_ui)
}
private fun createSignInIntent() {
// [START auth_fui_create_intent]
// Choose authentication providers
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.PhoneBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build(),
AuthUI.IdpConfig.FacebookBuilder().build(),
AuthUI.IdpConfig.TwitterBuilder().build(),
)
// Create and launch sign-in intent
val signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build()
signInLauncher.launch(signInIntent)
// [END auth_fui_create_intent]
}
// [START auth_fui_result]
private fun onSignInResult(result: FirebaseAuthUIAuthenticationResult) {
val response = result.idpResponse
if (result.resultCode == RESULT_OK) {
// Successfully signed in
val user = FirebaseAuth.getInstance().currentUser
// ...
} else {
// Sign in failed. If response is null the user canceled the
// sign-in flow using the back button. Otherwise check
// response.getError().getErrorCode() and handle the error.
// ...
}
}
// [END auth_fui_result]
private fun signOut() {
// [START auth_fui_signout]
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener {
// ...
}
// [END auth_fui_signout]
}
private fun delete() {
// [START auth_fui_delete]
AuthUI.getInstance()
.delete(this)
.addOnCompleteListener {
// ...
}
// [END auth_fui_delete]
}
private fun themeAndLogo() {
val providers = emptyList<AuthUI.IdpConfig>()
// [START auth_fui_theme_logo]
val signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setLogo(R.drawable.my_great_logo) // Set logo drawable
.setTheme(R.style.MySuperAppTheme) // Set theme
.build()
signInLauncher.launch(signInIntent)
// [END auth_fui_theme_logo]
}
private fun privacyAndTerms() {
val providers = emptyList<AuthUI.IdpConfig>()
// [START auth_fui_pp_tos]
val signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setTosAndPrivacyPolicyUrls(
"https://example.com/terms.html",
"https://example.com/privacy.html",
)
.build()
signInLauncher.launch(signInIntent)
// [END auth_fui_pp_tos]
}
open fun emailLink() {
// [START auth_fui_email_link]
val actionCodeSettings = ActionCodeSettings.newBuilder()
.setAndroidPackageName( // yourPackageName=
"...", // installIfNotAvailable=
true, // minimumVersion=
null,
)
.setHandleCodeInApp(true) // This must be set to true
.setUrl("https://google.com") // This URL needs to be whitelisted
.build()
val providers = listOf(
EmailBuilder()
.enableEmailLinkSignIn()
.setActionCodeSettings(actionCodeSettings)
.build(),
)
val signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build()
signInLauncher.launch(signInIntent)
// [END auth_fui_email_link]
}
open fun catchEmailLink() {
val providers: List<IdpConfig> = emptyList()
// [START auth_fui_email_link_catch]
if (AuthUI.canHandleIntent(intent)) {
val extras = intent.extras ?: return
val link = extras.getString("email_link_sign_in")
if (link != null) {
val signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setEmailLink(link)
.setAvailableProviders(providers)
.build()
signInLauncher.launch(signInIntent)
}
}
// [END auth_fui_email_link_catch]
}
}