2
2
3
3
import android .content .Intent ;
4
4
import android .os .Bundle ;
5
+
6
+ import androidx .activity .result .ActivityResultCallback ;
7
+ import androidx .activity .result .ActivityResultLauncher ;
5
8
import androidx .annotation .NonNull ;
6
9
import androidx .appcompat .app .AppCompatActivity ;
7
10
8
11
import com .firebase .ui .auth .AuthUI ;
12
+ import com .firebase .ui .auth .FirebaseAuthUIActivityResultContract ;
9
13
import com .firebase .ui .auth .IdpResponse ;
14
+ import com .firebase .ui .auth .data .model .FirebaseAuthUIAuthenticationResult ;
15
+ import com .firebase .ui .auth .util .ExtraConstants ;
10
16
import com .google .android .gms .tasks .OnCompleteListener ;
11
17
import com .google .android .gms .tasks .Task ;
18
+ import com .google .firebase .auth .ActionCodeSettings ;
12
19
import com .google .firebase .auth .FirebaseAuth ;
13
20
import com .google .firebase .auth .FirebaseUser ;
14
21
18
25
19
26
public class FirebaseUIActivity extends AppCompatActivity {
20
27
21
- private static final int RC_SIGN_IN = 123 ;
28
+ // [START auth_fui_create_launcher]
29
+ // See: https://developer.android.com/training/basics/intents/result
30
+ private final ActivityResultLauncher <Intent > signInLauncher = registerForActivityResult (
31
+ new FirebaseAuthUIActivityResultContract (),
32
+ new ActivityResultCallback <FirebaseAuthUIAuthenticationResult >() {
33
+ @ Override
34
+ public void onActivityResult (FirebaseAuthUIAuthenticationResult result ) {
35
+ onSignInResult (result );
36
+ }
37
+ }
38
+ );
39
+ // [END auth_fui_create_launcher]
22
40
23
41
@ Override
24
42
protected void onCreate (Bundle savedInstanceState ) {
@@ -37,33 +55,26 @@ public void createSignInIntent() {
37
55
new AuthUI .IdpConfig .TwitterBuilder ().build ());
38
56
39
57
// Create and launch sign-in intent
40
- startActivityForResult (
41
- AuthUI .getInstance ()
42
- .createSignInIntentBuilder ()
43
- .setAvailableProviders (providers )
44
- .build (),
45
- RC_SIGN_IN );
58
+ Intent signInIntent = AuthUI .getInstance ()
59
+ .createSignInIntentBuilder ()
60
+ .setAvailableProviders (providers )
61
+ .build ();
62
+ signInLauncher .launch (signInIntent );
46
63
// [END auth_fui_create_intent]
47
64
}
48
65
49
66
// [START auth_fui_result]
50
- @ Override
51
- protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
52
- super .onActivityResult (requestCode , resultCode , data );
53
-
54
- if (requestCode == RC_SIGN_IN ) {
55
- IdpResponse response = IdpResponse .fromResultIntent (data );
56
-
57
- if (resultCode == RESULT_OK ) {
58
- // Successfully signed in
59
- FirebaseUser user = FirebaseAuth .getInstance ().getCurrentUser ();
60
- // ...
61
- } else {
62
- // Sign in failed. If response is null the user canceled the
63
- // sign-in flow using the back button. Otherwise check
64
- // response.getError().getErrorCode() and handle the error.
65
- // ...
66
- }
67
+ private void onSignInResult (FirebaseAuthUIAuthenticationResult result ) {
68
+ IdpResponse response = result .getIdpResponse ();
69
+ if (result .getResultCode () == RESULT_OK ) {
70
+ // Successfully signed in
71
+ FirebaseUser user = FirebaseAuth .getInstance ().getCurrentUser ();
72
+ // ...
73
+ } else {
74
+ // Sign in failed. If response is null the user canceled the
75
+ // sign-in flow using the back button. Otherwise check
76
+ // response.getError().getErrorCode() and handle the error.
77
+ // ...
67
78
}
68
79
}
69
80
// [END auth_fui_result]
@@ -97,29 +108,74 @@ public void themeAndLogo() {
97
108
List <AuthUI .IdpConfig > providers = Collections .emptyList ();
98
109
99
110
// [START auth_fui_theme_logo]
100
- startActivityForResult (
101
- AuthUI .getInstance ()
102
- .createSignInIntentBuilder ()
103
- .setAvailableProviders (providers )
104
- .setLogo (R .drawable .my_great_logo ) // Set logo drawable
105
- .setTheme (R .style .MySuperAppTheme ) // Set theme
106
- .build (),
107
- RC_SIGN_IN );
111
+ Intent signInIntent = AuthUI .getInstance ()
112
+ .createSignInIntentBuilder ()
113
+ .setAvailableProviders (providers )
114
+ .setLogo (R .drawable .my_great_logo ) // Set logo drawable
115
+ .setTheme (R .style .MySuperAppTheme ) // Set theme
116
+ .build ();
117
+ signInLauncher .launch (signInIntent );
108
118
// [END auth_fui_theme_logo]
109
119
}
110
120
111
121
public void privacyAndTerms () {
112
122
List <AuthUI .IdpConfig > providers = Collections .emptyList ();
123
+
113
124
// [START auth_fui_pp_tos]
114
- startActivityForResult (
115
- AuthUI .getInstance ()
125
+ Intent signInIntent = AuthUI .getInstance ()
126
+ .createSignInIntentBuilder ()
127
+ .setAvailableProviders (providers )
128
+ .setTosAndPrivacyPolicyUrls (
129
+ "https://example.com/terms.html" ,
130
+ "https://example.com/privacy.html" )
131
+ .build ();
132
+ signInLauncher .launch (signInIntent );
133
+ // [END auth_fui_pp_tos]
134
+ }
135
+
136
+ public void emailLink () {
137
+ // [START auth_fui_email_link]
138
+ ActionCodeSettings actionCodeSettings = ActionCodeSettings .newBuilder ()
139
+ .setAndroidPackageName (
140
+ /* yourPackageName= */ "..." ,
141
+ /* installIfNotAvailable= */ true ,
142
+ /* minimumVersion= */ null )
143
+ .setHandleCodeInApp (true ) // This must be set to true
144
+ .setUrl ("https://google.com" ) // This URL needs to be whitelisted
145
+ .build ();
146
+
147
+ List <AuthUI .IdpConfig > providers = Arrays .asList (
148
+ new AuthUI .IdpConfig .EmailBuilder ()
149
+ .enableEmailLinkSignIn ()
150
+ .setActionCodeSettings (actionCodeSettings )
151
+ .build ()
152
+ );
153
+ Intent signInIntent = AuthUI .getInstance ()
154
+ .createSignInIntentBuilder ()
155
+ .setAvailableProviders (providers )
156
+ .build ();
157
+ signInLauncher .launch (signInIntent );
158
+ // [END auth_fui_email_link]
159
+ }
160
+
161
+ public void catchEmailLink () {
162
+ List <AuthUI .IdpConfig > providers = Collections .emptyList ();
163
+
164
+ // [START auth_fui_email_link_catch]
165
+ if (AuthUI .canHandleIntent (getIntent ())) {
166
+ if (getIntent ().getExtras () == null ) {
167
+ return ;
168
+ }
169
+ String link = getIntent ().getExtras ().getString (ExtraConstants .EMAIL_LINK_SIGN_IN );
170
+ if (link != null ) {
171
+ Intent signInIntent = AuthUI .getInstance ()
116
172
.createSignInIntentBuilder ()
173
+ .setEmailLink (link )
117
174
.setAvailableProviders (providers )
118
- .setTosAndPrivacyPolicyUrls (
119
- "https://example.com/terms.html" ,
120
- "https://example.com/privacy.html" )
121
- .build (),
122
- RC_SIGN_IN );
123
- // [END auth_fui_pp_tos]
175
+ .build ();
176
+ signInLauncher .launch (signInIntent );
177
+ }
178
+ }
179
+ // [END auth_fui_email_link_catch]
124
180
}
125
181
}
0 commit comments