Summary - Firebase Auth
- Flutter course
- Advanced
- Lesson 20/69
- 4 min
Combine signup, signin and authStateChanges to make an auth flow.
Auth flow
An auth flow combines registration, sign-in, and state listening. First a user is created with createUserWithEmailAndPassword, then signed in with signInWithEmailAndPassword. The authStateChanges stream catches every state change and updates the UI. For sign-out, signOut is called.
await auth.createUserWithEmailAndPassword(
email: 'a@b.com', password: '123456');
final cred = await auth.signInWithEmailAndPassword(
email: 'a@b.com', password: '123456');
print(cred.user.email);
await auth.signOut();Practice Write a complete auth flow
Get the auth instance. First register with createUserWithEmailAndPassword. Then sign in with signInWithEmailAndPassword and print the email. Finally call signOut and print 'yok' if currentUser is null.
Starter code
class User {
final String email;
User(this.email);
}
class UserCredential {
final User user;
UserCredential(this.user);
}
class FirebaseAuth {
static final FirebaseAuth instance = FirebaseAuth._();
FirebaseAuth._();
User? _user;
User? get currentUser => _user;
Future<UserCredential> createUserWithEmailAndPassword({
required String email,
required String password,
}) async {
_user = User(email);
return UserCredential(_user!);
}
Future<UserCredential> signInWithEmailAndPassword({
required String email,
required String password,
}) async {
_user = User(email);
return UserCredential(_user!);
}
Future<void> signOut() async {
_user = null;
}
Stream<User?> authStateChanges() async* {
yield _user;
}
}
void main() async {
// kayıt metodu ile kullanıcı oluştur
// giriş metodu ile oturum aç, e-postayı yazdır
// çıkış metodu ile oturumu kapat, 'yok' yazdır
}Sample solution
class User {
final String email;
User(this.email);
}
class UserCredential {
final User user;
UserCredential(this.user);
}
class FirebaseAuth {
static final FirebaseAuth instance = FirebaseAuth._();
FirebaseAuth._();
User? _user;
User? get currentUser => _user;
Future<UserCredential> createUserWithEmailAndPassword({
required String email,
required String password,
}) async {
_user = User(email);
return UserCredential(_user!);
}
Future<UserCredential> signInWithEmailAndPassword({
required String email,
required String password,
}) async {
_user = User(email);
return UserCredential(_user!);
}
Future<void> signOut() async {
_user = null;
}
Stream<User?> authStateChanges() async* {
yield _user;
}
}
void main() async {
final auth = FirebaseAuth.instance;
await auth.createUserWithEmailAndPassword(
email: 'a@b.com',
password: '123456',
);
final cred = await auth.signInWithEmailAndPassword(
email: 'a@b.com',
password: '123456',
);
print(cred.user.email);
await auth.signOut();
print(auth.currentUser?.email ?? 'yok');
}Expected output
a@b.com yok
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is used to continuously monitor the signed-in user?
- Listening to the authStateChanges stream Correct answer
- Calling signOut
- Calling createUserWithEmailAndPassword
- Reading currentUser once
Explanation: The authStateChanges stream emits on every auth change, making it suitable for continuous monitoring; currentUser is a one-time read.
2 Close the session
await auth.____();
await auth.signOut();
Explanation: To end the session you call signOut; currentUser becomes null.
3 Auth flow parts
| Term | Meaning |
|---|---|
createUserWithEmailAndPassword |
Registration |
signInWithEmailAndPassword |
Sign in |
authStateChanges |
State listening |
signOut |
Sign out |