-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db0df6d
commit d9f1ddd
Showing
24 changed files
with
619 additions
and
666 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
lib/features/authentication/data/datasources/authentication_local_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:coffeecard/features/authentication/data/models/authenticated_user_model.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:logger/logger.dart'; | ||
|
||
class AuthenticationLocalDataSource { | ||
static const _authenticatedUserKey = 'authenticated_user'; | ||
|
||
final FlutterSecureStorage storage; | ||
final Logger logger; | ||
|
||
const AuthenticationLocalDataSource({ | ||
required this.storage, | ||
required this.logger, | ||
}); | ||
|
||
Future<void> saveAuthenticatedUser( | ||
AuthenticatedUserModel authenticatedUser, | ||
) async { | ||
await storage.write( | ||
key: _authenticatedUserKey, | ||
value: json.encode(authenticatedUser), | ||
); | ||
|
||
logger.d('$authenticatedUser added to storage'); | ||
} | ||
|
||
Future<Option<AuthenticatedUserModel>> getAuthenticatedUser() async { | ||
final jsonString = await storage.read(key: _authenticatedUserKey); | ||
|
||
if (jsonString == null) { | ||
return const None(); | ||
} | ||
|
||
final user = AuthenticatedUserModel.fromJson( | ||
json.decode(jsonString) as Map<String, dynamic>, | ||
); | ||
|
||
return Some(user); | ||
} | ||
|
||
Future<void> clearAuthenticatedUser() async { | ||
await storage.delete(key: _authenticatedUserKey); | ||
logger.d('deleted data for $_authenticatedUserKey'); | ||
} | ||
|
||
Future<void> updateToken(String token) async { | ||
final user = await getAuthenticatedUser(); | ||
|
||
user.map( | ||
(user) async { | ||
final model = AuthenticatedUserModel( | ||
email: user.email, | ||
token: token, | ||
encodedPasscode: user.encodedPasscode, | ||
); | ||
|
||
await saveAuthenticatedUser(model); | ||
}, | ||
); | ||
} | ||
} |
16 changes: 7 additions & 9 deletions
16
lib/features/authentication/data/intercepters/authentication_interceptor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:chopper/chopper.dart'; | ||
import 'package:coffeecard/core/storage/secure_storage.dart'; | ||
import 'package:coffeecard/features/authentication/data/datasources/authentication_local_data_source.dart'; | ||
|
||
class AuthenticationInterceptor implements RequestInterceptor { | ||
final SecureStorage _storage; | ||
final AuthenticationLocalDataSource localDataSource; | ||
|
||
AuthenticationInterceptor(this._storage); | ||
AuthenticationInterceptor(this.localDataSource); | ||
|
||
/// Try retrieve authentication token from storage and add authentication header if exists | ||
@override | ||
FutureOr<Request> onRequest(Request request) async { | ||
final token = await _storage.readToken(); | ||
final user = await localDataSource.getAuthenticatedUser(); | ||
|
||
if (token != null) { | ||
return user.match(() => request, (user) { | ||
final updatedHeaders = Map.of(request.headers); | ||
updatedHeaders['Authorization'] = 'Bearer $token'; | ||
updatedHeaders['Authorization'] = 'Bearer ${user.token}'; | ||
|
||
return request.copyWith(headers: updatedHeaders); | ||
} | ||
|
||
return request; | ||
}); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
lib/features/authentication/data/models/authenticated_user_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:coffeecard/features/authentication/domain/entities/authenticated_user.dart'; | ||
|
||
class AuthenticatedUserModel extends AuthenticatedUser { | ||
const AuthenticatedUserModel({ | ||
required super.email, | ||
required super.token, | ||
required super.encodedPasscode, | ||
}); | ||
|
||
factory AuthenticatedUserModel.fromJson(Map<String, dynamic> json) { | ||
return AuthenticatedUserModel( | ||
email: json['email'] as String, | ||
token: json['token'] as String, | ||
encodedPasscode: json['passcode'] as String, | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'email': email, | ||
'token': token, | ||
'passcode': encodedPasscode, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
lib/features/authentication/domain/usecases/clear_authenticated_user.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:coffeecard/features/authentication/data/datasources/authentication_local_data_source.dart'; | ||
|
||
class ClearAuthenticatedUser { | ||
final AuthenticationLocalDataSource dataSource; | ||
|
||
ClearAuthenticatedUser({required this.dataSource}); | ||
|
||
Future<void> call() async { | ||
await dataSource.clearAuthenticatedUser(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
lib/features/authentication/domain/usecases/get_authenticated_user.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:coffeecard/features/authentication/data/datasources/authentication_local_data_source.dart'; | ||
import 'package:coffeecard/features/authentication/domain/entities/authenticated_user.dart'; | ||
import 'package:fpdart/fpdart.dart'; | ||
|
||
class GetAuthenticatedUser { | ||
final AuthenticationLocalDataSource dataSource; | ||
|
||
GetAuthenticatedUser({required this.dataSource}); | ||
|
||
Future<Option<AuthenticatedUser>> call() async { | ||
return dataSource.getAuthenticatedUser(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
lib/features/authentication/domain/usecases/save_authenticated_user.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:coffeecard/features/authentication/data/datasources/authentication_local_data_source.dart'; | ||
import 'package:coffeecard/features/authentication/data/models/authenticated_user_model.dart'; | ||
|
||
class SaveAuthenticatedUser { | ||
final AuthenticationLocalDataSource dataSource; | ||
|
||
SaveAuthenticatedUser({required this.dataSource}); | ||
|
||
Future<void> call({ | ||
required String email, | ||
required String token, | ||
required String encodedPasscode, | ||
}) async { | ||
return dataSource.saveAuthenticatedUser( | ||
AuthenticatedUserModel( | ||
email: email, | ||
token: token, | ||
encodedPasscode: encodedPasscode, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.