2024-04-29 14:36:25 +02:00
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
|
|
|
2024-07-12 13:14:09 +02:00
|
|
|
import '../swipe_stream_service.dart';
|
|
|
|
|
2024-04-29 14:36:25 +02:00
|
|
|
class AuthService {
|
2024-06-17 20:39:56 +02:00
|
|
|
// instance of auth
|
2024-04-29 14:36:25 +02:00
|
|
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
2024-04-30 19:25:16 +02:00
|
|
|
|
|
|
|
// get current user
|
|
|
|
User? getCurrentUser() {
|
|
|
|
return _auth.currentUser;
|
|
|
|
}
|
2024-04-29 14:36:25 +02:00
|
|
|
|
2024-05-18 00:24:10 +02:00
|
|
|
/// sign in (login)
|
|
|
|
///
|
|
|
|
/// @throws FirebaseAuthException
|
2024-06-18 22:02:46 +02:00
|
|
|
Future<UserCredential> signInWithEmailPassword(String email, String password) async {
|
2024-04-29 14:36:25 +02:00
|
|
|
try {
|
|
|
|
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
);
|
2024-04-30 19:25:16 +02:00
|
|
|
|
2024-04-29 14:36:25 +02:00
|
|
|
return userCredential;
|
2024-05-18 00:24:10 +02:00
|
|
|
} on FirebaseAuthException {
|
|
|
|
rethrow;
|
2024-04-29 14:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 00:24:10 +02:00
|
|
|
/// sign up (register)
|
|
|
|
///
|
|
|
|
/// @throws FirebaseAuthException
|
2024-04-29 14:36:25 +02:00
|
|
|
Future<UserCredential> signUpWithEmailPassword(String email, password) async {
|
|
|
|
try {
|
2024-04-30 19:25:16 +02:00
|
|
|
// create user
|
2024-04-29 14:36:25 +02:00
|
|
|
UserCredential userCredential =
|
|
|
|
await _auth.createUserWithEmailAndPassword(
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
);
|
2024-04-30 19:25:16 +02:00
|
|
|
|
2024-04-29 14:36:25 +02:00
|
|
|
return userCredential;
|
2024-05-18 00:24:10 +02:00
|
|
|
} on FirebaseAuthException {
|
|
|
|
rethrow;
|
2024-04-29 14:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:26:49 +02:00
|
|
|
// sign out
|
2024-04-29 14:36:25 +02:00
|
|
|
Future<void> signOut() async {
|
2024-07-12 13:14:09 +02:00
|
|
|
SwipeStreamService().stopListening();
|
2024-05-19 17:01:06 +02:00
|
|
|
await _auth.signOut();
|
2024-04-29 14:36:25 +02:00
|
|
|
}
|
|
|
|
}
|