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

Commit 2b1723c

Browse files
get UserID
1 parent aa21ca3 commit 2b1723c

File tree

4 files changed

+152
-13
lines changed

4 files changed

+152
-13
lines changed

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

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
package com.google.samples.quickstart.canonical
22

3+
import android.content.Context
4+
import android.content.Intent
35
import androidx.appcompat.app.AppCompatActivity
46
import android.os.Bundle
7+
import android.util.AttributeSet
8+
import android.util.Log
9+
import android.view.View
10+
import android.widget.Button
511
import androidx.fragment.app.FragmentTransaction
12+
import com.google.android.gms.auth.api.signin.GoogleSignIn
13+
import com.google.android.gms.auth.api.signin.GoogleSignInClient
14+
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
15+
import com.google.android.gms.common.SignInButton
16+
import com.google.android.gms.common.api.ApiException
617
import com.google.android.material.bottomnavigation.BottomNavigationView
18+
import com.google.android.material.snackbar.Snackbar
19+
import com.google.firebase.auth.FirebaseAuth
20+
import com.google.firebase.auth.GoogleAuthProvider
721

822
class MainActivity : AppCompatActivity() {
23+
private lateinit var mGoogleSignInClient: GoogleSignInClient
24+
private lateinit var auth: FirebaseAuth
925

10-
override fun onCreate(savedInstanceState: Bundle?) {
11-
super.onCreate(savedInstanceState)
12-
setContentView(R.layout.activity_main)
26+
private fun googleSignInInit() {
27+
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
28+
.requestIdToken(getString(R.string.default_web_client_id))
29+
.requestEmail()
30+
.build()
31+
32+
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
33+
}
1334

35+
private fun signIn() {
36+
auth = FirebaseAuth.getInstance()
37+
val signInIntent = mGoogleSignInClient.signInIntent
38+
startActivityForResult(signInIntent, RC_SIGN_IN)
39+
}
40+
41+
private fun setupNavigationBar() {
1442
val runFragment = RunFragment()
1543
supportFragmentManager
1644
.beginTransaction()
@@ -57,6 +85,78 @@ class MainActivity : AppCompatActivity() {
5785
}
5886
}
5987

88+
override fun onCreate(savedInstanceState: Bundle?) {
89+
super.onCreate(savedInstanceState)
90+
setContentView(R.layout.activity_main)
91+
92+
googleSignInInit()
93+
setupNavigationBar()
94+
}
95+
96+
override fun onStart() {
97+
super.onStart()
98+
val account = GoogleSignIn.getLastSignedInAccount(this)
99+
if (account == null) {
100+
setContentView(R.layout.login_in_page)
101+
102+
findViewById<SignInButton>(R.id.sign_in_button).setOnClickListener {
103+
signIn()
104+
}
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+
}
133+
}
134+
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+
155+
companion object {
156+
private const val RC_SIGN_IN = 0
157+
}
158+
159+
60160

61161

62162
}

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

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

33
import android.os.Bundle
4+
import android.util.Log
45
import androidx.fragment.app.Fragment
56
import android.view.LayoutInflater
67
import android.view.View
78
import android.view.ViewGroup
9+
import com.google.android.gms.auth.api.signin.GoogleSignIn
10+
import com.google.android.gms.auth.api.signin.GoogleSignInClient
811
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
12+
import com.google.firebase.auth.FirebaseAuth
913

1014
// TODO: Rename parameter arguments, choose names that match
1115
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
@@ -21,6 +25,8 @@ class MeFragment : Fragment() {
2125
// TODO: Rename and change types of parameters
2226
private var param1: String? = null
2327
private var param2: String? = null
28+
private lateinit var mAuth : FirebaseAuth
29+
private lateinit var mGoogleSignInClient : GoogleSignInClient
2430

2531
override fun onCreate(savedInstanceState: Bundle?) {
2632
super.onCreate(savedInstanceState)
@@ -34,9 +40,12 @@ class MeFragment : Fragment() {
3440
// .requestIdToken(getString(R.string.default_web_client_id))
3541
// .requestEmail()
3642
// .build()
37-
43+
//
44+
// mGoogleSignInClient = GoogleSignIn.getClient(this.context!!, gso)
45+
// mAuth = FirebaseAuth.getInstance()
3846
}
3947

48+
4049
override fun onCreateView(
4150
inflater: LayoutInflater, container: ViewGroup?,
4251
savedInstanceState: Bundle?
@@ -45,6 +54,16 @@ class MeFragment : Fragment() {
4554
return inflater.inflate(R.layout.fragment_me, container, false)
4655
}
4756

57+
override fun onStart() {
58+
super.onStart()
59+
val account = GoogleSignIn.getLastSignedInAccount(this.context)
60+
account?.let {
61+
Log.i("MeFragment", "No login")
62+
} ?.run {
63+
Log.i("MeFragment", "Already login")
64+
}
65+
}
66+
4867
companion object {
4968
/**
5069
* Use this factory method to create a new instance of

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

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

20-
<com.google.android.gms.common.SignInButton
21-
android:id="@+id/sign_in_button"
22-
android:layout_width="wrap_content"
23-
android:layout_height="wrap_content"
24-
android:layout_marginBottom="40dp"
25-
app:layout_constraintBottom_toBottomOf="parent"
26-
app:layout_constraintEnd_toEndOf="parent"
27-
app:layout_constraintStart_toStartOf="parent" />
28-
2920
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".MeFragment">
8+
9+
<!-- TODO: Update blank fragment layout -->
10+
<TextView
11+
android:id="@+id/textView"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
android:gravity="center"
15+
android:text="This is Login Page"
16+
android:textSize="30sp"
17+
app:layout_constraintBottom_toBottomOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
20+
<com.google.android.gms.common.SignInButton
21+
android:id="@+id/sign_in_button"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:layout_marginBottom="40dp"
25+
app:layout_constraintBottom_toBottomOf="parent"
26+
app:layout_constraintEnd_toEndOf="parent"
27+
app:layout_constraintStart_toStartOf="parent" />
28+
29+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)