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

Commit ac80bfe

Browse files
implemented logout
1 parent c1560e0 commit ac80bfe

File tree

4 files changed

+85
-72
lines changed

4 files changed

+85
-72
lines changed
Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
package com.google.samples.quickstart.canonical
22

3-
import android.content.Context
43
import android.content.Intent
54
import androidx.appcompat.app.AppCompatActivity
65
import android.os.Bundle
7-
import android.util.AttributeSet
86
import android.util.Log
9-
import android.view.View
10-
import android.widget.Button
117
import androidx.fragment.app.FragmentTransaction
128
import com.google.android.gms.auth.api.signin.GoogleSignIn
139
import com.google.android.gms.auth.api.signin.GoogleSignInClient
1410
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
1511
import com.google.android.gms.common.SignInButton
1612
import com.google.android.gms.common.api.ApiException
1713
import com.google.android.material.bottomnavigation.BottomNavigationView
18-
import com.google.android.material.snackbar.Snackbar
1914
import com.google.firebase.auth.FirebaseAuth
2015
import com.google.firebase.auth.GoogleAuthProvider
2116

2217
class MainActivity : AppCompatActivity() {
23-
private lateinit var mGoogleSignInClient: GoogleSignInClient
18+
lateinit var mGoogleSignInClient: GoogleSignInClient
2419
private lateinit var auth: FirebaseAuth
20+
private val tag = "MainActivity-Login"
21+
private val firebaseTag = "MainActivity-Login"
2522

2623
private fun googleSignInInit() {
2724
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
@@ -38,6 +35,51 @@ class MainActivity : AppCompatActivity() {
3835
startActivityForResult(signInIntent, RC_SIGN_IN)
3936
}
4037

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+
4183
private fun setupNavigationBar() {
4284
val runFragment = RunFragment()
4385
supportFragmentManager
@@ -85,78 +127,32 @@ class MainActivity : AppCompatActivity() {
85127
}
86128
}
87129

130+
88131
override fun onCreate(savedInstanceState: Bundle?) {
89132
super.onCreate(savedInstanceState)
90133
setContentView(R.layout.activity_main)
91134

92135
googleSignInInit()
93136
setupNavigationBar()
137+
94138
}
95139

140+
96141
override fun onStart() {
97142
super.onStart()
98143
val account = GoogleSignIn.getLastSignedInAccount(this)
99144
if (account == null) {
100145
setContentView(R.layout.login_in_page)
101-
102146
findViewById<SignInButton>(R.id.sign_in_button).setOnClickListener {
103147
signIn()
104148
}
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-
132149
}
133150
}
134151

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-
154152

155153
companion object {
156154
private const val RC_SIGN_IN = 0
157155
}
158156

159157

160-
161-
162158
}

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.google.samples.quickstart.canonical
22

3+
import android.content.Intent
34
import android.os.Bundle
45
import android.util.Log
56
import androidx.fragment.app.Fragment
67
import android.view.LayoutInflater
78
import android.view.View
89
import android.view.ViewGroup
10+
import android.widget.Button
11+
import android.widget.TextView
912
import com.google.android.gms.auth.api.signin.GoogleSignIn
10-
import com.google.android.gms.auth.api.signin.GoogleSignInClient
11-
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
1213
import com.google.firebase.auth.FirebaseAuth
1314

14-
// TODO: Rename parameter arguments, choose names that match
15-
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
1615
private const val ARG_PARAM1 = "param1"
1716
private const val ARG_PARAM2 = "param2"
17+
private const val meTag = "MeFragment"
1818

1919
/**
2020
* A simple [Fragment] subclass.
@@ -25,24 +25,13 @@ class MeFragment : Fragment() {
2525
// TODO: Rename and change types of parameters
2626
private var param1: String? = null
2727
private var param2: String? = null
28-
private lateinit var mAuth : FirebaseAuth
29-
private lateinit var mGoogleSignInClient : GoogleSignInClient
3028

3129
override fun onCreate(savedInstanceState: Bundle?) {
3230
super.onCreate(savedInstanceState)
3331
arguments?.let {
3432
param1 = it.getString(ARG_PARAM1)
3533
param2 = it.getString(ARG_PARAM2)
3634
}
37-
38-
// // Configure Google Sign In
39-
// val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
40-
// .requestIdToken(getString(R.string.default_web_client_id))
41-
// .requestEmail()
42-
// .build()
43-
//
44-
// mGoogleSignInClient = GoogleSignIn.getClient(this.context!!, gso)
45-
// mAuth = FirebaseAuth.getInstance()
4635
}
4736

4837

@@ -54,16 +43,33 @@ class MeFragment : Fragment() {
5443
return inflater.inflate(R.layout.fragment_me, container, false)
5544
}
5645

46+
47+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
48+
super.onViewCreated(view, savedInstanceState)
49+
val logoutButton : Button = view.findViewById(R.id.logout_button)
50+
logoutButton.setOnClickListener {
51+
// Sign out both Google account and Firebase
52+
FirebaseAuth.getInstance().signOut()
53+
(activity as MainActivity).mGoogleSignInClient.signOut().addOnCompleteListener {
54+
val intent = Intent(context, MainActivity::class.java)
55+
startActivity(intent)
56+
}
57+
}
58+
}
59+
60+
5761
override fun onStart() {
5862
super.onStart()
5963
val account = GoogleSignIn.getLastSignedInAccount(this.context)
6064
account?.let {
61-
Log.i("MeFragment", "No login")
65+
Log.i(meTag, "Already login")
66+
view?.findViewById<TextView>(R.id.textView)?.text = account.displayName
6267
} ?.run {
63-
Log.i("MeFragment", "Already login")
68+
Log.i(meTag, "No login")
6469
}
6570
}
6671

72+
6773
companion object {
6874
/**
6975
* Use this factory method to create a new instance of

android/canonical/app/src/main/res/layout/fragment_me.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@
1717
app:layout_constraintBottom_toBottomOf="parent"
1818
app:layout_constraintTop_toTopOf="parent" />
1919

20+
<Button
21+
android:id="@+id/logout_button"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:text="@string/logout_button_text"
25+
app:layout_constraintBottom_toBottomOf="parent"
26+
app:layout_constraintEnd_toEndOf="parent"
27+
app:layout_constraintStart_toStartOf="parent"
28+
app:layout_constraintTop_toBottomOf="@+id/textView" />
29+
2030
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
<string name="stopped">Stopped</string>
1111
<string name="my_location_title">My location</string>
1212
<string name="cannot_access_location">Cannot access location now. Please Try later</string>
13+
<string name="logout_button_text">Logout</string>
1314
</resources>

0 commit comments

Comments
 (0)