Untitled
unknown
kotlin
3 years ago
4.0 kB
10
Indexable
package com.example.kuiz.register
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.kuiz.R
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.SignInButton
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
class LoginScreen : Fragment() {
private lateinit var googleSignInClient: GoogleSignInClient
lateinit var gso: GoogleSignInOptions
private val RC_SIGN_IN =9000
lateinit var signInBtn : SignInButton
private lateinit var mAuth : FirebaseAuth
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_login_screen, container, false)
signInBtn = view.findViewById<SignInButton>(R.id.sign_in_button)
//Configure Google Sign In
gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("569946025589-hbthnpc34bkpgut4pefab490rtvkr0ln.apps.googleusercontent.com")
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(requireContext(),gso)
//Firebase Auth instance
mAuth = FirebaseAuth.getInstance()
//val googleAccount:GoogleSignInAccount?=GoogleSignIn.getLastSignedInAccount(requireContext())
signInBtn.setOnClickListener {
signIn()
}
return view
}
fun signIn(){
val signInIntent: Intent = googleSignInClient.signInIntent
startActivityForResult(signInIntent,RC_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == RC_SIGN_IN){
val task : Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
val exception = task.exception
if(task.isSuccessful){
handleSignInResul(task)
}else{
Log.w("SignInActivity",exception.toString())
}
}
}
private fun handleSignInResul(completedTask: Task<GoogleSignInAccount>){
try {
val account:GoogleSignInAccount = completedTask.getResult(ApiException::class.java)
Log.d("SigninActivity","firebaseAuthWithGoogle:" +account.id)
firebaseAuthWithGoogle(account.idToken!!)
}catch (e: ApiException){
Log.d("SigninActivity","Google sign in failed", e)
}
}
private fun navigateToProfileOptions(){
findNavController().navigate(R.id.action_loginScreen_to_profileOptionsScreen)
}
private fun firebaseAuthWithGoogle(idToken:String){
var credential = GoogleAuthProvider.getCredential(idToken,null)
mAuth.signInWithCredential(credential)
.addOnCompleteListener { task->
if (task.isSuccessful){
//Sign in success
Log.d("SignInActivity", "signInWithCredential:success")
//val user = mAuth.currentUser
navigateToProfileOptions()
}else{
Log.w("SignInActivity", "signInWithCredential:failure", task.exception)
}
}
}
}Editor is loading...