Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package carel.twee.happyhourapp
- import android.content.Intent
- import android.os.Bundle
- import android.util.Log
- import android.view.View
- import android.widget.CheckBox
- import android.widget.Toast
- import androidx.appcompat.app.AppCompatActivity
- import com.google.android.gms.auth.api.signin.GoogleSignIn
- import com.google.android.gms.auth.api.signin.GoogleSignInOptions
- import com.google.android.gms.common.api.ApiException
- import com.google.firebase.auth.FirebaseAuth
- import com.google.firebase.auth.FirebaseUser
- import com.google.firebase.auth.GoogleAuthProvider
- import com.google.firebase.auth.ktx.auth
- import com.google.firebase.ktx.Firebase
- class LoginActivity : AppCompatActivity() {
- private companion object {
- private const val TAG = "LoginActivity"
- private const val RC_GOOGLE_SIGN_IN= 4915
- }
- private lateinit var auth: FirebaseAuth
- private lateinit var checkBox: CheckBox
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_login)
- auth = Firebase.auth
- // Configure Google Sign In
- val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build()
- val client = GoogleSignIn.getClient(this, gso)
- findViewById<View>(R.id.btnSignIn)?.setOnClickListener {
- val signInIntent = client.signInIntent
- startActivityForResult(signInIntent, RC_GOOGLE_SIGN_IN)
- }
- }
- override fun onStart() {
- super.onStart()
- // Check if user is signed in (non-null) and update UI accordingly.
- val currentUser = auth.currentUser
- updateUI(currentUser)
- }
- private fun updateUI(user: FirebaseUser?) {
- //Navigate to MainActivity
- if (user == null){
- Log.w(TAG, "User is null, not going to navigate")
- return
- } else {
- val mCheckBox = findViewById<CheckBox>(R.id.check_box_18)
- if (mCheckBox.isChecked) {
- startActivity(Intent(this, MainActivity::class.java))
- finish()
- } else {
- Toast.makeText(
- applicationContext,
- "Please confirm you are 18+ years old",
- Toast.LENGTH_SHORT
- ).show()
- }
- }
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
- if (requestCode == RC_GOOGLE_SIGN_IN) {
- val task = GoogleSignIn.getSignedInAccountFromIntent(data)
- try {
- // Google Sign In was successful, authenticate with Firebase
- val account = task.getResult(ApiException::class.java)!!
- Log.d(TAG, "firebaseAuthWithGoogle:" + account.id)
- firebaseAuthWithGoogle(account.idToken!!)
- } catch (e: ApiException) {
- // Google Sign In failed, update UI appropriately
- Log.w(TAG, "Google sign in failed", e)
- }
- }
- }
- private fun firebaseAuthWithGoogle(idToken: String) {
- val credential = GoogleAuthProvider.getCredential(idToken, null)
- auth.signInWithCredential(credential)
- .addOnCompleteListener(this) { task ->
- if (task.isSuccessful) {
- // Sign in success, update UI with the signed-in user's information
- Log.d(TAG, "signInWithCredential:success")
- val user = auth.currentUser
- updateUI(user)
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "signInWithCredential:failure", task.exception)
- Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
- updateUI(null)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment