forked from hussien89aa/AndroidTutorialForBeginners
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirebase_auth.java
More file actions
Latest commit
160 lines (132 loc) · 5.25 KB
/
Copy pathFirebase_auth.java
File metadata and controls
160 lines (132 loc) · 5.25 KB
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
// Fire base sign in
//add to grade
compile'com.google.firebase:firebase-auth:9.6.1'
//code
/**
* Activity to demonstrate anonymous login and account linking (with an email/password account).
*/
//1- define
privatestaticfinalStringTAG = "AnonymousAuth";
// [START declare_auth]
privateFirebaseAuthmAuth;
// [END declare_auth]
// [START declare_auth_listener]
privateFirebaseAuth.AuthStateListenermAuthListener;
// [END declare_auth_listener]
//2- initiailze OnCreate()
// [START initialize_auth]
mAuth = FirebaseAuth.getInstance();
// [END initialize_auth]
// [START auth_state_listener]
mAuthListener = newFirebaseAuth.AuthStateListener() {
@Override
publicvoidonAuthStateChanged(@NonNullFirebaseAuthfirebaseAuth) {
FirebaseUseruser = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// [START_EXCLUDE]
updateUI(user);
// [END_EXCLUDE]
}
};
// [END auth_state_listener]
privatevoidupdateUI(FirebaseUseruser) {
hideProgressDialog();
StringEmail=user.getEmail();
StringUid= user.getUid();
}
// [START on_start_add_listener]
@Override
publicvoidonStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
// [END on_start_add_listener]
// [START on_stop_remove_listener]
@Override
publicvoidonStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
// [END on_stop_remove_listener]
privatevoidsignInAnonymously() {
showProgressDialog();
// [START signin_anonymously]
mAuth.signInAnonymously()
.addOnCompleteListener(this, newOnCompleteListener<AuthResult>() {
@Override
publicvoidonComplete(@NonNullTask<AuthResult> task) {
Log.d(TAG, "signInAnonymously: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, "signInAnonymously", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// [START_EXCLUDE]
hideProgressDialog();
// [END_EXCLUDE]
}
});
// [END signin_anonymously]
}
privatevoidsignOut() {
mAuth.signOut();
}
privatevoidlinkAccount() {
// Get email and password from form
Stringemail = "User_Email";
Stringpassword ="User_Password";
// Create EmailAuthCredential with email and password
AuthCredentialcredential = EmailAuthProvider.getCredential(email, password);
// Link the anonymous user to the email credential
showProgressDialog();
// [START link_credential]
mAuth.getCurrentUser().linkWithCredential(credential)
.addOnCompleteListener(this, newOnCompleteListener<AuthResult>() {
@Override
publicvoidonComplete(@NonNullTask<AuthResult> task) {
Log.d(TAG, "linkWithCredential: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()) {
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// [START_EXCLUDE]
hideProgressDialog();
// [END_EXCLUDE]
}
});
// [END link_credential]
}
@VisibleForTesting
publicProgressDialogmProgressDialog;
publicvoidshowProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = newProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
publicvoidhideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
@Override
publicvoidonStop() {
super.onStop();
hideProgressDialog();
}