반응형
Firebase 회원 인증기능
- Firebase Console → 빌드 → Authentication
- 우선 간단하게 이메일/비밀번호를 택하여 인증기능 구현하기
- 이후 shop 페이지에서 인증시 접근 가능하도록 하기
- 셋팅
// shop.dart
// firebase_auth (인증)
import 'package:firebase_auth/firebase_auth.dart';
final auth = FirebaseAuth.instance;
- 회원가입 하기
signUp() async{
try {
var result = await auth.createUserWithEmailAndPassword(
email: 'email',
password: 'password'
);
print(result.user);
} catch (e) {
print(e);
}
}
- singUp() 함수를 실행시키고 Firebase의 Authentication의 user를 확인해보면 회원가입이 잘 되어있는 것을 확인할 수 있다.
- 여기서 유저의 이름도 함께 가입시 넣고 싶다면 아래와 같이 작성하기
signUp() async{
try {
var result = await auth.createUserWithEmailAndPassword(
email: 'test2@naver.com',
password: '123123'
);
result.user?.updateDisplayName('hm'); // <-- 요기
print(result.user);
} catch (e) {
print(e);
}
}
- 잠시 여기서 ?. 으로 물음표랑 마침표를 하는 이유는 무엇일지 짚고 넘어간다면
- 그냥 result.uer.updateDisplayName()을 하게 되면 updateDisplayName이 없을 수도 있기에 미리 에러를 발생하게 된다.
- 즉, null check를 하라는 의미로 간편하게 null check를 하고자 한다면 ?. 을 사용하면 된다.
- 그래서 해당 코드를 해석하자면 updateDisplayName() 가 없다면 null을 남기고 있다면 해당 함수를 실행해라는 의미가 된다.
- 그리고 !. 로 느낌표랑 마침표를 사용할 때는
- 무조건 null이 아닐때 사용하는 문법이 된다.
- 로그인 하기
login() async{
try {
await auth.signInWithEmailAndPassword(
email: 'email',
password: 'password'
);
} catch (e) {
print(e);
}
}
- 로그인 여부 판별하기
- 로그인 유저의 이름을 확인하고자 한다면
- auth.currentUser?.displayName
- 그리고 UID 또한 확인할 수 있다.
- auth.currentUser?.uid
- 여기서 UID란?
- 유저를 구분하는 유니크한 문자열
login() async{ try { await auth.signInWithEmailAndPassword( email: 'test2@naver.com', password: '123123' ); } catch (e) { print(e); } // ============ 아래 코드 ============ if (auth.currentUser?.uid == null) { print('로그인 하슈!!!'); } else { print('로그인 했네!'); } }
- 로그인 유저의 이름을 확인하고자 한다면
- 로그아웃 하는 법
logout() async{
await auth.signOut();
}
- 회원가입시 이메일 확인 후 가입을 시키기 위하거나, 폰 인증을 받고자 하는 등등에 대한 자료는 https://firebase.flutter.dev에서 찾아보면 된다.
'Flutter' 카테고리의 다른 글
[Flutter] 반응형으로 스크린 사이즈 대응하기 & 유용한 패키지 (0) | 2023.08.17 |
---|---|
[Flutter] Firestore 데이터 입출력시 규칙 정하는 법 (0) | 2023.08.16 |
[Flutter] Firebase에 데이터 저장 & 조회 & 삭제 & 수정 (0) | 2023.08.15 |
[Flutter] Firebase 셋팅 (다수 에러 해결 과정) (0) | 2023.08.15 |
[Flutter] 특정 시간 알림 & 주기적인 알림 띄우기 (0) | 2023.08.15 |