Skip to content

Commit c706c6e

Browse files
committed
fix: rearrange login process, add authStateListener to update UI after login
1 parent 9280da8 commit c706c6e

File tree

3 files changed

+58
-70
lines changed

3 files changed

+58
-70
lines changed

app/src/main/java/tech/akpmakes/android/taskkeeper/MainActivity.java

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,19 @@ private void applyRemoteConfig() {
9292
@Override
9393
public void onStart() {
9494
super.onStart();
95-
// Check if user is signed in (non-null) and update UI accordingly.
96-
FirebaseUser currentUser = mAuth.getCurrentUser();
97-
if(currentUser == null) {
98-
mAuth.signInAnonymously()
99-
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
100-
@Override
101-
public void onComplete(@NonNull Task<AuthResult> task) {
102-
if (task.isSuccessful()) {
103-
// Sign in success, update UI with the signed-in user's information
104-
Log.d(TAG, "signInAnonymously:success");
105-
FirebaseUser user = mAuth.getCurrentUser();
106-
updateUI(user);
107-
} else {
108-
// If sign in fails, display a message to the user.
109-
Log.w(TAG, "signInAnonymously:failure", task.getException());
110-
Snackbar.make(findViewById(android.R.id.content), "Server connection failed. You may experience problems saving data if this issue persists.",
111-
Snackbar.LENGTH_LONG).show();
112-
updateUI(null);
113-
}
114-
}
115-
});
116-
} else {
117-
updateUI(currentUser);
118-
}
95+
96+
mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
97+
@Override
98+
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
99+
updateUI(firebaseAuth.getCurrentUser());
100+
}
101+
});
102+
}
103+
104+
@Override
105+
public void onResume() {
106+
super.onResume();
107+
updateAuth();
119108
}
120109

121110
@Override
@@ -126,6 +115,33 @@ protected void onDestroy() {
126115
}
127116
}
128117

118+
private void updateAuth() {
119+
// Check if user is signed in (non-null) and update UI accordingly.
120+
FirebaseUser currentUser = mAuth.getCurrentUser();
121+
if(currentUser == null) {
122+
mAuth.signInAnonymously()
123+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
124+
@Override
125+
public void onComplete(@NonNull Task<AuthResult> task) {
126+
if (task.isSuccessful()) {
127+
// Sign in success, update UI with the signed-in user's information
128+
Log.d(TAG, "signInAnonymously:success");
129+
FirebaseUser user = mAuth.getCurrentUser();
130+
updateUI(user);
131+
} else {
132+
// If sign in fails, display a message to the user.
133+
Log.w(TAG, "signInAnonymously:failure", task.getException());
134+
Snackbar.make(findViewById(android.R.id.content), R.string.sign_in_failure,
135+
Snackbar.LENGTH_LONG).show();
136+
updateUI(null);
137+
}
138+
}
139+
});
140+
} else {
141+
updateUI(currentUser);
142+
}
143+
}
144+
129145
private void updateUI(FirebaseUser user) {
130146
if(user == null) {
131147
return; // TODO: display an error

app/src/main/java/tech/akpmakes/android/taskkeeper/SettingsActivity.java

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -294,54 +294,25 @@ private void handleGoogleSignInResult(GoogleSignInResult result) {
294294

295295
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
296296
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
297+
Log.d(TAG, "firebaseAuthWithGoogle:currentUid:" + mAuth.getCurrentUser().getUid());
297298

298299
final AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
299-
if (mAuth.getCurrentUser() != null) {
300-
mAuth.getCurrentUser().linkWithCredential(credential)
301-
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
302-
@Override
303-
public void onComplete(@NonNull Task<AuthResult> task) {
304-
if (task.isSuccessful()) {
305-
Log.d(TAG, "linkWithCredential:success");
306-
} else {
307-
Log.w(TAG, "linkWithCredential:failure", task.getException());
308-
mAuth.signInWithCredential(credential)
309-
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
310-
@Override
311-
public void onComplete(@NonNull Task<AuthResult> task) {
312-
if (task.isSuccessful()) {
313-
// Sign in success, update UI with the signed-in user's information
314-
Log.d(TAG, "signInWithCredential:success");
315-
} else {
316-
// If sign in fails, display a message to the user.
317-
Log.w(TAG, "signInWithCredential:failure", task.getException());
318-
Snackbar.make(getActivity().findViewById(android.R.id.content), "Authentication failed.",
319-
Snackbar.LENGTH_LONG).show();
320-
}
321-
updateUI();
322-
}
323-
});
324-
}
325-
}
326-
});
327-
} else {
328-
mAuth.signInWithCredential(credential)
329-
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
330-
@Override
331-
public void onComplete(@NonNull Task<AuthResult> task) {
332-
if (task.isSuccessful()) {
333-
// Sign in success, update UI with the signed-in user's information
334-
Log.d(TAG, "signInWithCredential:success");
335-
} else {
336-
// If sign in fails, display a message to the user.
337-
Log.w(TAG, "signInWithCredential:failure", task.getException());
338-
Snackbar.make(getActivity().findViewById(android.R.id.content), "Authentication failed.",
339-
Snackbar.LENGTH_LONG).show();
340-
}
341-
updateUI();
342-
}
343-
});
344-
}
300+
mAuth.signInWithCredential(credential)
301+
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
302+
@Override
303+
public void onComplete(@NonNull Task<AuthResult> task) {
304+
if (task.isSuccessful()) {
305+
// Sign in success, update UI with the signed-in user's information
306+
Log.d(TAG, "signInWithCredential:success");
307+
} else {
308+
// If sign in fails, display a message to the user.
309+
Log.w(TAG, "signInWithCredential:failure", task.getException());
310+
Snackbar.make(getActivity().findViewById(android.R.id.content), "Authentication failed.",
311+
Snackbar.LENGTH_LONG).show();
312+
}
313+
updateUI();
314+
}
315+
});
345316
}
346317

347318
private void signInGoogle() {

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@
7070
<string name="welcome_screen_title">Welcome screen</string>
7171
<string name="rate_title">Rate When</string>
7272
<string name="rate_summary"></string>
73+
<string name="sign_in_failure">Sign in failed. You may experience problems saving data if this issue persists.</string>
7374
</resources>

0 commit comments

Comments
 (0)