Skip to content

Commit

Permalink
feat: add fresh package for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-struk-surf committed Apr 4, 2024
1 parent 1a8e773 commit f0d2757
Show file tree
Hide file tree
Showing 25 changed files with 160 additions and 388 deletions.
39 changes: 39 additions & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Authentication

The guide describes token-based authentication which relies on **access** and **refresh** tokens.

## Fresh

The recommended approach is to use the `fresh` package.

[**Fresh**](https://pub.dev/packages/fresh) is a token refresh library for dart. This library consists of a collection of packages which specialize in a particular aspect of token refresh.

Project contains preconfigured classes for `fresh_dio` package to make its usage even more easier.

Example:
```dart
final fresh = Fresh<AuthTokenPair>(
tokenHeader: (token) => {'Authorization': 'Bearer ${token.accessToken}'},
tokenStorage: const TokenStorageImpl(FlutterSecureStorage()),
refreshToken: (options, handler) async { ... },
);
// Add interceptor to Dio instance.
dio.interceptors.add(fresh);
// Listen to authentication status changes.
fresh.authenticationStatus.listen((status) { ... });
// Set token.
fresh.setToken(token);
// Clear token.
fresh.clearToken();
```
## Token Storage

`TokenStorageImpl` is an implementation of `TokenStorage<AuthTokenPair>` from `fresh` package which relies on `flutter_secure_storage`.

## Auth Tokens

`AuthTokenPair` is a class that represents a pair of access and refresh tokens. It is used by `TokenStorageImpl`.
135 changes: 0 additions & 135 deletions docs/authorization.md

This file was deleted.

117 changes: 0 additions & 117 deletions lib/common/interceptors/auth_interceptor.dart

This file was deleted.

1 change: 1 addition & 0 deletions lib/common/mixin/theme_wm_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mixin ThemeWMMixin<W extends ElementaryWidget, M extends ElementaryModel> on Wid

@override
void didChangeDependencies() {
super.didChangeDependencies();
_colorScheme = AppColorScheme.of(context);
_textScheme = AppTextScheme.of(context);
}
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions lib/common/widgets/di_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ typedef ScopeFactory<T> = T Function(BuildContext context);
class DiScope<T> extends SingleChildStatefulWidget {
/// Create an instance [DiScope].
const DiScope({
required this.onFactory,
required this.factory,
this.onDispose,
super.key,
super.child,
});

/// Factory that returns the dependency scope.
final ScopeFactory<T> onFactory;
final ScopeFactory<T> factory;

/// The method called when disposing the widget.
final ValueChanged<T>? onDispose;
Expand All @@ -32,7 +32,7 @@ class _DiScopeState<T> extends SingleChildState<DiScope<T>> {
@override
void initState() {
super.initState();
_scope = widget.onFactory(context);
_scope = widget.factory(context);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/features/app/app_flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AppFlow extends StatelessWidget {
Widget build(BuildContext context) {
return Nested(
children: [
DiScope<IAppScope>(onFactory: (_) => appScope),
DiScope<IAppScope>(factory: (_) => appScope),
ChangeNotifierProvider<AppRouter>(create: (_) => AppRouter()),
const ThemeModeProvider(),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/features/debug/presentation/debug/debug_flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DebugFlow extends StatelessWidget implements AutoRouteWrapper {
@override
Widget wrappedRoute(BuildContext context) {
return DiScope<IDebugScope>(
onFactory: DebugScope.create,
factory: DebugScope.create,
onDispose: (scope) => scope.dispose(),
child: this,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FeatureExampleFlow extends StatelessWidget implements AutoRouteWrapper {
@override
Widget wrappedRoute(BuildContext context) {
return DiScope<IFeatureExampleScope>(
onFactory: FeatureExampleScope.create,
factory: FeatureExampleScope.create,
onDispose: (scope) => scope.dispose(),
child: this,
);
Expand Down
Empty file.
10 changes: 0 additions & 10 deletions lib/features/shared/domain/repositories/auth_repository.dart

This file was deleted.

Loading

0 comments on commit f0d2757

Please sign in to comment.