본문 바로가기

플러터(Flutter)

Flutter firebase 로그인 연동

1. firebase project 설정

프로젝트 선택 후 빌드 -> Authentication 선택

 

시작하기 버튼 클릭 후 이메일/비밀번호 클릭 후, 사용 설정한 후에 저장 선

2. flutter 파일 수정

2-1. 플러그인 추가

auth 관련 플러그인 추가

flutter pub add firebase_auth
flutterfire configure

 

2-2. flutter 프로젝트 코드 수정

auth 파일을 import 후, 인스턴스 생성

import 'package:firebase_auth/firebase_auth.dart';
...
  final _authentication = FirebaseAuth.instance;
...

 

2-3. user 생성하기

try {
  final newUser = await _authentication.createUserWithEmailAndPassword(
    email: email,
    password: password,
  );
  if (newUser.user != null) {
    // user 생성 성공
  }
}
catch (e) {
  print(e)
}

 

2-4. 기존 user 정보로 로그인하기

    try {
      final account = await _authentication.signInWithEmailAndPassword(
        email: userId,
        password: userPw,
      );

      if (account.user != null) {
        return true;	// 로그인 성공
      }
    }
    catch (e) {
      print(e);
    }

 

2-5. 현재 로그인 되어있는 user 정보 가져오기

이때 user 정보는 _authentication.signOut(); 을 수행하지 않는 이상, 로그인 후에는 어디에서 호출해도 같은 user 정보가 나타난다.

추후 이를 이용해 user의 고유값인 uid를 알아내고 활용할 수 있다.

final curUser = _authentication.currentUser;

'플러터(Flutter)' 카테고리의 다른 글

StreamBuilder를 통해 chatting message 가져오기  (0) 2023.06.22
Firestore에 데이터 저장하는 방법  (0) 2023.06.15
Firestore db 권한 설정  (0) 2023.06.15
Flutter Firestore 연동  (0) 2023.06.12
Flutter와 Firebase 연동  (0) 2023.05.22