-
Notifications
You must be signed in to change notification settings - Fork 399
/
MainActivity.java
639 lines (558 loc) · 26.3 KB
/
MainActivity.java
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.quickstart.auth;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.ActionCodeSettings;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FacebookAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthSettings;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GithubAuthProvider;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;
import com.google.firebase.auth.PlayGamesAuthProvider;
import com.google.firebase.auth.SignInMethodQueryResult;
import com.google.firebase.auth.UserInfo;
import com.google.firebase.auth.UserProfileChangeRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkCurrentUser() {
// [START check_current_user]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is signed in
} else {
// No user is signed in
}
// [END check_current_user]
}
public void getUserProfile() {
// [START get_user_profile]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// Name, email address, and profile photo Url
String name = user.getDisplayName();
String email = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
// Check if user's email is verified
boolean emailVerified = user.isEmailVerified();
// The user's ID, unique to the Firebase project. Do NOT use this value to
// authenticate with your backend server, if you have one. Use
// FirebaseUser.getIdToken() instead.
String uid = user.getUid();
}
// [END get_user_profile]
}
public void getProviderData() {
// [START get_provider_data]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
for (UserInfo profile : user.getProviderData()) {
// Id of the provider (ex: google.com)
String providerId = profile.getProviderId();
// UID specific to the provider
String uid = profile.getUid();
// Name, email address, and profile photo Url
String name = profile.getDisplayName();
String email = profile.getEmail();
Uri photoUrl = profile.getPhotoUrl();
}
}
// [END get_provider_data]
}
public void updateProfile() {
// [START update_profile]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Jane Q. User")
.setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
.build();
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
}
});
// [END update_profile]
}
public void updateEmail() {
// [START update_email]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.updateEmail("[email protected]")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User email address updated.");
}
}
});
// [END update_email]
}
public void updatePassword() {
// [START update_password]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String newPassword = "SOME-SECURE-PASSWORD";
user.updatePassword(newPassword)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User password updated.");
}
}
});
// [END update_password]
}
public void sendEmailVerification() {
// [START send_email_verification]
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
user.sendEmailVerification()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Email sent.");
}
}
});
// [END send_email_verification]
}
public void sendEmailVerificationWithContinueUrl() {
// [START send_email_verification_with_continue_url]
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
String url = "http://www.example.com/verify?uid=" user.getUid();
ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder()
.setUrl(url)
.setIOSBundleId("com.example.ios")
// The default for this is populated with the current android package name.
.setAndroidPackageName("com.example.android", false, null)
.build();
user.sendEmailVerification(actionCodeSettings)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Email sent.");
}
}
});
// [END send_email_verification_with_continue_url]
// [START localize_verification_email]
auth.setLanguageCode("fr");
// To apply the default app language instead of explicitly setting it.
// auth.useAppLanguage();
// [END localize_verification_email]
}
public void sendPasswordReset() {
// [START send_password_reset]
FirebaseAuth auth = FirebaseAuth.getInstance();
String emailAddress = "[email protected]";
auth.sendPasswordResetEmail(emailAddress)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Email sent.");
}
}
});
// [END send_password_reset]
}
public void deleteUser() {
// [START delete_user]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User account deleted.");
}
}
});
// [END delete_user]
}
public void reauthenticate() {
// [START reauthenticate]
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
// Get auth credentials from the user for re-authentication. The example below shows
// email and password credentials but there are multiple possible providers,
// such as GoogleAuthProvider or FacebookAuthProvider.
AuthCredential credential = EmailAuthProvider
.getCredential("[email protected]", "password1234");
// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "User re-authenticated.");
}
});
// [END reauthenticate]
}
public void authWithGithub() {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// [START auth_with_github]
String token = "<GITHUB-ACCESS-TOKEN>";
AuthCredential credential = GithubAuthProvider.getCredential(token);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
// [END auth_with_github]
}
public void linkAndMerge(AuthCredential credential) {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// [START auth_link_and_merge]
FirebaseUser prevUser = FirebaseAuth.getInstance().getCurrentUser();
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
FirebaseUser currentUser = task.getResult().getUser();
// Merge prevUser and currentUser accounts and data
// ...
}
});
// [END auth_link_and_merge]
}
public void unlink(String providerId) {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// [START auth_unlink]
mAuth.getCurrentUser().unlink(providerId)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Auth provider unlinked from account
// ...
}
}
});
// [END auth_unlink]
}
public void buildActionCodeSettings() {
// [START auth_build_action_code_settings]
ActionCodeSettings actionCodeSettings =
ActionCodeSettings.newBuilder()
// URL you want to redirect back to. The domain (www.example.com) for this
// URL must be whitelisted in the Firebase Console.
.setUrl("https://www.example.com/finishSignUp?cartId=1234")
// This must be true
.setHandleCodeInApp(true)
.setIOSBundleId("com.example.ios")
.setAndroidPackageName(
"com.example.android",
true, /* installIfNotAvailable */
"12" /* minimumVersion */)
.build();
// [END auth_build_action_code_settings]
}
public void sendSignInLink(String email, ActionCodeSettings actionCodeSettings) {
// [START auth_send_sign_in_link]
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.sendSignInLinkToEmail(email, actionCodeSettings)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Email sent.");
}
}
});
// [END auth_send_sign_in_link]
}
public void verifySignInLink() {
// [START auth_verify_sign_in_link]
FirebaseAuth auth = FirebaseAuth.getInstance();
Intent intent = getIntent();
String emailLink = intent.getData().toString();
// Confirm the link is a sign-in with email link.
if (auth.isSignInWithEmailLink(emailLink)) {
// Retrieve this from wherever you stored it
String email = "[email protected]";
// The client SDK will parse the code from the link for you.
auth.signInWithEmailLink(email, emailLink)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Successfully signed in with email link!");
AuthResult result = task.getResult();
// You can access the new user via result.getUser()
// Additional user info profile *not* available via:
// result.getAdditionalUserInfo().getProfile() == null
// You can check if the user is new or existing:
// result.getAdditionalUserInfo().isNewUser()
} else {
Log.e(TAG, "Error signing in with email link", task.getException());
}
}
});
}
// [END auth_verify_sign_in_link]
}
public void linkWithSignInLink(String email, String emailLink) {
FirebaseAuth auth = FirebaseAuth.getInstance();
// [START auth_link_with_link]
// Construct the email link credential from the current URL.
AuthCredential credential =
EmailAuthProvider.getCredentialWithLink(email, emailLink);
// Link the credential to the current user.
auth.getCurrentUser().linkWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Successfully linked emailLink credential!");
AuthResult result = task.getResult();
// You can access the new user via result.getUser()
// Additional user info profile *not* available via:
// result.getAdditionalUserInfo().getProfile() == null
// You can check if the user is new or existing:
// result.getAdditionalUserInfo().isNewUser()
} else {
Log.e(TAG, "Error linking emailLink credential", task.getException());
}
}
});
// [END auth_link_with_link]
}
public void reauthWithLink(String email, String emailLink) {
FirebaseAuth auth = FirebaseAuth.getInstance();
// [START auth_reauth_with_link]
// Construct the email link credential from the current URL.
AuthCredential credential =
EmailAuthProvider.getCredentialWithLink(email, emailLink);
// Re-authenticate the user with this credential.
auth.getCurrentUser().reauthenticateAndRetrieveData(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// User is now successfully reauthenticated
} else {
Log.e(TAG, "Error reauthenticating", task.getException());
}
}
});
// [END auth_reauth_with_link]
}
public void differentiateLink(String email) {
FirebaseAuth auth = FirebaseAuth.getInstance();
// [START auth_differentiate_link]
auth.fetchSignInMethodsForEmail(email)
.addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
@Override
public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
if (task.isSuccessful()) {
SignInMethodQueryResult result = task.getResult();
List<String> signInMethods = result.getSignInMethods();
if (signInMethods.contains(EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD)) {
// User can sign in with email/password
} else if (signInMethods.contains(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD)) {
// User can sign in with email/link
}
} else {
Log.e(TAG, "Error getting sign in methods for user", task.getException());
}
}
});
// [END auth_differentiate_link]
}
public void getGoogleCredentials() {
String googleIdToken = "";
// [START auth_google_cred]
AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
// [END auth_google_cred]
}
public void getFbCredentials() {
AccessToken token = AccessToken.getCurrentAccessToken();
// [START auth_fb_cred]
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
// [END auth_fb_cred]
}
public void getEmailCredentials() {
String email = "";
String password = "";
// [START auth_email_cred]
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
// [END auth_email_cred]
}
public void signOut() {
// [START auth_sign_out]
FirebaseAuth.getInstance().signOut();
// [END auth_sign_out]
}
public void testPhoneVerify() {
// [START auth_test_phone_verify]
String phoneNum = " 16505554567";
String testVerificationCode = "123456";
// Whenever verification is triggered with the whitelisted number,
// provided it is not set for auto-retrieval, onCodeSent will be triggered.
FirebaseAuth auth = FirebaseAuth.getInstance();
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber(phoneNum)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
// Save the verification id somewhere
// ...
// The corresponding whitelisted code above should be used to complete sign-in.
MainActivity.this.enableUserManuallyInputCode();
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
// Sign in with the credential
// ...
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
// ...
}
})
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
// [END auth_test_phone_verify]
}
private void enableUserManuallyInputCode() {
// No-op
}
public void testPhoneAutoRetrieve() {
// [START auth_test_phone_auto]
// The test phone number and code should be whitelisted in the console.
String phoneNumber = " 16505554567";
String smsCode = "123456";
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseAuthSettings firebaseAuthSettings = firebaseAuth.getFirebaseAuthSettings();
// Configure faking the auto-retrieval with the whitelisted numbers.
firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode);
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(firebaseAuth)
.setPhoneNumber(phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) {
// Instant verification is applied and a credential is directly returned.
// ...
}
// [START_EXCLUDE]
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
// [END_EXCLUDE]
})
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
// [END auth_test_phone_auto]
}
public void gamesMakeGoogleSignInOptions() {
// [START games_google_signin_options]
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
.requestServerAuthCode(getString(R.string.default_web_client_id))
.build();
// [END games_google_signin_options]
}
// [START games_auth_with_firebase]
// Call this both in the silent sign-in task's OnCompleteListener and in the
// Activity's onActivityResult handler.
private void firebaseAuthWithPlayGames(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithPlayGames:" acct.getId());
final FirebaseAuth auth = FirebaseAuth.getInstance();
AuthCredential credential = PlayGamesAuthProvider.getCredential(acct.getServerAuthCode());
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = auth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
}
// [END games_auth_with_firebase]
private void gamesGetUserInfo() {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// [START games_get_user_info]
FirebaseUser user = mAuth.getCurrentUser();
String playerName = user.getDisplayName();
// The user's Id, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server, if you
// have one; use FirebaseUser.getIdToken() instead.
String uid = user.getUid();
// [END games_get_user_info]
}
private void updateUI(@Nullable FirebaseUser user) {
// No-op
}
}