[ 작성시 개발 환경 ]
- Window 7
- Android Studio 4.0.1
들어가기에 앞서...
Firebase 에서는 앱에서 사용자 관리를 개발자가 쉽고 편하게 등록할 수 있도록 다양한 인터페이스를 제공합니다. 구글, 트위터, 전화번호, 페이스북 등 유명한 소셜 및 기타 계정을 통해서 자유롭게 사용자 계정을 관리할 수 있습니다. 당연하게도 앱 별로 말이죠.

이후로는 구글 로그인에 한하여 블로그를 작성해보았습니다. 중간중간에 빠진 부분도 있겠지만, 저자도 공식문서를 참고하여 제작하였으니, 막히는 부분이 있다면 참조 섹터를 확인해서 진행바랍니다.
1) Firebase 연동하기
1-1) Firebase Console 접속
로그인 - Google 계정
하나의 계정으로 모든 Google 서비스를 Google 계정으로 로그인
accounts.google.com
1-2) Firebase 프로젝트 생성하기
앱에 연결되는 Firebase Project 이므로 이름을 앱 이름과 비슷하게 진행하여도 좋습니다.


1-3) 생성한 앱에서 SHA-1 추출


1-4) 프로젝트를 앱과 연동
1-1) 페이지에서 진행합니다.




그 후 앱에 인터넷 권한을 추가한 뒤 실기기에 실행시켜 앱과 연동 확인
1-5) 로그인 방법 설정하기


※ 참고 : 사용자 등록 한도 및 가격 정책 확인
Firebase 인증 한도
다음 인증 작업은 실행 빈도가 제한되어 있습니다. 이러한 제한은 사전 통보 없이 변경될 수 있습니다. 특수한 사용 사례에 대해 논의하려면 Firebase 지원팀에 문의하시기 바랍니다. 계정 생성 및
firebase.google.com
2. Google API에 통합 계정 생성하기
2-1) 구글 통합 프로젝트 생성 페이지 접속
Start Integrating Google Sign-In into Your Android App
Before you can start integrating Google Sign-In in your own app, you must configure a Google API Console project and set up your Android Studio project. The steps on this page do just that. The next steps then describe how to integrate Google Sign-In into
developers.google.com



3. 앱에 로그인 페이지 구성
3-1) 앱 수준 build.gradle 의 dependencies 에 firebase 연동

※ 뷰바인딩을 사용한다면

3-2) 액티비티 레이아웃 구성 - 버튼 추가
<com.google.android.gms.common.SignInButton | |
android:id="@+id/google_button" | |
android:layout_width="300dp" | |
android:layout_height="?attr/actionBarSize" | |
android:layout_marginTop="?attr/actionBarSize" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
android:layout_marginBottom="84dp" | |
/> |

3-3) 구글 클라이언트 초기화
private lateinit var binding: ActivitySignInBinding | |
private lateinit var auth: FirebaseAuth | |
private lateinit var googleSignInClient: GoogleSignInClient | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// View 설정 | |
binding = ActivitySignInBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
// 클라이언트에 넣을 구글 로그인 옵션 설정 | |
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() | |
// 클릭 확인 | |
binding.googleButton.setOnClickListener(this) | |
} |
3-4) 버튼 클릭시 로그인 Intent 시작
override fun onClick(v: View?) { | |
when(v?.id) | |
{ | |
R.id.google_button -> signIn(GOOGLE_SIGN_IN) | |
} | |
} | |
private fun signIn(type : Int) | |
{ | |
when(type) | |
{ | |
GOOGLE_SIGN_IN-> startActivityForResult(googleSignInClient.signInIntent, type) | |
} | |
} |
3-5) onActivityResult에서 결과값 수신
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if( requestCode == GOOGLE_SIGN_IN ) | |
{ | |
val task = GoogleSignIn.getSignedInAccountFromIntent(data) | |
try | |
{ | |
// 로그인 성공 확인 | |
val account = task.getResult(ApiException::class.java)!! | |
Log.d(TAG, "Google Sign in :" + account.id ) | |
firebaseAuthWithGoogle(account.idToken!!) | |
}catch (e:ApiException) | |
{ | |
// 연결 실패 | |
Log.w(TAG, "Google sign in failed ", e) | |
showFailSnackbar() | |
} | |
} | |
} | |
private fun firebaseAuthWithGoogle(idToken: String) { | |
val credential = GoogleAuthProvider.getCredential(idToken, null) | |
auth.signInWithCredential(credential) | |
.addOnCompleteListener(this) { task -> | |
if (task.isSuccessful) { | |
// 스플레시 액티비티로 구성하여 성공한다면 finish() 동작 수행 | |
// Sign in success, update UI with the signed-in user's information | |
Log.d(TAG, "signInWithCredential:success") | |
finish() | |
} else { | |
// If sign in fails, display a message to the user. | |
Log.w(TAG, "signInWithCredential:failure", task.exception) | |
showFailSnackbar() | |
} | |
} | |
} |
3-6) 실제 로그인 되어 있는지는 다음과 같이 확인
// Sign in Check | |
if( FirebaseAuth.getInstance().currentUser == null ) | |
{ | |
val intent = Intent(this, SignInActivity::class.java) | |
intent.addFlags(FLAG_ACTIVITY_NO_HISTORY) | |
startActivity(intent) | |
return | |
} |
4. 실제 화면

※ 구글 API를 통해서 다양한 로그인 접속 방법이 소개되어 있습니다. 여기에서는 구글 로그인만 소개하고 있습니다.
[참조]
1) 구글로 로그인하기 공식 문서 : firebase.google.com/docs/auth/android/google-signin?hl=ko
2) SHA-1 적용 : goni9071.tistory.com/489
3) 안드로이드에 구글 로그인 통합 시작 : developers.google.com/identity/sign-in/android/start-integrating#get_your_backend_servers_oauth_20_client_id

'Android Story' 카테고리의 다른 글
[Android] Bluetooth BLE 연동 가이드 (1) | 2021.05.04 |
---|---|
[Android] ViewPager2 Animation By RecyclerView (4) | 2020.11.02 |
[Android] Android 10 Image 관리 (2) | 2020.08.13 |
[Android] string.xml 정규표현식으로 문자열 및 ID 추출 (0) | 2020.03.25 |
[Android] 다중 라인 수정 (0) | 2019.05.31 |
[NDK] OpenSSL CrossCompile (2) | 2019.05.22 |
[Android] Switch Custom (0) | 2019.01.15 |
[Android ] 안드로이드 UI 디자인, Framer (1) | 2017.12.22 |