#이 포스팅은 플린이의 입장에서 쓰여진 글입니다. 코드 지적은 언제나 환영입니다. 🙆🏻♂️
이 프로젝트는 'Youtube The net ninja'의 flutter firebase tutorial 강의를 참고했습니다.
로그인 차례입니다!
사용자는 서버에 등록된 email, password로 로그인할 수 있어요.
로그인은 회원가입과 아주아주 유사해요.
코드를 그대로 복사해서 붙여넣기 해도될 정도에요.
구현했던 Sign up method를 볼게요.
AuthService.dart
Future signUpWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User user = result.user;
return _userFromFirebaseUser(user);
} catch (e) {
print('sign up failed');
print(e.toString());
return null;
}
}
여기서 method 하나만 바꾸면 돼요.
Future signInWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.signInWithEmailAndPassword(
email: email, password: password);
User user = result.user;
return _userFromFirebaseUser(user);
} catch (e) {
print('sign in failed');
print(e.toString());
return null;
}
}
이제 Login button에 트리거해보도록 할게요.
sign_in_screen.dart
RaisedButton(
child: Text(
'SIGN IN',
style: TextStyle(color: Colors.white),
),
color: Colors.pink[400],
onPressed: () async {
if (_formKey.currentState.validate()) {
dynamic result = await _auth
.signInWithEmailAndPassword(email, password);
if (result == null) {
setState(() => error =
'could not sign in with those credentials');
}
}
})
로그인도 끝!
베리 이지! 🕺