Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
AndroidTutorialForBeginners/Firebase_PlayServices_auth.java at master · elref/AndroidTutorialForBeginners · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
elref
AndroidTutorialForBeginners
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AndroidTutorialForBeginners
/
Firebase_PlayServices_auth.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
201 lines (167 loc) · 6.74 KB
master
Breadcrumbs
AndroidTutorialForBeginners
/
Firebase_PlayServices_auth.java
Copy path
Top
File metadata and controls
Code
Blame
201 lines (167 loc) · 6.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// login firebase with play services
//1-- add to gradle
compile
'com.google.firebase:firebase-auth:9.6.1'
compile
'com.google.android.gms:play-services-auth:9.6.1'
//1- extend
GoogleApiClient
.
OnConnectionFailedListener
//2 - define in public
private
static
final
String
TAG
=
"GoogleActivity"
;
private
static
final
int
RC_SIGN_IN
=
9001
;
// [START declare_auth]
private
FirebaseAuth
mAuth
;
// [END declare_auth]
// [START declare_auth_listener]
private
FirebaseAuth
.
AuthStateListener
mAuthListener
;
// [END declare_auth_listener]
private
GoogleApiClient
mGoogleApiClient
;
//3- onCreate add
// [START config_signin]
// Configure Google Sign In
GoogleSignInOptions
gso
=
new
GoogleSignInOptions
.
Builder
(
GoogleSignInOptions
.
DEFAULT_SIGN_IN
)
.
requestIdToken
(
getString
(
R
.
string
.
default_web_client_id
))
.
requestEmail
()
.
build
();
// [END config_signin]
mGoogleApiClient
=
new
GoogleApiClient
.
Builder
(
this
)
.
enableAutoManage
(
this
/* FragmentActivity */
,
this
/* OnConnectionFailedListener */
)
.
addApi
(
Auth
.
GOOGLE_SIGN_IN_API
,
gso
)
.
build
();
// [START initialize_auth]
mAuth
=
FirebaseAuth
.
getInstance
();
// [END initialize_auth]
// [START auth_state_listener]
mAuthListener
=
new
FirebaseAuth
.
AuthStateListener
() {
@
Override
public
void
onAuthStateChanged
(
@
NonNull
FirebaseAuth
firebaseAuth
) {
FirebaseUser
user
=
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]
//4 deine this methods
// [START on_start_add_listener]
@
Override
public
void
onStart
() {
super
.
onStart
();
mAuth
.
addAuthStateListener
(
mAuthListener
);
}
// [END on_start_add_listener]
// [START on_stop_remove_listener]
@
Override
public
void
onStop
() {
super
.
onStop
();
if
(
mAuthListener
!=
null
) {
mAuth
.
removeAuthStateListener
(
mAuthListener
);
}
}
// [END on_stop_remove_listener]
// [START onactivityresult]
@
Override
public
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
) {
super
.
onActivityResult
(
requestCode
,
resultCode
,
data
);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if
(
requestCode
==
RC_SIGN_IN
) {
GoogleSignInResult
result
=
Auth
.
GoogleSignInApi
.
getSignInResultFromIntent
(
data
);
if
(
result
.
isSuccess
()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount
account
=
result
.
getSignInAccount
();
firebaseAuthWithGoogle
(
account
);
}
else
{
// Google Sign In failed, update UI appropriately
// [START_EXCLUDE]
updateUI
(
null
);
// [END_EXCLUDE]
}
}
}
// [END onactivityresult]
// [START auth_with_google]
private
void
firebaseAuthWithGoogle
(
GoogleSignInAccount
acct
) {
Log
.
d
(
TAG
,
"firebaseAuthWithGoogle:"
+
acct
.
getId
());
// [START_EXCLUDE silent]
showProgressDialog
();
// [END_EXCLUDE]
AuthCredential
credential
=
GoogleAuthProvider
.
getCredential
(
acct
.
getIdToken
(),
null
);
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
(
GoogleSignInActivity
.
this
,
"Authentication failed."
,
Toast
.
LENGTH_SHORT
).
show
();
}
// [START_EXCLUDE]
hideProgressDialog
();
// [END_EXCLUDE]
}
});
}
// [END auth_with_google]
// [START signin]
private
void
signIn
() {
Intent
signInIntent
=
Auth
.
GoogleSignInApi
.
getSignInIntent
(
mGoogleApiClient
);
startActivityForResult
(
signInIntent
,
RC_SIGN_IN
);
}
// [END signin]
private
void
signOut
() {
// Firebase sign out
mAuth
.
signOut
();
// Google sign out
Auth
.
GoogleSignInApi
.
signOut
(
mGoogleApiClient
).
setResultCallback
(
new
ResultCallback
<
Status
>() {
@
Override
public
void
onResult
(
@
NonNull
Status
status
) {
updateUI
(
null
);
}
});
}
private
void
updateUI
(
FirebaseUser
user
) {
hideProgressDialog
();
String
Email
=
user
.
getEmail
();
String
Uid
=
user
.
getUid
();
}
@
Override
public
void
onConnected
(
Bundle
connectionHint
) {
Log
.
d
(
TAG
,
"onConnection is connected:"
);
}
@
Override
public
void
onConnectionFailed
(
@
NonNull
ConnectionResult
connectionResult
) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log
.
d
(
TAG
,
"onConnectionFailed:"
+
connectionResult
);
Toast
.
makeText
(
this
,
"Google Play Services error."
,
Toast
.
LENGTH_SHORT
).
show
();
}
@
VisibleForTesting
public
ProgressDialog
mProgressDialog
;
public
void
showProgressDialog
() {
if
(
mProgressDialog
==
null
) {
mProgressDialog
=
new
ProgressDialog
(
this
);
mProgressDialog
.
setMessage
(
getString
(
R
.
string
.
loading
));
mProgressDialog
.
setIndeterminate
(
true
);
}
mProgressDialog
.
show
();
}
public
void
hideProgressDialog
() {
if
(
mProgressDialog
!=
null
&&
mProgressDialog
.
isShowing
()) {
mProgressDialog
.
dismiss
();
}
}
@
Override
public
void
onStop
() {
super
.
onStop
();
hideProgressDialog
();
}
You can’t perform that action at this time.