Google認証


公式ドキュメントに沿って進めていく↓
[Android で Google ログインを使用して認証する]
(https://firebase.google.com/docs/auth/android/google-signin?hl=ja)

SHA証明書フィンガープリント作成↓
[Authenticating Your Client](https://developers.google.com/android/guides/client-auth)

```
% keytool -exportcert -alias androiddebugkey -keys
No Java runtime present, requesting install.
```

javaが入っていなければインストール↓

[MacOS JDKのインストール方法](https://maysatsuki.com/mac/jdk/90)

AndroidアプリへのGoogleログインの統合↓
[Integrating Google Sign-In into Your Android App]
(https://developers.google.com/identity/sign-in/android/sign-in?hl=ja)

```kotlin


class LoginActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth
    private var user: FirebaseUser? = null

    private lateinit var mGoogleSignInClient: GoogleSignInClient

    override fun onCreate(savedInstanceState: Bundle?) {

        auth = FirebaseAuth.getInstance()
        user = FirebaseAuth.getInstance().currentUser

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build()

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso)

        sign_in_button.setSize(SignInButton.SIZE_STANDARD)

        sign_in_button.setOnClickListener{onLoginButtonClick(it)}

        }

    override fun onStart() {
        super.onStart()

        val currentUser = auth.currentUser
    }

    fun onLoginButtonClick(view: View?){
        if(view != null) {
            when (view.id) {
                R.id.sign_in_button -> {
                    signIn()
                }
            }
        }
    }

    private fun signIn() {
        val signInIntent:Intent= mGoogleSignInClient.signInIntent
        startActivityForResult(signInIntent, RC_SIGN_IN)
    }


    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_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 ->
//ログイン成功　userにIDトークンを設定
                if (task.isSuccessful) {
                    Log.d(TAG, "signInWithCredential:success")
                    val user = auth.currentUser
                } else {
                    Log.w(TAG, "signInWithCredential:failure", task.exception)
                }
            }
    }

    companion object {
        private const val TAG = "GoogleActivity"
        private const val RC_SIGN_IN = 9001
    }
}
```