Android/Firebase

Google Auth

lipnus 2019. 1. 30. 14:18
반응형

Google Login 

https://grokonez.com/android/kotlin-firebase-authentication-google-sign-in-android

https://medium.com/@myric.september/authenticate-using-google-sign-in-kotlin-firebase-4490f71d9e44




Build.Gradle(Project: ~)

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}


Build.Gradle(Module: app)

//Google Play Service
implementation 'com.google.android.gms:play-services-auth:11.6.2'

//Firebase
implementation 'com.google.firebase:firebase-auth:11.6.2'
implementation 'com.google.firebase:firebase-core:11.6.2'




GoogleAuth.kt (Activity)


class GoogleAuth : BaseActivity(), GoogleApiClient.OnConnectionFailedListener {

//테스트
//-----------------------------------------------------------------------
val RC_SIGN_IN: Int = 1
lateinit var mGoogleSignInClient: GoogleSignInClient
lateinit var mGoogleSignInOptions: GoogleSignInOptions
//-----------------------------------------------------------------------



override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(com.lipnus.R.layout.
activity_googleAuth)

configureGoogleSignIn()
initLayout()

mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(
this) {
// [START_EXCLUDE]
Log.d("SSS", "revokeAccess()")
// [END_EXCLUDE]
}


}

private fun configureGoogleSignIn() {

mGoogleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(
"670975308456-s2pohalo6nhlktuhn8rke1ige00s66rj.apps.googleusercontent.com")
.requestEmail()
.build()

mGoogleSignInClient = GoogleSignIn.getClient(this, mGoogleSignInOptions)
}

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


override fun onStart() {
super.onStart()

val account = GoogleSignIn.getLastSignedInAccount(this)


if(account!=null) {
Log.d("SSS", "accout존재")
}else{
Log.d("SSS", "accout없음")
}
}


override fun initLayout() {

//구글로그인 버튼
intro_google_btn.setOnClickListener {
signIn()
}


}




override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent();

// presenter.getGoogleSignInRequestCode()
if (requestCode == 1) {
val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)

if (result.isSuccess) {
// successful -> authenticate with Firebase
val account = result.signInAccount
Log.d("SSS", "구글토큰아이디: " + account!!.idToken)
Log.d("SSS", "이메일: " + account!!.email)

} else {
// failed -> update UI
// updateUI(null)
}
}
}



override fun onConnectionFailed(p0: ConnectionResult) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}


}

mGoogleSignInClient.revokeAccess()

이걸 해줘야 선택창이 새로 뜬다. 안그러면 전에 했던 로그인 그대로 실행함.

이메일을 가져오는 용도로만 사용한다.


반응형

'Android > Firebase' 카테고리의 다른 글

Cloud Firestore Database 시작하기  (0) 2019.06.17
Google SignIn  (0) 2019.06.10
Android에 Firebase 파일 추가  (0) 2019.01.08
[Firebase] Cloud Firestore OR과 LIKE구현하기  (0) 2018.08.02
[Firebase] CloudStore 복합쿼리  (0) 2018.08.01