반응형
*firebase 프로젝트 설정 안 되어 있다면
firebase auth 설정
flutter pub add firebase_auth
위 명령어를 통해 firebase auth 라이브러리를 추가하고 put get 을 진행해준다.
FirebaseAuth.instance.authStateChanges().listen((user) {
if (user == null) {
} else {}
});
위의 dart 코드를 통해 유저의 로그인 상태를 확인하고 변화된 이벤트를 받을 수 있다.
StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, AsyncSnapshot<User?> user) {
if (user.hasData) {
return const MainWidget();
} else {
return const LoginWidget();
}
},
);
만약 상태의 변화를 streamBuilder로 받아보려면 해당 코드를 사용하면 된다.
google sign in 설정
firebase console 설정
flutter 설정을 진행하기 전에 firebase에 google auth를 사용하는 설정과 sha 인증서 지문을 등록해야한다.
1. Google auth 사용설정
firebase 콘솔의 Authentication 탭에서 제공업체 google 사용설정을 체크하고 저장
2. sha 인증서 지문 등록
먼저 SHA1 인증서 지문을 추출한다.
// mac/linux
keytool -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
// window
keytool -list -v \
-alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore
flutter project 경로에서 위의 명령어를 입력
비밀번호는 android를 입력하면 SHA1, SHA256 인증서 지문이 출력된다.
firebase 콘솔에서 프로젝트 설정화면에서 하단의 '디지털 지문 추가'를 통해 SHA1 지문을 등록한다.
https://developers.google.com/android/guides/client-auth
그리고 flutter로 돌아와
flutter pub add google_sign_in
위 명령어를 통해 google sign in 라이브러리를 추가하고 put get 을 진행해준다.
Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
위 메서드를 통해 google sign in 진행이 가능하다.
반응형
'Flutter' 카테고리의 다른 글
[Flutter] 매우 쉽게 Flutter 푸시 메세지(FCM) 사용하기 (0) | 2022.04.12 |
---|---|
[FlutterFire] 아주 쉽게 Flutter프로젝트 Firebase 설정 (with firebase CLI) (0) | 2022.04.08 |
[Flutter] Dart에서 const와 final의 차이점 (0) | 2022.04.07 |
[Flutter] GetX를 사용해야할 이유3 - 종속성 관리(의존성 주입, 바인딩) (1) | 2022.04.07 |
[Flutter] GetX를 사용해야할 이유2 - 상태관리 (0) | 2022.04.06 |