CodeBlanks Logo Icon CodeBlanks

What is Firebase Auth?

Sign users in with Firebase Auth.

What is FirebaseAuth?

FirebaseAuth is Firebase's authentication service; it provides user registration, sign-in, and session management under a single API. You access the singleton instance available everywhere via FirebaseAuth.instance. Through this instance you perform sign-in with email, phone, or Google methods.

final auth = FirebaseAuth.instance;
final user = auth.currentUser;

Session check with currentUser

You access the currently signed-in user with auth.currentUser. It returns a User object if a session is open, or null if not. You use this property to check the user's session status when the app starts.

final user = auth.currentUser;
if (user != null) {
  print(user.email);
}

Practice Check the current user

Get the auth instance via FirebaseAuth.instance. Check currentUser; print 'yok' if there is no session, or the email if there is.

Starter code

main.dart
class User {
  final String email;
  User(this.email);
}

class FirebaseAuth {
  static final FirebaseAuth instance = FirebaseAuth._();
  FirebaseAuth._();
  User? get currentUser => null;
}

void main() {
  // tekil örnek ile auth al
  // mevcut kullanıcıyı kontrol et, yoksa 'yok' yazdır
}

Sample solution

main.dart
class User {
  final String email;
  User(this.email);
}

class FirebaseAuth {
  static final FirebaseAuth instance = FirebaseAuth._();
  FirebaseAuth._();
  User? get currentUser => null;
}

void main() {
  final auth = FirebaseAuth.instance;
  final user = auth.currentUser;
  print(user?.email ?? 'yok');
}

Expected output

yok

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does FirebaseAuth.instance return?

  • A new User object
  • An initialization settings object
  • An error handling object
  • A singleton FirebaseAuth instance across the app Correct answer

Explanation: FirebaseAuth.instance returns the singleton FirebaseAuth instance accessible from anywhere in the app; it is not a new User or a settings object.

2 Access the singleton instance

Question
final auth = ____;
Answer
final auth = FirebaseAuth.instance;

Explanation: FirebaseAuth.instance gives the singleton instance accessible from anywhere in the app; it is not a constructor call.

3 FirebaseAuth parts

Term Meaning
FirebaseAuth.instance Singleton instance
currentUser Signed-in user
FirebaseAuth Authentication service
null When no session

All lessons in Advanced