|
| 1 | +package com.google.firebase.quickstart.auth; |
| 2 | + |
| 3 | +import android.net.Uri; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.annotation.NonNull; |
| 6 | +import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.util.Log; |
| 8 | +import android.widget.Toast; |
| 9 | + |
| 10 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 11 | +import com.google.android.gms.tasks.Task; |
| 12 | +import com.google.firebase.auth.AuthCredential; |
| 13 | +import com.google.firebase.auth.AuthResult; |
| 14 | +import com.google.firebase.auth.EmailAuthProvider; |
| 15 | +import com.google.firebase.auth.FirebaseAuth; |
| 16 | +import com.google.firebase.auth.FirebaseUser; |
| 17 | +import com.google.firebase.auth.GithubAuthProvider; |
| 18 | +import com.google.firebase.auth.UserInfo; |
| 19 | +import com.google.firebase.auth.UserProfileChangeRequest; |
| 20 | + |
| 21 | +public class MainActivity extends AppCompatActivity { |
| 22 | + |
| 23 | + private static final String TAG = "MainActivity"; |
| 24 | + |
| 25 | + @Override |
| 26 | + protected void onCreate(Bundle savedInstanceState) { |
| 27 | + super.onCreate(savedInstanceState); |
| 28 | + setContentView(R.layout.activity_main); |
| 29 | + } |
| 30 | + |
| 31 | + private void checkCurrentUser() { |
| 32 | + // [START check_current_user] |
| 33 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 34 | + if (user != null) { |
| 35 | + // User is signed in |
| 36 | + } else { |
| 37 | + // No user is signed in |
| 38 | + } |
| 39 | + // [END check_current_user] |
| 40 | + } |
| 41 | + |
| 42 | + private void getUserProfile() { |
| 43 | + // [START get_user_profile] |
| 44 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 45 | + if (user != null) { |
| 46 | + // Name, email address, and profile photo Url |
| 47 | + String name = user.getDisplayName(); |
| 48 | + String email = user.getEmail(); |
| 49 | + Uri photoUrl = user.getPhotoUrl(); |
| 50 | + |
| 51 | + // Check if user's email is verified |
| 52 | + boolean emailVerified = user.isEmailVerified(); |
| 53 | + |
| 54 | + // The user's ID, unique to the Firebase project. Do NOT use this value to |
| 55 | + // authenticate with your backend server, if you have one. Use |
| 56 | + // FirebaseUser.getToken() instead. |
| 57 | + String uid = user.getUid(); |
| 58 | + } |
| 59 | + // [END get_user_profile] |
| 60 | + } |
| 61 | + |
| 62 | + private void getProviderData() { |
| 63 | + // [START get_provider_data] |
| 64 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 65 | + if (user != null) { |
| 66 | + for (UserInfo profile : user.getProviderData()) { |
| 67 | + // Id of the provider (ex: google.com) |
| 68 | + String providerId = profile.getProviderId(); |
| 69 | + |
| 70 | + // UID specific to the provider |
| 71 | + String uid = profile.getUid(); |
| 72 | + |
| 73 | + // Name, email address, and profile photo Url |
| 74 | + String name = profile.getDisplayName(); |
| 75 | + String email = profile.getEmail(); |
| 76 | + Uri photoUrl = profile.getPhotoUrl(); |
| 77 | + }; |
| 78 | + } |
| 79 | + // [END get_provider_data] |
| 80 | + } |
| 81 | + |
| 82 | + private void updateProfile() { |
| 83 | + // [START update_profile] |
| 84 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 85 | + |
| 86 | + UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder() |
| 87 | + .setDisplayName("Jane Q. User") |
| 88 | + .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg")) |
| 89 | + .build(); |
| 90 | + |
| 91 | + user.updateProfile(profileUpdates) |
| 92 | + .addOnCompleteListener(new OnCompleteListener<Void>() { |
| 93 | + @Override |
| 94 | + public void onComplete(@NonNull Task<Void> task) { |
| 95 | + if (task.isSuccessful()) { |
| 96 | + Log.d(TAG, "User profile updated."); |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + // [END update_profile] |
| 101 | + } |
| 102 | + |
| 103 | + private void updateEmail() { |
| 104 | + // [START update_email] |
| 105 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 106 | + |
| 107 | + user. updateEmail( "[email protected]") |
| 108 | + .addOnCompleteListener(new OnCompleteListener<Void>() { |
| 109 | + @Override |
| 110 | + public void onComplete(@NonNull Task<Void> task) { |
| 111 | + if (task.isSuccessful()) { |
| 112 | + Log.d(TAG, "User email address updated."); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + // [END update_email] |
| 117 | + } |
| 118 | + |
| 119 | + private void updatePassword() { |
| 120 | + // [START update_password] |
| 121 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 122 | + String newPassword = "SOME-SECURE-PASSWORD"; |
| 123 | + |
| 124 | + user.updatePassword(newPassword) |
| 125 | + .addOnCompleteListener(new OnCompleteListener<Void>() { |
| 126 | + @Override |
| 127 | + public void onComplete(@NonNull Task<Void> task) { |
| 128 | + if (task.isSuccessful()) { |
| 129 | + Log.d(TAG, "User password updated."); |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + // [END update_password] |
| 134 | + } |
| 135 | + |
| 136 | + private void sendPasswordReset() { |
| 137 | + // [START send_password_reset] |
| 138 | + FirebaseAuth auth = FirebaseAuth.getInstance(); |
| 139 | + String emailAddress = "[email protected]"; |
| 140 | + |
| 141 | + auth.sendPasswordResetEmail(emailAddress) |
| 142 | + .addOnCompleteListener(new OnCompleteListener<Void>() { |
| 143 | + @Override |
| 144 | + public void onComplete(@NonNull Task<Void> task) { |
| 145 | + if (task.isSuccessful()) { |
| 146 | + Log.d(TAG, "Email sent."); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | + // [END send_password_reset] |
| 151 | + } |
| 152 | + |
| 153 | + private void deleteUser() { |
| 154 | + // [START delete_user] |
| 155 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 156 | + |
| 157 | + user.delete() |
| 158 | + .addOnCompleteListener(new OnCompleteListener<Void>() { |
| 159 | + @Override |
| 160 | + public void onComplete(@NonNull Task<Void> task) { |
| 161 | + if (task.isSuccessful()) { |
| 162 | + Log.d(TAG, "User account deleted."); |
| 163 | + } |
| 164 | + } |
| 165 | + }); |
| 166 | + // [END delete_user] |
| 167 | + } |
| 168 | + |
| 169 | + private void reauthenticate() { |
| 170 | + // [START reauthenticate] |
| 171 | + FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); |
| 172 | + |
| 173 | + // Get auth credentials from the user for re-authentication. The example below shows |
| 174 | + // email and password credentials but there are multiple possible providers, |
| 175 | + // such as GoogleAuthProvider or FacebookAuthProvider. |
| 176 | + AuthCredential credential = EmailAuthProvider |
| 177 | + . getCredential( "[email protected]", "password1234"); |
| 178 | + |
| 179 | + // Prompt the user to re-provide their sign-in credentials |
| 180 | + user.reauthenticate(credential) |
| 181 | + .addOnCompleteListener(new OnCompleteListener<Void>() { |
| 182 | + @Override |
| 183 | + public void onComplete(@NonNull Task<Void> task) { |
| 184 | + Log.d(TAG, "User re-authenticated."); |
| 185 | + } |
| 186 | + }); |
| 187 | + // [END reauthenticate] |
| 188 | + } |
| 189 | + |
| 190 | + private void authWithGithub() { |
| 191 | + FirebaseAuth mAuth = FirebaseAuth.getInstance(); |
| 192 | + |
| 193 | + // [START auth_with_github] |
| 194 | + String token = "<GITHUB-ACCESS-TOKEN>"; |
| 195 | + AuthCredential credential = GithubAuthProvider.getCredential(token); |
| 196 | + mAuth.signInWithCredential(credential) |
| 197 | + .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { |
| 198 | + @Override |
| 199 | + public void onComplete(@NonNull Task<AuthResult> task) { |
| 200 | + Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); |
| 201 | + |
| 202 | + // If sign in fails, display a message to the user. If sign in succeeds |
| 203 | + // the auth state listener will be notified and logic to handle the |
| 204 | + // signed in user can be handled in the listener. |
| 205 | + if (!task.isSuccessful()) { |
| 206 | + Log.w(TAG, "signInWithCredential", task.getException()); |
| 207 | + Toast.makeText(MainActivity.this, "Authentication failed.", |
| 208 | + Toast.LENGTH_SHORT).show(); |
| 209 | + } |
| 210 | + |
| 211 | + // ... |
| 212 | + } |
| 213 | + }); |
| 214 | + // [END auth_with_github] |
| 215 | + } |
| 216 | +} |
0 commit comments