-
Notifications
You must be signed in to change notification settings - Fork 0
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
cde3ba0
commit 5f1b663
Showing
30 changed files
with
618 additions
and
454 deletions.
There are no files selected for viewing
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,17 @@ | ||
class UnauthenticatedException implements Exception { | ||
final String message; | ||
|
||
UnauthenticatedException(this.message); | ||
|
||
@override | ||
String toString() => 'UnauthenticatedException: $message'; | ||
} | ||
|
||
class ServerException implements Exception { | ||
final String message; | ||
|
||
ServerException(this.message); | ||
|
||
@override | ||
String toString() => 'ServerException: $message'; | ||
} |
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,33 @@ | ||
// lib/data/datasources/authenticated_http_client.dart | ||
|
||
import 'package:domain/repositories_abstract/token_provider.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
import '../core/exceptions.dart'; | ||
|
||
class AuthenticatedHttpClient extends http.BaseClient { | ||
final http.Client _inner; | ||
final TokenProvider _tokenProvider; | ||
|
||
AuthenticatedHttpClient(this._inner, this._tokenProvider); | ||
|
||
@override | ||
Future<http.StreamedResponse> send(http.BaseRequest request) async { | ||
final token = await _tokenProvider.getToken(); | ||
if (token != null) { | ||
request.headers['Authorization'] = 'Bearer $token'; | ||
} | ||
|
||
var response = await _inner.send(request); | ||
|
||
if (response.statusCode == 401) { | ||
// Token is invalid or expired, delete it | ||
await _tokenProvider.deleteToken(); | ||
|
||
// Notify the app or handle as needed | ||
throw UnauthenticatedException('Token expired'); | ||
} | ||
|
||
return response; | ||
} | ||
} |
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,24 @@ | ||
// lib/data/providers/token_provider_impl.dart | ||
|
||
import 'package:domain/repositories_abstract/token_provider.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
|
||
class TokenProviderImpl implements TokenProvider { | ||
final FlutterSecureStorage _secureStorage = FlutterSecureStorage(); | ||
static const _tokenKey = 'accessToken'; | ||
|
||
@override | ||
Future<void> saveToken(String token) async { | ||
await _secureStorage.write(key: _tokenKey, value: token); | ||
} | ||
|
||
@override | ||
Future<String?> getToken() async { | ||
return await _secureStorage.read(key: _tokenKey); | ||
} | ||
|
||
@override | ||
Future<void> deleteToken() async { | ||
await _secureStorage.delete(key: _tokenKey); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// lib/domain/providers/token_provider.dart | ||
|
||
abstract class TokenProvider { | ||
Future<void> saveToken(String token); | ||
Future<String?> getToken(); | ||
Future<void> deleteToken(); | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.