-
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.
Merge branch 'dev' into feat#63/tela_conteudo
- Loading branch information
Showing
43 changed files
with
1,117 additions
and
589 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,44 @@ | ||
import 'dart:convert'; | ||
|
||
class UserModel { | ||
final String id; | ||
final String name; | ||
final String userName; | ||
final String email; | ||
final String role; | ||
|
||
UserModel({ | ||
required this.id, | ||
required this.name, | ||
required this.userName, | ||
required this.email, | ||
required this.role, | ||
}); | ||
|
||
UserModel.fromMap(Map<String, dynamic> json) | ||
: id = json['userId']! as String, | ||
name = json['name']! as String, | ||
userName = json['username']! 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, | ||
userName: json['username']! as String, | ||
email: json['email']! as String, | ||
role: json['role']! as String, | ||
); | ||
} | ||
|
||
String toJson() { | ||
return jsonEncode({ | ||
'userId': id, | ||
'name': name, | ||
'email': email, | ||
'role': role, | ||
}); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
import 'package:aranduapp/core/data/local/storage_value.dart'; | ||
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(e); | ||
} | ||
} | ||
} | ||
|
||
Future<void> clearUser() async { | ||
_userCache = null; | ||
StorageValue.getInstance().setJsonUser(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,57 @@ | ||
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()); | ||
|
||
if (json['userPayload'] == null) Log.e('user payload is null'); | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:aranduapp/ui/edit_delete_user/service/edit_delete_user_service.dart'; | ||
import 'package:aranduapp/ui/edit_delete_user/viewmode/edit_delte_user_viewmodel.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
|
||
void setupEditDeleteUser() { | ||
GetIt.I.registerLazySingleton(() => EditDeleteUserService()); | ||
GetIt.I.registerLazySingleton(() => EditDelteUserViewmodel()); | ||
} |
8 changes: 8 additions & 0 deletions
8
lib/ui/edit_delete_user/service/edit_delete_user_service.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,8 @@ | ||
import 'package:aranduapp/core/network/auth_api.dart'; | ||
|
||
class EditDeleteUserService { | ||
Future<void> deleteUser(String idUser) { | ||
AuthApi.getInstance(auth: true).delete(path: '/users/$idUser'); | ||
return Future.value(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,67 @@ | ||
import 'package:aranduapp/core/log/log.dart'; | ||
import 'package:aranduapp/ui/edit_delete_user/viewmode/edit_delte_user_viewmodel.dart'; | ||
import 'package:aranduapp/ui/login/view/login_view.dart'; | ||
import 'package:aranduapp/ui/shared/command_button.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
class EditDeleteUser extends StatelessWidget { | ||
const EditDeleteUser({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ChangeNotifierProvider<EditDelteUserViewmodel>.value( | ||
value: GetIt.instance<EditDelteUserViewmodel>(), | ||
child: const EditDeleteUserScreen(), | ||
); | ||
} | ||
} | ||
|
||
class EditDeleteUserScreen extends StatelessWidget { | ||
const EditDeleteUserScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
EditDelteUserViewmodel viewModel = | ||
Provider.of<EditDelteUserViewmodel>(context); | ||
|
||
return Scaffold( | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const Padding( | ||
padding: EdgeInsets.symmetric(horizontal: 16.0), | ||
child: Text( | ||
'Tem certeza de que deseja excluir sua conta? Essa ação não pode ser desfeita.', | ||
textAlign: TextAlign.center, | ||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), | ||
), | ||
), | ||
const SizedBox(height: 20), | ||
CommandButton( | ||
tap: viewModel.deleteUserCommand.execute, | ||
command: viewModel.deleteUserCommand, | ||
nameButton: "Deletar", | ||
onSuccessCallback: () { | ||
Navigator.of(context).pushReplacement( | ||
MaterialPageRoute( | ||
builder: (context) => const Login(), | ||
), | ||
); | ||
}, | ||
onErrorCallback: (e) { | ||
Log.e(e); | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar( | ||
content: Text('Não foi possível apagar a conta')), | ||
); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.