-
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
5437e55
commit 7e1378e
Showing
26 changed files
with
405 additions
and
292 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
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.
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,8 @@ | ||
import 'package:aranduapp/core/network/token_manager/repository/auth_repository.dart'; | ||
import 'package:aranduapp/core/network/token_manager/service/auth_service.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
|
||
void setupAuthDI() { | ||
GetIt.I.registerLazySingleton(() => AuthRepository()); | ||
GetIt.I.registerFactory(() => AuthService()); | ||
} |
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,41 @@ | ||
import 'dart:convert'; | ||
|
||
class UserModel { | ||
final String id; | ||
final String name; | ||
final String email; | ||
final String role; | ||
|
||
UserModel({ | ||
required this.id, | ||
required this.name, | ||
required this.email, | ||
required this.role, | ||
}); | ||
|
||
UserModel.fromMap(Map<String, dynamic> json) | ||
: id = json['userId']! as String, | ||
name = json['name']! as String, | ||
email = json['email']! as String, | ||
role = json['role']! as String; | ||
|
||
factory UserModel.fromJsonString(String jsonString) { | ||
Map<String, dynamic> json = jsonDecode(jsonString); | ||
return UserModel( | ||
id: json['userId']! as String, | ||
name: json['name']! as String, | ||
email: json['email']! as String, | ||
role: json['role']! as String, | ||
); | ||
} | ||
|
||
String toJson() { | ||
return jsonEncode({ | ||
'userId': id, | ||
'name': name, | ||
'email': email, | ||
'role': role, | ||
}); | ||
} | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
lib/core/network/token_manager/repository/auth_repository.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:aranduapp/core/network/token_manager/model/user_model.dart'; | ||
import 'package:aranduapp/core/network/token_manager/service/auth_service.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
|
||
class AuthRepository { | ||
UserModel? _userCache; | ||
|
||
Future<UserModel> getUser() async { | ||
if (_userCache != null) { | ||
return _userCache!; | ||
} else { | ||
try { | ||
UserModel user = await GetIt.instance<AuthService>().getUser(); | ||
_userCache = user; | ||
return user; | ||
} catch (e) { | ||
throw Exception('Erro ao obter o usuário'); | ||
} | ||
} | ||
} | ||
|
||
Future<void> clearUser() async { | ||
_userCache = null; | ||
} | ||
} |
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,55 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:aranduapp/core/data/local/storage_value.dart'; | ||
import 'package:aranduapp/core/log/log.dart'; | ||
import 'package:aranduapp/core/network/auth_api.dart'; | ||
import 'package:aranduapp/core/network/token_manager/model/refresh_token_response.dart'; | ||
import 'package:aranduapp/core/network/token_manager/model/user_model.dart'; | ||
import 'package:dio/dio.dart'; | ||
|
||
class AuthService { | ||
Future<RefreshTokenResponse> refreshToken() async { | ||
String? refresh = await StorageValue.getInstance().getRefreshToken(); | ||
|
||
assert(refresh != null); | ||
|
||
Response response = await AuthApi.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<Response> validateToken() async { | ||
return await AuthApi.getInstance(auth: true) | ||
.get(path: '/auth/validate-token'); | ||
} | ||
|
||
Future<UserModel> getUser() async { | ||
String? json = await StorageValue.getInstance().getJsonUser(); | ||
|
||
if (json != null) { | ||
return UserModel.fromJsonString(json); | ||
} else { | ||
final response = await validateToken(); | ||
|
||
Log.f(response); | ||
|
||
Map<String, dynamic> json = jsonDecode(response.toString()); | ||
|
||
UserModel user = UserModel.fromMap(json['userPayload']!); | ||
|
||
await StorageValue.getInstance().setJsonUser(user.toJson()); | ||
|
||
return user; | ||
} | ||
} | ||
} |
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,9 +1,9 @@ | ||
import 'package:aranduapp/core/network/base_api.dart'; | ||
import 'package:aranduapp/core/network/auth_api.dart'; | ||
import 'package:aranduapp/ui/edit_password/model/edit_password_request.dart'; | ||
|
||
class EditPasswordService { | ||
Future<void> edit(EditPasswordRequest editPasswordRequest) async { | ||
await BaseApi.getInstance(auth: true) | ||
await AuthApi.getInstance(auth: true) | ||
.put(path: '/auth/change-password', data: editPasswordRequest.toJson()); | ||
} | ||
} |
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,9 +1,9 @@ | ||
import 'package:aranduapp/core/network/base_api.dart'; | ||
import 'package:aranduapp/core/network/auth_api.dart'; | ||
import 'package:aranduapp/ui/edit_profile/model/edit_profile_request.dart'; | ||
|
||
class EditProfileService { | ||
Future<void> edit(EditProfileRequest editProfileRequest) async { | ||
await BaseApi.getInstance(auth: true) | ||
await AuthApi.getInstance(auth: true) | ||
.patch(path: '/users', data: editProfileRequest.toJson()); | ||
} | ||
} |
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
Oops, something went wrong.