1
1
package com.google.samples.quickstart.canonical
2
2
3
- import android.content.Context
4
3
import android.content.Intent
5
4
import androidx.appcompat.app.AppCompatActivity
6
5
import android.os.Bundle
7
- import android.util.AttributeSet
8
6
import android.util.Log
9
- import android.view.View
10
- import android.widget.Button
11
7
import androidx.fragment.app.FragmentTransaction
12
8
import com.google.android.gms.auth.api.signin.GoogleSignIn
13
9
import com.google.android.gms.auth.api.signin.GoogleSignInClient
14
10
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
15
11
import com.google.android.gms.common.SignInButton
16
12
import com.google.android.gms.common.api.ApiException
17
13
import com.google.android.material.bottomnavigation.BottomNavigationView
18
- import com.google.android.material.snackbar.Snackbar
19
14
import com.google.firebase.auth.FirebaseAuth
20
15
import com.google.firebase.auth.GoogleAuthProvider
21
16
22
17
class MainActivity : AppCompatActivity () {
23
- private lateinit var mGoogleSignInClient: GoogleSignInClient
18
+ lateinit var mGoogleSignInClient: GoogleSignInClient
24
19
private lateinit var auth: FirebaseAuth
20
+ private val tag = " MainActivity-Login"
21
+ private val firebaseTag = " MainActivity-Login"
25
22
26
23
private fun googleSignInInit () {
27
24
val gso = GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
@@ -38,6 +35,51 @@ class MainActivity : AppCompatActivity() {
38
35
startActivityForResult(signInIntent, RC_SIGN_IN )
39
36
}
40
37
38
+
39
+ override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
40
+ super .onActivityResult(requestCode, resultCode, data)
41
+
42
+ // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
43
+ if (requestCode == RC_SIGN_IN ) {
44
+ val task = GoogleSignIn .getSignedInAccountFromIntent(data)
45
+ if (task.isSuccessful) {
46
+ try {
47
+ // Google Sign In was successful, authenticate with Firebase
48
+ val account = task.getResult(ApiException ::class .java)!!
49
+ Log .d(tag, " firebaseAuthWithGoogle:" + account.id)
50
+ firebaseAuthWithGoogle(account.idToken!! )
51
+ setContentView(R .layout.activity_main)
52
+ setupNavigationBar()
53
+ } catch (e: ApiException ) {
54
+ // Google Sign In failed
55
+ Log .w(tag, " Google sign in failed" , e)
56
+ }
57
+ } else {
58
+ Log .w(tag, " Google sign in unsuccessful" )
59
+ }
60
+
61
+ }
62
+ // No other requestCode, ignore it.
63
+ }
64
+
65
+
66
+ private fun firebaseAuthWithGoogle (idToken : String ) {
67
+ val credential = GoogleAuthProvider .getCredential(idToken, null )
68
+ auth.signInWithCredential(credential)
69
+ .addOnCompleteListener(this ) { task ->
70
+ if (task.isSuccessful) {
71
+ // Firebase Sign in success, update UI with the signed-in user's information
72
+ Log .d(firebaseTag, " signInWithCredential:success" )
73
+ val user = auth.currentUser
74
+ Log .d(firebaseTag, " signed-in user's Email:" + user!! .email)
75
+ } else {
76
+ // If sign in fails, log a message to the user.
77
+ Log .w(firebaseTag, " signInWithCredential:failure" , task.exception)
78
+ }
79
+ }
80
+ }
81
+
82
+
41
83
private fun setupNavigationBar () {
42
84
val runFragment = RunFragment ()
43
85
supportFragmentManager
@@ -85,78 +127,32 @@ class MainActivity : AppCompatActivity() {
85
127
}
86
128
}
87
129
130
+
88
131
override fun onCreate (savedInstanceState : Bundle ? ) {
89
132
super .onCreate(savedInstanceState)
90
133
setContentView(R .layout.activity_main)
91
134
92
135
googleSignInInit()
93
136
setupNavigationBar()
137
+
94
138
}
95
139
140
+
96
141
override fun onStart () {
97
142
super .onStart()
98
143
val account = GoogleSignIn .getLastSignedInAccount(this )
99
144
if (account == null ) {
100
145
setContentView(R .layout.login_in_page)
101
-
102
146
findViewById<SignInButton >(R .id.sign_in_button).setOnClickListener {
103
147
signIn()
104
148
}
105
-
106
- }
107
- }
108
-
109
- override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
110
- super .onActivityResult(requestCode, resultCode, data)
111
-
112
- // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
113
- if (requestCode == RC_SIGN_IN ) {
114
- val task = GoogleSignIn .getSignedInAccountFromIntent(data)
115
- if (task.isSuccessful) {
116
- try {
117
- // Google Sign In was successful, authenticate with Firebase
118
- val account = task.getResult(ApiException ::class .java)!!
119
- Log .d(" MainActivity-Login" , " firebaseAuthWithGoogle:" + account.id)
120
- firebaseAuthWithGoogle(account.idToken!! )
121
- setContentView(R .layout.activity_main)
122
- setupNavigationBar()
123
- } catch (e: ApiException ) {
124
- // Google Sign In failed, update UI appropriately
125
- Log .w(" MainActivity-Login" , " Google sign in failed" , e)
126
- // ...
127
- }
128
- } else {
129
- Log .w(" MainActivity-Login" , " Google sign in unsuccessful" )
130
- }
131
-
132
149
}
133
150
}
134
151
135
- private fun firebaseAuthWithGoogle (idToken : String ) {
136
- val credential = GoogleAuthProvider .getCredential(idToken, null )
137
- auth.signInWithCredential(credential)
138
- .addOnCompleteListener(this ) { task ->
139
- if (task.isSuccessful) {
140
- // Sign in success, update UI with the signed-in user's information
141
- Log .d(" firebaseAuthWithGoogle" , " signInWithCredential:success" )
142
- val user = auth.currentUser
143
- } else {
144
- // If sign in fails, display a message to the user.
145
- Log .w(" firebaseAuthWithGoogle" , " signInWithCredential:failure" , task.exception)
146
- // ...
147
- }
148
-
149
- // ...
150
- }
151
- }
152
-
153
-
154
152
155
153
companion object {
156
154
private const val RC_SIGN_IN = 0
157
155
}
158
156
159
157
160
-
161
-
162
158
}
0 commit comments