Carelkat

Login Activity

Apr 26th, 2022 (edited)
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.21 KB | None | 0 0
  1. package carel.twee.happyhourapp
  2.  
  3. import android.content.Intent
  4. import android.os.Bundle
  5. import android.util.Log
  6. import android.view.View
  7. import android.widget.CheckBox
  8. import android.widget.Toast
  9. import androidx.appcompat.app.AppCompatActivity
  10. import com.google.android.gms.auth.api.signin.GoogleSignIn
  11. import com.google.android.gms.auth.api.signin.GoogleSignInOptions
  12. import com.google.android.gms.common.api.ApiException
  13. import com.google.firebase.auth.FirebaseAuth
  14. import com.google.firebase.auth.FirebaseUser
  15. import com.google.firebase.auth.GoogleAuthProvider
  16. import com.google.firebase.auth.ktx.auth
  17. import com.google.firebase.ktx.Firebase
  18.  
  19. class LoginActivity : AppCompatActivity() {
  20.  
  21.     private companion object {
  22.         private const val TAG = "LoginActivity"
  23.         private const val RC_GOOGLE_SIGN_IN= 4915
  24.     }
  25.  
  26.     private lateinit var auth: FirebaseAuth
  27.     private lateinit var checkBox: CheckBox
  28.  
  29.  
  30.     override fun onCreate(savedInstanceState: Bundle?) {
  31.         super.onCreate(savedInstanceState)
  32.         setContentView(R.layout.activity_login)
  33.  
  34.         auth = Firebase.auth
  35.  
  36.             // Configure Google Sign In
  37.             val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  38.                 .requestIdToken(getString(R.string.default_web_client_id))
  39.                 .requestEmail()
  40.                 .build()
  41.             val client = GoogleSignIn.getClient(this, gso)
  42.             findViewById<View>(R.id.btnSignIn)?.setOnClickListener {
  43.                 val signInIntent = client.signInIntent
  44.                 startActivityForResult(signInIntent, RC_GOOGLE_SIGN_IN)
  45.             }
  46.     }
  47.  
  48.     override fun onStart() {
  49.         super.onStart()
  50.         // Check if user is signed in (non-null) and update UI accordingly.
  51.         val currentUser = auth.currentUser
  52.         updateUI(currentUser)
  53.     }
  54.  
  55.     private fun updateUI(user: FirebaseUser?) {
  56.         //Navigate to MainActivity
  57.         if (user == null){
  58.             Log.w(TAG, "User is null, not going to navigate")
  59.             return
  60.         } else {
  61.             val mCheckBox = findViewById<CheckBox>(R.id.check_box_18)
  62.             if (mCheckBox.isChecked) {
  63.                 startActivity(Intent(this, MainActivity::class.java))
  64.                 finish()
  65.             } else {
  66.                 Toast.makeText(
  67.                     applicationContext,
  68.                     "Please confirm you are 18+ years old",
  69.                     Toast.LENGTH_SHORT
  70.                 ).show()
  71.             }
  72.         }
  73.  
  74.     }
  75.  
  76.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  77.         super.onActivityResult(requestCode, resultCode, data)
  78.  
  79.         // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
  80.         if (requestCode == RC_GOOGLE_SIGN_IN) {
  81.             val task = GoogleSignIn.getSignedInAccountFromIntent(data)
  82.             try {
  83.                 // Google Sign In was successful, authenticate with Firebase
  84.                 val account = task.getResult(ApiException::class.java)!!
  85.                 Log.d(TAG, "firebaseAuthWithGoogle:" + account.id)
  86.                 firebaseAuthWithGoogle(account.idToken!!)
  87.             } catch (e: ApiException) {
  88.                 // Google Sign In failed, update UI appropriately
  89.                 Log.w(TAG, "Google sign in failed", e)
  90.             }
  91.         }
  92.     }
  93.  
  94.     private fun firebaseAuthWithGoogle(idToken: String) {
  95.         val credential = GoogleAuthProvider.getCredential(idToken, null)
  96.         auth.signInWithCredential(credential)
  97.             .addOnCompleteListener(this) { task ->
  98.                 if (task.isSuccessful) {
  99.                     // Sign in success, update UI with the signed-in user's information
  100.                     Log.d(TAG, "signInWithCredential:success")
  101.                     val user = auth.currentUser
  102.                     updateUI(user)
  103.                 } else {
  104.                     // If sign in fails, display a message to the user.
  105.                     Log.w(TAG, "signInWithCredential:failure", task.exception)
  106.                     Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
  107.                     updateUI(null)
  108.                 }
  109.             }
  110.     }
  111. }
Add Comment
Please, Sign In to add comment