22
33import android .content .Intent ;
44import android .os .Bundle ;
5+
6+ import androidx .activity .result .ActivityResultCallback ;
7+ import androidx .activity .result .ActivityResultLauncher ;
58import androidx .annotation .NonNull ;
69import androidx .appcompat .app .AppCompatActivity ;
710
811import com .firebase .ui .auth .AuthUI ;
12+ import com .firebase .ui .auth .FirebaseAuthUIActivityResultContract ;
913import com .firebase .ui .auth .IdpResponse ;
14+ import com .firebase .ui .auth .data .model .FirebaseAuthUIAuthenticationResult ;
15+ import com .firebase .ui .auth .util .ExtraConstants ;
1016import com .google .android .gms .tasks .OnCompleteListener ;
1117import com .google .android .gms .tasks .Task ;
18+ import com .google .firebase .auth .ActionCodeSettings ;
1219import com .google .firebase .auth .FirebaseAuth ;
1320import com .google .firebase .auth .FirebaseUser ;
1421
1825
1926public class FirebaseUIActivity extends AppCompatActivity {
2027
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]
2240
2341 @ Override
2442 protected void onCreate (Bundle savedInstanceState ) {
@@ -37,33 +55,26 @@ public void createSignInIntent() {
3755 new AuthUI .IdpConfig .TwitterBuilder ().build ());
3856
3957 // 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 );
4663 // [END auth_fui_create_intent]
4764 }
4865
4966 // [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+ // ...
6778 }
6879 }
6980 // [END auth_fui_result]
@@ -97,29 +108,74 @@ public void themeAndLogo() {
97108 List <AuthUI .IdpConfig > providers = Collections .emptyList ();
98109
99110 // [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 );
108118 // [END auth_fui_theme_logo]
109119 }
110120
111121 public void privacyAndTerms () {
112122 List <AuthUI .IdpConfig > providers = Collections .emptyList ();
123+
113124 // [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 ()
116172 .createSignInIntentBuilder ()
173+ .setEmailLink (link )
117174 .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]
124180 }
125181}
0 commit comments