728x90
반응형
SMALL
파이어베이스 구글 로그인
파이어베이스는 구글 (Google)이 소유하고 있는 모바일 애플리케이션 개발 플랫폼이다. 구글 로그인 인증을 구현하기 위해 파이어베이스 구성 파일과 SDK를 추가한다. 프로젝트를 위해 login 페이지와 login 후의 페이지를 구성한다.
build.gradle
implementation platform('com.google.firebase:firebase-bom:31.1.1')
implementation 'com.google.firebase:firebase-auth-ktx:21.1.0'
implementation 'com.google.firebase:firebase-storage-ktx:20.0.1'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.android.gms:play-services-auth:20.4.0'
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<com.google.android.gms.common.SignInButton
android:id="@+id/btnSignIn"
android:layout_width="271dp"
android:layout_height="66dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
class : login
import android.content.Intent
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
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.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
class intro : AppCompatActivity() {
lateinit var googleSignInClient: GoogleSignInClient
lateinit var auth : FirebaseAuth
lateinit var imageview : ImageView
val binding by lazy {ActivityIntroBinding.inflate(layoutInflater)}
var googleLoginReult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
result ->
var data = result.data
var task = GoogleSignIn.getSignedInAccountFromIntent(data)
val account = task.getResult(ApiException::class.java)
firebaseAuthWithGoogle(account.idToken)
}
fun firebaseAuthWithGoogle(idToken: String?){
var credential = GoogleAuthProvider.getCredential(idToken, null)
auth.signInWithCredential((credential)).addOnCompleteListener{
task ->
if(task.isSuccessful){
moveMainPage(task.result?.user)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
auth = FirebaseAuth.getInstance()
binding.btnSignIn.setOnClickListener{
signIn()
}
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.requestProfile()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
}
private fun signIn() {
val i = googleSignInClient.signInIntent
googleLoginReult.launch(i)
}
//로그인 activity에서 Main으로
fun moveMainPage(user: FirebaseUser?){
if (user != null){
startActivity(Intent(this,MainActivity::class.java))
}
}
}
728x90
반응형
LIST
'App Programming > Kotlin' 카테고리의 다른 글
[Kotlin] timePicker (0) | 2023.01.17 |
---|---|
[Kotlin] 구글 자동 로그인 (0) | 2023.01.15 |
[Kotlin] 파이어베이스 (Firebase) (0) | 2023.01.15 |
[Kotlin] ImageView (0) | 2023.01.12 |
[Kotlin] TextView (0) | 2023.01.12 |