이번에는 구글 아이디로 로그인을 만들어보겠습니다.
여기에서 구글 로그인 설정을 하고
아래와 같이 코드를 추가해줍니다.
package com.bd.baemin_before_video.Auth
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.bd.baemin_before_video.MainActivity
import com.bd.baemin_before_video.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.api.ApiException
import com.google.android.material.snackbar.Snackbar
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.activity_join.*
class JoinActivity : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var googleSignInClient: GoogleSignInClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_join)
// Configure Google Sign In
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
auth = FirebaseAuth.getInstance()
join_login_button.setOnClickListener {
auth.createUserWithEmailAndPassword(join_email_area.text.toString(), join_password_area.text.toString())
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
val intent = Intent(this, JoinInfoActivity::class.java)
startActivity(intent)
} else {
// If sign in fails, display a message to the user.
Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show()
}
}
}
google_button.setOnClickListener {
Log.e(TAG, "google")
signIn()
}
}
private fun signIn() {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
public 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)
firebaseAuthWithGoogle(account!!)
val intent = Intent(this, JoinInfoActivity::class.java)
startActivity(intent)
} catch (e: ApiException) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e)
// ...
}
}
}
private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.id!!)
val credential = GoogleAuthProvider.getCredential(acct.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
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.exception)
}
// ...
}
}
companion object {
private const val TAG = "GoogleActivity"
private const val RC_SIGN_IN = 9001
}
}
그 다음, mycominActivity에서 logout버튼을 만들어서 기능을 만들어주고
이러고 나면 구글 로그인도 완성!
'인프런 - 강의 > 하 - 코딩의 민족 만들기 (Android kotlin)' 카테고리의 다른 글
9 - 프로필 이미지 설정 (0) | 2019.11.12 |
---|---|
8 - 이미지 업로드 다운로드 (0) | 2019.11.08 |
6 - 찜 기능 구현 3 (0) | 2019.11.07 |
5 - 찜 기능 구현 2 (0) | 2019.11.05 |
4 - 찜 기능 구현 (0) | 2019.11.05 |