-
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.
fix(#59): corrige e melhora api do aplicativo
- Loading branch information
1 parent
33c817d
commit 62a0856
Showing
11 changed files
with
137 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
import 'package:aranduapp/core/data/local/StorageValue.dart'; | ||
import 'package:aranduapp/core/log/Log.dart'; | ||
import 'package:aranduapp/core/network/token_manager/auth_service.dart'; | ||
import 'package:aranduapp/core/network/token_manager/model/refresh_token_response.dart'; | ||
import 'package:dio/dio.dart'; | ||
|
||
class AppInterceptors extends Interceptor { | ||
@override | ||
Future<void> onRequest( | ||
RequestOptions options, RequestInterceptorHandler handler) async { | ||
String? token = await StorageValue.getInstance().getAuthToken(); | ||
|
||
Log.w(token); | ||
|
||
if (token != null) { | ||
options.headers['Authorization'] = token; | ||
} else { | ||
Log.w('Token não encontrado'); | ||
} | ||
|
||
super.onRequest(options, handler); | ||
} | ||
|
||
@override | ||
Future onError(DioException err, ErrorInterceptorHandler handler) async { | ||
if (err.response?.statusCode == 401) { | ||
try { | ||
Log.i('Token expirado. Tentando atualizar o token...'); | ||
|
||
RefreshTokenResponse tokens = | ||
await AuthService().refreshToken(); | ||
|
||
final requestOptions = err.requestOptions; | ||
|
||
requestOptions.headers['Authorization'] = 'Bearer ${tokens.authToken}'; | ||
|
||
final response = await Dio().request( | ||
requestOptions.path, | ||
options: Options( | ||
method: requestOptions.method, | ||
headers: requestOptions.headers, | ||
), | ||
data: requestOptions.data, | ||
); | ||
|
||
Log.i('Token expirado atualizado.'); | ||
return handler.resolve(response); | ||
} catch (e) { | ||
super.onError(err, handler); | ||
} | ||
} | ||
} | ||
} |
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,31 @@ | ||
import 'package:aranduapp/core/data/local/StorageValue.dart'; | ||
import 'package:aranduapp/core/log/Log.dart'; | ||
import 'package:aranduapp/core/network/base_api.dart'; | ||
import 'package:aranduapp/core/network/token_manager/model/refresh_token_response.dart'; | ||
import 'package:dio/dio.dart'; | ||
|
||
class AuthService { | ||
Future<RefreshTokenResponse> refreshToken() async { | ||
String? refresh = await StorageValue.getInstance().getRefreshToken(); | ||
|
||
assert(refresh != null); | ||
|
||
Response response = await BaseApi.getInstance(auth: false).post( | ||
path: '/auth/refresh', | ||
data: <String, dynamic>{'refreshToken': refresh}); | ||
|
||
RefreshTokenResponse tokens = | ||
RefreshTokenResponse.fromJsonString(response.toString()); | ||
|
||
await StorageValue.getInstance().setAuthToken(tokens.authToken!); | ||
await StorageValue.getInstance().setRefreshToken(tokens.refreshToken!); | ||
|
||
Log.i("Tokens atualizados com sucesso."); | ||
|
||
return tokens; | ||
} | ||
|
||
Future<void> validateToken() async { | ||
await BaseApi.getInstance(auth: true).get(path: '/auth/validate-token'); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
lib/core/network/token_manager/model/refresh_token_response.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,18 @@ | ||
import 'dart:convert'; | ||
|
||
class RefreshTokenResponse { | ||
|
||
String? authToken; | ||
String? refreshToken; | ||
|
||
RefreshTokenResponse(this.authToken, this.refreshToken); | ||
|
||
factory RefreshTokenResponse.fromJsonString(String jsonString) { | ||
Map<String, dynamic> json = jsonDecode(jsonString); | ||
return RefreshTokenResponse( | ||
json['accessToken'] as String?, | ||
json['refreshToken'] as String? | ||
); | ||
} | ||
|
||
} |
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 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 |
---|---|---|
@@ -1,20 +1,14 @@ | ||
import 'package:aranduapp/core/network/BaseApi.dart'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:aranduapp/core/network/base_api.dart'; | ||
import '../model/RegisterRequest.dart'; | ||
|
||
class RegisterService { | ||
|
||
static Future<void> register(RegisterRequest registerRequest) async { | ||
|
||
await BaseApi.getInstance() | ||
await BaseApi.getInstance(auth: false) | ||
.post(path: '/users', data: <String, dynamic>{ | ||
'name': registerRequest.name, | ||
'email': registerRequest.email, | ||
'username': registerRequest.userName, | ||
'password': registerRequest.password, | ||
}); | ||
|
||
} | ||
|
||
} | ||
|