Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit a7f9ed3

Browse files
firestore create user
1 parent 832118a commit a7f9ed3

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

android/canonical/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
5757
implementation 'com.google.firebase:firebase-analytics:17.4.4'
5858
implementation 'com.google.firebase:firebase-auth:19.3.2'
59+
implementation 'com.google.firebase:firebase-firestore-ktx:21.4.3'
5960
kapt 'com.android.databinding:compiler:3.1.4'
6061
testImplementation 'junit:junit:4.12'
6162
androidTestImplementation 'androidx.test.ext:junit:1.1.1'

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import android.view.ViewGroup
1010
import androidx.fragment.app.activityViewModels
1111
import androidx.lifecycle.Observer
1212
import com.google.android.gms.common.SignInButton
13+
import com.google.firebase.firestore.SetOptions
14+
import com.google.firebase.firestore.ktx.firestore
15+
import com.google.firebase.ktx.Firebase
1316
import com.google.samples.quickstart.canonical.SignInViewModel.Companion.FIREBASE_AUTH_WITH_GOOGLE_FAIL
1417
import com.google.samples.quickstart.canonical.SignInViewModel.Companion.FIREBASE_AUTH_WITH_GOOGLE_SUCCESSFUL
1518
import com.google.samples.quickstart.canonical.SignInViewModel.Companion.GOOGLE_SIGN_IN_UNSUCCESSFUL
@@ -51,6 +54,21 @@ class LoginFragment : Fragment() {
5154
signInVM.getFirebaseAuthLogStatusLiveData().observe(this, Observer {
5255
when (it) {
5356
true -> {
57+
val curFirebaseUser = signInVM.getFirebaseAuthCurUser()
58+
val db = Firebase.firestore
59+
val dbFirebaseUser = hashMapOf(
60+
"UserID" to (curFirebaseUser?.uid ?: ""),
61+
"UserName" to "sdsdsds",
62+
"Email" to (curFirebaseUser?.email ?: "")
63+
)
64+
db.collection("RunUser").document(curFirebaseUser!!.uid)
65+
.set(dbFirebaseUser, SetOptions.merge())
66+
.addOnSuccessListener {
67+
Log.d(LOGIN_FRAGMENT_TAG, "DocumentSnapshot added with ID: ${curFirebaseUser.uid}")
68+
}
69+
.addOnFailureListener { e ->
70+
Log.w(LOGIN_FRAGMENT_TAG, "Error adding document", e)
71+
}
5472
Log.d(LOGIN_FRAGMENT_TAG, "firebaseUser is not null")
5573
val intent = Intent(context, MainActivity::class.java)
5674
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/SignInViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class SignInViewModel : ViewModel() {
8787
curFirebaseUser.value!!.isLogin.value = true
8888
} else {
8989
// If sign in fails, log a message to the user.
90+
Toast.makeText(context, context.getString(R.string.login_failed), Toast.LENGTH_LONG).show()
91+
signOut()
9092
Log.w(SIGN_IN_VM_TAG, "signInWithCredential:failure", task.exception)
9193
}
9294
}
@@ -107,6 +109,7 @@ class SignInViewModel : ViewModel() {
107109
FIREBASE_AUTH_WITH_GOOGLE_FAIL
108110
}
109111
} else {
112+
Toast.makeText(context, context.getString(R.string.login_failed), Toast.LENGTH_LONG).show()
110113
Log.w(SIGN_IN_VM_TAG, "Google sign in unsuccessful")
111114
return GOOGLE_SIGN_IN_UNSUCCESSFUL
112115
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
<string name="cannot_access_location">Cannot access location now. Please Try later</string>
1313
<string name="logout_button_text">Logout</string>
1414
<string name="singout_failed">SignOut Failed. Please try again.</string>
15+
<string name="login_failed">Login Failed. Please try it later.</string>
1516
</resources>

0 commit comments

Comments
 (0)