Skip to content

Yang auth #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jul 29, 2020
Merged

Yang auth #476

merged 22 commits into from
Jul 29, 2020

Conversation

butterflyforever
Copy link
Collaborator

@butterflyforever butterflyforever commented Jul 17, 2020

This PR contains 2 updates

  1. Integrated login feature using Auth API and Firebase Auth
  2. Implemented Logout feature

The last commit:

  1. Add login fragment. Separate login process from MainActivity
  2. Reduce unnecessary variables in MainActivity
  3. Using ViewModel to save the user
  4. Fix bug in firebaseAuthWithGoogle addOnCompleteListener
  5. Remove the activity in back stack.

2. Reduce unnecessary variables in MainActivity
3. Using ViewModel to save the user
4. Fix bug in firebaseAuthWithGoogle addOnCompleteListener
5. Remove the activity in backstack.
@butterflyforever butterflyforever linked an issue Jul 22, 2020 that may be closed by this pull request
2. Add AuthStateListener and oberver to lisen the change of current login status
3. Solve the coordination between async operations
signInVM.signOut()
}
// update UI
view.findViewById<TextView>(R.id.textView)?.text = signInVM.getFirebaseAuthCurUser()?.email
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can either bind the viewmodel's currentUser email to fragment_me.xml or update this in the observer when currentUser is not null (maybe move the observer down here).

Comment on lines +19 to +22
data class CurFirebaseUser(
var firebaseUser: MutableLiveData<FirebaseUser?> = MutableLiveData(FirebaseAuth.getInstance().currentUser),
var isLogin: MutableLiveData<Boolean> = MutableLiveData(firebaseUser.value?.let { true } ?: run{ false })
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to wrap the Firebaseuser to antoher class. Seems we can just have something like this

val currentUser : MutableLiveData<FirebaseUser?> by lazy {
    MutableLiveData<FirebaseUser?> (FirebaseAuth.getInstance().currentUser)
}

And we can just determine the isLogin status by checking the value of currentUser. Keeping another isLogin might be error prone, cuz you will need to update 2 values together if something has changed. What if you forget to update one of them, it can cause the 2 fields out of sync.

Comment on lines +115 to +121
fun getFirebaseAuthLogStatusLiveData(): MutableLiveData<Boolean> {
return curFirebaseUser.value!!.isLogin
}

fun getFirebaseAuthCurUser(): FirebaseUser? {
return curFirebaseUser.value!!.firebaseUser.value
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seems unnecessary. In the fragments, we can just observe the currentUser, something like this

signInVM.currentUser.observe(this, Observer {
  if (it != null) {
    // do something with user logged in
  } else {
    // do something with user not logged in
  }
})

Comment on lines +36 to +44
private fun googleSignInInit() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(context.getString(R.string.default_web_client_id))
.requestEmail()
.build()

googleSignInClient = GoogleSignIn.getClient(activity, gso)
Log.d(SIGN_IN_VM_TAG, "googleSignInClientInit")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private fun googleSignInInit() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(context.getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(activity, gso)
Log.d(SIGN_IN_VM_TAG, "googleSignInClientInit")
}
private fun getGoogleSignInClient() : GoogleSignInClient {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(context.getString(R.string.default_web_client_id))
.requestEmail()
.build()
Log.d(SIGN_IN_VM_TAG, "googleSignInClientInit")
return GoogleSignIn.getClient(activity, gso)
}

Seems this can just return a GoogleSignInClient. And the googleSignInClient field on top is not necessary.

@caller9 caller9 merged commit 832118a into summer-2020 Jul 29, 2020
@butterflyforever butterflyforever deleted the yang-auth branch July 29, 2020 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Google Auth and Firebase sign-in
3 participants