Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 1d5f0ee

Browse files
committed
feat(auth): add email link sign-in
- Implemented sendSignInLinkToEmail - Implemented signInWithEmailLink - Propagated client exceptions - Wrapped generic exceptions
1 parent 30f6937 commit 1d5f0ee

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

lib/src/ht_authentication_repository.dart

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import 'package:ht_authentication_client/ht_authentication_client.dart';
22
import 'package:rxdart/rxdart.dart';
33

44
/// {@template ht_authentication_repository}
5-
/// Repository which manages authentication.
5+
/// Repository which manages authentication using various providers.
6+
/// It abstracts the underlying [HtAuthenticationClient].
67
/// {@endtemplate}
78
class HtAuthenticationRepository {
89
/// {@macro ht_authentication_repository}
@@ -26,22 +27,41 @@ class HtAuthenticationRepository {
2627
/// Defaults to a default [User] if there is no cached user.
2728
User get currentUser => _userSubject.value;
2829

29-
/// Signs in with email and password.
30+
/// Sends a sign-in link to the provided email address.
3031
///
31-
/// Throws an [EmailSignInException] if sign-in fails.
32-
Future<void> signInWithEmailAndPassword({
32+
/// Throws a [SendSignInLinkException] if sending the link fails.
33+
Future<void> sendSignInLinkToEmail({required String email}) async {
34+
try {
35+
await _authenticationClient.sendSignInLinkToEmail(email: email);
36+
} on SendSignInLinkException {
37+
rethrow; // Re-throw specific client exceptions directly
38+
} catch (e, st) {
39+
// Wrap generic exceptions
40+
throw SendSignInLinkException(e, st);
41+
}
42+
}
43+
44+
/// Signs in the user using the email and the validated sign-in link.
45+
///
46+
/// Throws an [InvalidSignInLinkException] if the link is invalid or expired.
47+
/// Throws a [UserNotFoundException] if the email is not found.
48+
Future<void> signInWithEmailLink({
3349
required String email,
34-
required String password,
50+
required String emailLink,
3551
}) async {
3652
try {
37-
await _authenticationClient.signInWithEmailAndPassword(
53+
await _authenticationClient.signInWithEmailLink(
3854
email: email,
39-
password: password,
55+
emailLink: emailLink,
4056
);
41-
} on EmailSignInException catch (e, st) {
42-
throw EmailSignInException(e, st);
57+
} on InvalidSignInLinkException {
58+
rethrow; // Re-throw specific client exceptions directly
59+
} on UserNotFoundException {
60+
rethrow; // Re-throw specific client exceptions directly
4361
} catch (e, st) {
44-
throw EmailSignInException(e, st);
62+
// Wrap generic exceptions, defaulting to InvalidSignInLinkException
63+
// as the most likely failure mode if not UserNotFound.
64+
throw InvalidSignInLinkException(e, st);
4565
}
4666
}
4767

@@ -51,9 +71,10 @@ class HtAuthenticationRepository {
5171
Future<void> signInWithGoogle() async {
5272
try {
5373
await _authenticationClient.signInWithGoogle();
54-
} on GoogleSignInException catch (e, st) {
55-
throw GoogleSignInException(e, st);
74+
} on GoogleSignInException {
75+
rethrow; // Re-throw specific client exceptions directly
5676
} catch (e, st) {
77+
// Wrap generic exceptions
5778
throw GoogleSignInException(e, st);
5879
}
5980
}
@@ -64,9 +85,10 @@ class HtAuthenticationRepository {
6485
Future<void> signInAnonymously() async {
6586
try {
6687
await _authenticationClient.signInAnonymously();
67-
} on AnonymousLoginException catch (e, st) {
68-
throw AnonymousLoginException(e, st);
88+
} on AnonymousLoginException {
89+
rethrow; // Re-throw specific client exceptions directly
6990
} catch (e, st) {
91+
// Wrap generic exceptions
7092
throw AnonymousLoginException(e, st);
7193
}
7294
}
@@ -77,9 +99,10 @@ class HtAuthenticationRepository {
7799
Future<void> signOut() async {
78100
try {
79101
await _authenticationClient.signOut();
80-
} on LogoutException catch (e, st) {
81-
throw LogoutException(e, st);
102+
} on LogoutException {
103+
rethrow; // Re-throw specific client exceptions directly
82104
} catch (e, st) {
105+
// Wrap generic exceptions
83106
throw LogoutException(e, st);
84107
}
85108
}
@@ -90,9 +113,10 @@ class HtAuthenticationRepository {
90113
Future<void> deleteAccount() async {
91114
try {
92115
await _authenticationClient.deleteAccount();
93-
} on DeleteAccountException catch (e, st) {
94-
throw DeleteAccountException(e, st);
116+
} on DeleteAccountException {
117+
rethrow; // Re-throw specific client exceptions directly
95118
} catch (e, st) {
119+
// Wrap generic exceptions
96120
throw DeleteAccountException(e, st);
97121
}
98122
}

0 commit comments

Comments
 (0)