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

Commit 7f05d40

Browse files
committed
docs: update README with usage example
- Added example using repository - Updated imports for example - Added sign in methods examples - Added sign out example - Added delete account example
1 parent 70f4481 commit 7f05d40

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

README.md

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,59 +45,62 @@ This repository provides a standardized way to handle user authentication. It ab
4545
);
4646
```
4747

48-
4. **Use the repository in your BLoCs:**
49-
50-
Inject the `HtAuthenticationRepository` instance into your BLoCs and use its methods to handle authentication-related events.
51-
5248
## Example
5349

5450
```dart
55-
// Example of using the repository in a BLoC
56-
class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState> {
57-
AuthenticationBloc({
58-
required HtAuthenticationRepository authenticationRepository,
59-
}) : _authenticationRepository = authenticationRepository,
60-
super(const AuthenticationState.unknown()) {
61-
on<AuthenticationStatusChanged>(_onAuthenticationStatusChanged);
62-
on<AuthenticationLogoutRequested>(_onAuthenticationLogoutRequested);
63-
64-
_authenticationStatusSubscription = _authenticationRepository.status
65-
.listen((status) => add(AuthenticationStatusChanged(status)));
51+
import 'package:ht_authentication_client/ht_authentication_client.dart';
52+
import 'package:ht_authentication_repository/ht_authentication_repository.dart';
53+
54+
void main() async {
55+
// Initialize the authentication client (replace with your actual client).
56+
final authenticationClient = HtAuthenticationClient();
57+
58+
// Initialize the authentication repository.
59+
final authenticationRepository = HtAuthenticationRepository(
60+
authenticationClient: authenticationClient,
61+
);
62+
63+
// Access the user stream.
64+
authenticationRepository.user.listen((user) {
65+
print('User: ${user.id}');
66+
});
67+
68+
// Sign in with email and password (replace with actual credentials).
69+
try {
70+
await authenticationRepository.signInWithEmailAndPassword(
71+
email: 'test@example.com',
72+
password: 'password',
73+
);
74+
} catch (e) {
75+
print('Sign in failed: $e');
76+
}
77+
78+
// Sign in with google.
79+
try {
80+
await authenticationRepository.signInWithGoogle();
81+
} catch (e) {
82+
print('Sign in failed: $e');
6683
}
6784
68-
final HtAuthenticationRepository _authenticationRepository;
69-
late StreamSubscription<AuthenticationStatus> _authenticationStatusSubscription;
70-
71-
Future<void> _onAuthenticationStatusChanged(
72-
AuthenticationStatusChanged event,
73-
Emitter<AuthenticationState> emit,
74-
) async {
75-
switch (event.status) {
76-
case AuthenticationStatus.unauthenticated:
77-
return emit(const AuthenticationState.unauthenticated());
78-
case AuthenticationStatus.authenticated:
79-
final user = await _authenticationRepository.user;
80-
return emit(
81-
user != null
82-
? AuthenticationState.authenticated(user)
83-
: const AuthenticationState.unauthenticated(),
84-
);
85-
case AuthenticationStatus.unknown:
86-
return emit(const AuthenticationState.unknown());
87-
}
85+
// Sign in anonymously.
86+
try {
87+
await authenticationRepository.signInAnonymously();
88+
} catch (e) {
89+
print('Sign in failed: $e');
8890
}
8991
90-
Future<void> _onAuthenticationLogoutRequested(
91-
AuthenticationLogoutRequested event,
92-
Emitter<AuthenticationState> emit,
93-
) async {
94-
_authenticationRepository.logOut();
92+
// Sign out.
93+
try {
94+
await authenticationRepository.signOut();
95+
} catch (e) {
96+
print('Sign out failed: $e');
9597
}
9698
97-
@override
98-
Future<void> close() {
99-
_authenticationStatusSubscription.cancel();
100-
return super.close();
99+
// Delete Account.
100+
try {
101+
await authenticationRepository.deleteAccount();
102+
} catch (e) {
103+
print('Delete account failed: $e');
101104
}
102105
}
103106

0 commit comments

Comments
 (0)