Skip to content

Commit 0fc1974

Browse files
author
hussienalrubaye
committed
firebase auth
1 parent dde62ab commit 0fc1974

File tree

2 files changed

+361
-0
lines changed

2 files changed

+361
-0
lines changed

Firebase_PlayServices_auth.java

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// login firebase with play services
2+
//1-- add to gradle
3+
compile 'com.google.firebase:firebase-auth:9.6.1'
4+
compile 'com.google.android.gms:play-services-auth:9.6.1'
5+
6+
//1- extend
7+
GoogleApiClient.OnConnectionFailedListener
8+
//2 - define in public
9+
10+
11+
private static final String TAG = "GoogleActivity";
12+
private static final int RC_SIGN_IN = 9001;
13+
14+
// [START declare_auth]
15+
private FirebaseAuth mAuth;
16+
// [END declare_auth]
17+
18+
// [START declare_auth_listener]
19+
private FirebaseAuth.AuthStateListener mAuthListener;
20+
// [END declare_auth_listener]
21+
22+
private GoogleApiClient mGoogleApiClient;
23+
24+
25+
//3- onCreate add
26+
27+
28+
// [START config_signin]
29+
// Configure Google Sign In
30+
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
31+
.requestIdToken(getString(R.string.default_web_client_id))
32+
.requestEmail()
33+
.build();
34+
// [END config_signin]
35+
36+
mGoogleApiClient = new GoogleApiClient.Builder(this)
37+
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
38+
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
39+
.build();
40+
41+
// [START initialize_auth]
42+
mAuth = FirebaseAuth.getInstance();
43+
// [END initialize_auth]
44+
45+
// [START auth_state_listener]
46+
mAuthListener = new FirebaseAuth.AuthStateListener() {
47+
@Override
48+
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
49+
FirebaseUser user = firebaseAuth.getCurrentUser();
50+
if (user != null) {
51+
// User is signed in
52+
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
53+
} else {
54+
// User is signed out
55+
Log.d(TAG, "onAuthStateChanged:signed_out");
56+
}
57+
// [START_EXCLUDE]
58+
updateUI(user);
59+
// [END_EXCLUDE]
60+
}
61+
};
62+
// [END auth_state_listener]
63+
64+
//4 deine this methods
65+
// [START on_start_add_listener]
66+
@Override
67+
public void onStart() {
68+
super.onStart();
69+
mAuth.addAuthStateListener(mAuthListener);
70+
}
71+
// [END on_start_add_listener]
72+
73+
// [START on_stop_remove_listener]
74+
@Override
75+
public void onStop() {
76+
super.onStop();
77+
if (mAuthListener != null) {
78+
mAuth.removeAuthStateListener(mAuthListener);
79+
}
80+
}
81+
// [END on_stop_remove_listener]
82+
83+
// [START onactivityresult]
84+
@Override
85+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
86+
super.onActivityResult(requestCode, resultCode, data);
87+
88+
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
89+
if (requestCode == RC_SIGN_IN) {
90+
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
91+
if (result.isSuccess()) {
92+
// Google Sign In was successful, authenticate with Firebase
93+
GoogleSignInAccount account = result.getSignInAccount();
94+
firebaseAuthWithGoogle(account);
95+
} else {
96+
// Google Sign In failed, update UI appropriately
97+
// [START_EXCLUDE]
98+
updateUI(null);
99+
// [END_EXCLUDE]
100+
}
101+
}
102+
}
103+
// [END onactivityresult]
104+
105+
// [START auth_with_google]
106+
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
107+
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
108+
// [START_EXCLUDE silent]
109+
showProgressDialog();
110+
// [END_EXCLUDE]
111+
112+
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
113+
mAuth.signInWithCredential(credential)
114+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
115+
@Override
116+
public void onComplete(@NonNull Task<AuthResult> task) {
117+
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
118+
119+
// If sign in fails, display a message to the user. If sign in succeeds
120+
// the auth state listener will be notified and logic to handle the
121+
// signed in user can be handled in the listener.
122+
if (!task.isSuccessful()) {
123+
Log.w(TAG, "signInWithCredential", task.getException());
124+
Toast.makeText(GoogleSignInActivity.this, "Authentication failed.",
125+
Toast.LENGTH_SHORT).show();
126+
}
127+
// [START_EXCLUDE]
128+
hideProgressDialog();
129+
// [END_EXCLUDE]
130+
}
131+
});
132+
}
133+
// [END auth_with_google]
134+
135+
// [START signin]
136+
private void signIn() {
137+
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
138+
startActivityForResult(signInIntent, RC_SIGN_IN);
139+
}
140+
// [END signin]
141+
142+
private void signOut() {
143+
// Firebase sign out
144+
mAuth.signOut();
145+
146+
// Google sign out
147+
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
148+
new ResultCallback<Status>() {
149+
@Override
150+
public void onResult(@NonNull Status status) {
151+
updateUI(null);
152+
}
153+
});
154+
}
155+
156+
157+
158+
private void updateUI(FirebaseUser user) {
159+
hideProgressDialog();
160+
String Email=user.getEmail();
161+
String Uid= user.getUid();
162+
}
163+
164+
@Override
165+
public void onConnected(Bundle connectionHint) {
166+
167+
Log.d(TAG, "onConnection is connected:" );
168+
}
169+
@Override
170+
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
171+
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
172+
// be available.
173+
Log.d(TAG, "onConnectionFailed:" + connectionResult);
174+
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
175+
}
176+
177+
178+
@VisibleForTesting
179+
public ProgressDialog mProgressDialog;
180+
181+
public void showProgressDialog() {
182+
if (mProgressDialog == null) {
183+
mProgressDialog = new ProgressDialog(this);
184+
mProgressDialog.setMessage(getString(R.string.loading));
185+
mProgressDialog.setIndeterminate(true);
186+
}
187+
188+
mProgressDialog.show();
189+
}
190+
191+
public void hideProgressDialog() {
192+
if (mProgressDialog != null && mProgressDialog.isShowing()) {
193+
mProgressDialog.dismiss();
194+
}
195+
}
196+
197+
@Override
198+
public void onStop() {
199+
super.onStop();
200+
hideProgressDialog();
201+
}

Firebase_auth.java

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Fire base sign in
2+
3+
//add to grade
4+
compile 'com.google.firebase:firebase-auth:9.6.1'
5+
6+
//code
7+
8+
9+
/**
10+
* Activity to demonstrate anonymous login and account linking (with an email/password account).
11+
*/
12+
//1- define
13+
private static final String TAG = "AnonymousAuth";
14+
15+
// [START declare_auth]
16+
private FirebaseAuth mAuth;
17+
// [END declare_auth]
18+
19+
// [START declare_auth_listener]
20+
private FirebaseAuth.AuthStateListener mAuthListener;
21+
// [END declare_auth_listener]
22+
23+
//2- initiailze OnCreate()
24+
// [START initialize_auth]
25+
mAuth = FirebaseAuth.getInstance();
26+
// [END initialize_auth]
27+
28+
// [START auth_state_listener]
29+
mAuthListener = new FirebaseAuth.AuthStateListener() {
30+
@Override
31+
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
32+
FirebaseUser user = firebaseAuth.getCurrentUser();
33+
if (user != null) {
34+
// User is signed in
35+
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
36+
} else {
37+
// User is signed out
38+
Log.d(TAG, "onAuthStateChanged:signed_out");
39+
}
40+
// [START_EXCLUDE]
41+
updateUI(user);
42+
// [END_EXCLUDE]
43+
}
44+
};
45+
// [END auth_state_listener]
46+
private void updateUI(FirebaseUser user) {
47+
hideProgressDialog();
48+
String Email=user.getEmail();
49+
String Uid= user.getUid();
50+
}
51+
52+
// [START on_start_add_listener]
53+
@Override
54+
public void onStart() {
55+
super.onStart();
56+
mAuth.addAuthStateListener(mAuthListener);
57+
}
58+
// [END on_start_add_listener]
59+
60+
// [START on_stop_remove_listener]
61+
@Override
62+
public void onStop() {
63+
super.onStop();
64+
if (mAuthListener != null) {
65+
mAuth.removeAuthStateListener(mAuthListener);
66+
}
67+
}
68+
// [END on_stop_remove_listener]
69+
70+
private void signInAnonymously() {
71+
showProgressDialog();
72+
// [START signin_anonymously]
73+
mAuth.signInAnonymously()
74+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
75+
@Override
76+
public void onComplete(@NonNull Task<AuthResult> task) {
77+
Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful());
78+
79+
// If sign in fails, display a message to the user. If sign in succeeds
80+
// the auth state listener will be notified and logic to handle the
81+
// signed in user can be handled in the listener.
82+
if (!task.isSuccessful()) {
83+
Log.w(TAG, "signInAnonymously", task.getException());
84+
Toast.makeText(MainActivity.this, "Authentication failed.",
85+
Toast.LENGTH_SHORT).show();
86+
}
87+
88+
// [START_EXCLUDE]
89+
hideProgressDialog();
90+
// [END_EXCLUDE]
91+
}
92+
});
93+
// [END signin_anonymously]
94+
}
95+
96+
private void signOut() {
97+
mAuth.signOut();
98+
99+
}
100+
101+
private void linkAccount() {
102+
103+
104+
// Get email and password from form
105+
String email = "User_Email";
106+
String password ="User_Password";
107+
108+
// Create EmailAuthCredential with email and password
109+
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
110+
111+
// Link the anonymous user to the email credential
112+
showProgressDialog();
113+
// [START link_credential]
114+
mAuth.getCurrentUser().linkWithCredential(credential)
115+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
116+
@Override
117+
public void onComplete(@NonNull Task<AuthResult> task) {
118+
Log.d(TAG, "linkWithCredential:onComplete:" + task.isSuccessful());
119+
120+
// If sign in fails, display a message to the user. If sign in succeeds
121+
// the auth state listener will be notified and logic to handle the
122+
// signed in user can be handled in the listener.
123+
if (!task.isSuccessful()) {
124+
Toast.makeText(MainActivity.this, "Authentication failed.",
125+
Toast.LENGTH_SHORT).show();
126+
}
127+
128+
// [START_EXCLUDE]
129+
hideProgressDialog();
130+
// [END_EXCLUDE]
131+
}
132+
});
133+
// [END link_credential]
134+
}
135+
136+
137+
@VisibleForTesting
138+
public ProgressDialog mProgressDialog;
139+
140+
public void showProgressDialog() {
141+
if (mProgressDialog == null) {
142+
mProgressDialog = new ProgressDialog(this);
143+
mProgressDialog.setMessage(getString(R.string.loading));
144+
mProgressDialog.setIndeterminate(true);
145+
}
146+
147+
mProgressDialog.show();
148+
}
149+
150+
public void hideProgressDialog() {
151+
if (mProgressDialog != null && mProgressDialog.isShowing()) {
152+
mProgressDialog.dismiss();
153+
}
154+
}
155+
156+
@Override
157+
public void onStop() {
158+
super.onStop();
159+
hideProgressDialog();
160+
}

0 commit comments

Comments
 (0)