Flutter로 앱을 만들다 보면 로그인 기능은 거의 필수다.
그중에서도 Firebase Authentication은 설정이 비교적 간단하면서도 안정적이라 Flutter 앱과 궁합이 좋다.
이번 글에서는 Flutter 앱에 Firebase 로그인을 연동하는 전체 과정을 정리해본다.
이메일/비밀번호 로그인부터 Google 로그인까지, 실제 개발 흐름 기준으로 설명한다.
Flutter Firebase 로그인, 왜 많이 쓰일까?
Flutter 앱에서 Firebase 로그인을 사용하는 이유는 명확하다.
- 서버 없이도 인증 구현 가능
- 이메일 / Google / Apple / 익명 로그인 지원
- 보안과 세션 관리가 Firebase에서 자동 처리
- Flutter 공식 패키지 지원
특히 개인 프로젝트나 MVP 단계에서는 Firebase 로그인 하나만으로 충분한 경우가 많다.
1️⃣ Firebase 프로젝트 생성하기
Flutter Firebase 로그인의 첫 단계는 Firebase 콘솔에서 프로젝트를 만드는 것이다.
- Firebase 콘솔 접속
- 새 프로젝트 생성
- Google Analytics는 선택 사항
프로젝트가 생성되면 Android / iOS 앱을 각각 등록해야 한다.
2️⃣ Flutter 앱 Firebase 연결 설정
Android 설정
- Android 앱 추가
- 패키지 이름 입력 (예: com.example.myapp)
- google-services.json 다운로드
- android/app 폴더에 추가
build.gradle 설정도 반드시 확인한다.
classpath 'com.google.gms:google-services:4.3.15'
apply plugin: 'com.google.gms.google-services'
iOS 설정
- iOS 앱 추가
- Bundle ID 입력
- GoogleService-Info.plist 다운로드
- Xcode 프로젝트에 추가
iOS는 Firebase 초기화 누락으로 오류가 나는 경우가 많으니 주의해야 한다.
3️⃣ Flutter Firebase 패키지 설치
Flutter 앱에서 Firebase 로그인을 사용하려면 아래 패키지가 필요하다.
flutter pub add firebase_core
flutter pub add firebase_auth
flutter pub add google_sign_in
그리고 main.dart에서 Firebase를 초기화한다.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
이 단계가 빠지면 앱 실행 시 바로 크래시가 난다.
4️⃣ 이메일 / 비밀번호 로그인 구현
Firebase Authentication에서 가장 기본이 되는 방식이다.
회원가입
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
로그인
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
Firebase 로그인은 예외 처리가 매우 중요하다.
try {
// 로그인
} on FirebaseAuthException catch (e) {
print(e.code);
}
wrong-password, user-not-found 같은 에러 코드를 UI에 맞게 처리해주면 된다.
5️⃣ Google 로그인 연동하기
Flutter Firebase 로그인에서 가장 많이 쓰는 소셜 로그인이다.
Firebase 콘솔 설정
- Authentication → 로그인 방법
- Google 로그인 활성화
Flutter 코드
final googleUser = await GoogleSignIn().signIn();
final googleAuth = await googleUser!.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
Android는 SHA-1 등록을 잊지 말아야 한다.
이걸 빠뜨리면 Google 로그인 버튼을 눌러도 반응이 없다.
6️⃣ 로그인 상태 유지 (자동 로그인)
Firebase 로그인은 기본적으로 세션을 유지한다.
FirebaseAuth.instance.authStateChanges().listen((user) {
if (user == null) {
// 로그인 안 됨
} else {
// 로그인 상태
}
});
이 방식을 사용하면 앱 실행 시 로그인 여부에 따라
로그인 화면 / 메인 화면을 자연스럽게 분기할 수 있다.
7️⃣ Flutter Firebase 로그인 구조 추천
실무나 개인 프로젝트에서 추천하는 구조는 아래와 같다.
- AuthService : FirebaseAuth 관련 로직 분리
- LoginPage : UI만 담당
- AuthProvider 또는 StateNotifier : 상태 관리
Firebase 로그인 로직을 UI에 직접 쓰기 시작하면
나중에 유지보수가 정말 힘들어진다.
마무리
Flutter 앱에서 Firebase 로그인은 한 번만 제대로 구조를 잡아두면
이메일 로그인, Google 로그인, Apple 로그인까지 쉽게 확장할 수 있다.
정리하면,
- Firebase 프로젝트 생성
- Flutter 앱 연동
- firebase_auth 패키지 사용
- 로그인 상태 분기 처리
이 흐름만 기억해두면 Flutter Firebase 로그인은 어렵지 않다.
'Flutter' 카테고리의 다른 글
| Dart 중급자 가이드: 언어 설계를 이해하면 코드가 달라진다 (0) | 2025.12.16 |
|---|---|
| Dart 언어 완벽 가이드 | 플러터 개발자를 위한 필수 기초 (0) | 2025.11.04 |
| Dart 형 체계(Type System) (0) | 2022.12.19 |
댓글