From 9cc190875cc8e24303613f02bd013867f31004c7 Mon Sep 17 00:00:00 2001 From: Gabriel Costa de Oliveira Date: Fri, 13 Dec 2024 18:05:46 -0300 Subject: [PATCH 1/3] fix(#58): inicio do refresh token Co-authored-by: Yasm1nNasc1mento --- lib/core/network/AppInterceptors.dart | 2 +- lib/ui/login/service/LoginService.dart | 44 ++++++++++++--------- lib/ui/login/viewModel/LoginViewModel.dart | 45 ++++++++-------------- 3 files changed, 42 insertions(+), 49 deletions(-) diff --git a/lib/core/network/AppInterceptors.dart b/lib/core/network/AppInterceptors.dart index 1e91297..813416b 100644 --- a/lib/core/network/AppInterceptors.dart +++ b/lib/core/network/AppInterceptors.dart @@ -14,7 +14,6 @@ class AppInterceptors extends Interceptor { if ( token != null) { options.headers['Authorization'] = 'Bearer $token'; - Log.e(token); } handler.next(options); @@ -22,6 +21,7 @@ class AppInterceptors extends Interceptor { } catch (e) { Log.e(e); + rethrow; } } } diff --git a/lib/ui/login/service/LoginService.dart b/lib/ui/login/service/LoginService.dart index fe47264..375f40e 100644 --- a/lib/ui/login/service/LoginService.dart +++ b/lib/ui/login/service/LoginService.dart @@ -6,37 +6,43 @@ import 'package:aranduapp/ui/login/model/LoginResponse.dart'; import 'package:dio/dio.dart'; class LoginService { - static Future login(LoginRequest loginRequest) async { - Log.d('${loginRequest.email} ${loginRequest.password}'); Response response = await BaseApi.getInstance().post( - path: '/auth/login', - data: { - 'email' : loginRequest.email, - 'password': loginRequest.password - } - ); - - LoginResponse loginResponse = LoginResponse.fromJsonString(response.toString()); + path: '/auth/login', + data: { + 'email': loginRequest.email, + 'password': loginRequest.password + }); - assert(loginResponse.authToken != null && loginResponse.refreshToken != null); + LoginResponse loginResponse = + LoginResponse.fromJsonString(response.toString()); - await StorageValue.getInstance().setAuthToken(loginResponse.authToken ?? ''); - await StorageValue.getInstance().setAuthToken(loginResponse.refreshToken??''); + assert( + loginResponse.authToken != null && loginResponse.refreshToken != null); + await StorageValue.getInstance() + .setAuthToken(loginResponse.authToken ?? ''); + await StorageValue.getInstance() + .setAuthToken(loginResponse.refreshToken ?? ''); return response; - } - static Future refreshToken() async { - // TODO: implement build - throw UnimplementedError(); + static Future refreshToken() async { + Log.d('init reresh token'); - } + await BaseApi.getInstance().post(path: '/auth/validate-token'); -} + String? refresh = await StorageValue.getInstance().getRefreshToken(); + + assert(refresh != null); + await BaseApi.getInstance().post( + path: '/auth/refresh', + data: {'refreshToken': refresh}); + Log.d('finish reresh token'); + } +} diff --git a/lib/ui/login/viewModel/LoginViewModel.dart b/lib/ui/login/viewModel/LoginViewModel.dart index 137b386..1a975e2 100644 --- a/lib/ui/login/viewModel/LoginViewModel.dart +++ b/lib/ui/login/viewModel/LoginViewModel.dart @@ -1,4 +1,4 @@ - +import 'package:aranduapp/core/log/Log.dart'; import 'package:flutter/material.dart'; import 'package:local_auth/local_auth.dart'; import 'package:aranduapp/ui/login/service/LoginService.dart'; @@ -7,27 +7,23 @@ import 'package:aranduapp/ui/login/model/LoginRequest.dart'; import 'package:aranduapp/ui/home/view/HomeView.dart'; class LoginViewModel extends ChangeNotifier { - final BuildContext context; - + bool isLoading; final GlobalKey formKey; final TextEditingController emailController; final TextEditingController passwordController; - LoginViewModel(this.context) : - isLoading = false, - formKey = GlobalKey(), - emailController = TextEditingController(), - passwordController = TextEditingController(); + LoginViewModel(this.context) + : isLoading = false, + formKey = GlobalKey(), + emailController = TextEditingController(), + passwordController = TextEditingController(); - - Future loginWithEmailAndPassword() async { - // TODO use mutex to make this - if (isLoading){ + if (isLoading) { return; } @@ -39,8 +35,8 @@ class LoginViewModel extends ChangeNotifier { throw Exception('Valores inválidos'); } - await LoginService.login(LoginRequest(emailController.text, passwordController.text)); - + await LoginService.login( + LoginRequest(emailController.text, passwordController.text)); } catch (e) { rethrow; } finally { @@ -50,21 +46,12 @@ class LoginViewModel extends ChangeNotifier { } Future getRefreshTokenFuture() async { - // TODO - //return await LoginService.refreshToken(); - throw UnimplementedError(); + await LoginService.refreshToken(); } - Future loginWithDeviceAuth() async { - bool value = await LocalAuthentication().authenticate( - localizedReason: 'Toque com o dedo no sensor para logar'); - - if (!value) - throw Exception(); - - } - - - + Future loginWithDeviceAuth() async { + Log.d('init loginWithDeviceAuth'); + return await LocalAuthentication() + .authenticate(localizedReason: 'Toque com o dedo no sensor para logar'); + } } - From d545d7cc5b87194338fb2374bdb879c779750796 Mon Sep 17 00:00:00 2001 From: Gabriel Costa de Oliveira Date: Sun, 15 Dec 2024 09:04:05 -0300 Subject: [PATCH 2/3] fix(#58): corrigerefresh token --- lib/core/network/AppInterceptors.dart | 35 +++++++++++++------------ lib/core/network/BaseApi.dart | 36 ++++++++++++++++++-------- lib/ui/login/service/LoginService.dart | 4 +-- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/lib/core/network/AppInterceptors.dart b/lib/core/network/AppInterceptors.dart index 813416b..019f6c0 100644 --- a/lib/core/network/AppInterceptors.dart +++ b/lib/core/network/AppInterceptors.dart @@ -3,25 +3,26 @@ import 'package:aranduapp/core/log/Log.dart'; import 'package:dio/dio.dart'; class AppInterceptors extends Interceptor { - @override - Future onRequest(RequestOptions options, RequestInterceptorHandler handler) async { - super.onRequest(options, handler); - - try { - - String? token = await StorageValue.getInstance().getAuthToken(); - - if ( token != null) { - options.headers['Authorization'] = 'Bearer $token'; - } + Future onRequest( + RequestOptions options, + RequestInterceptorHandler handler + ) async { + super.onRequest(options, handler); - handler.next(options); + try { + String? token = await StorageValue.getInstance().getAuthToken(); - - } catch (e) { - Log.e(e); - rethrow; + if (token != null) { + options.headers['Authorization'] = 'asdfasdfasdfasdfasdfasdfasdfsadfsadf'; + } else { + Log.e('Token não encontrado'); } - } + } catch (e) { + Log.e('Erro ao recuperar token: $e'); + } + + handler.next(options); + } } + diff --git a/lib/core/network/BaseApi.dart b/lib/core/network/BaseApi.dart index 2add88f..b5db224 100644 --- a/lib/core/network/BaseApi.dart +++ b/lib/core/network/BaseApi.dart @@ -1,4 +1,4 @@ - +import 'package:aranduapp/core/data/local/StorageValue.dart'; import 'package:aranduapp/core/log/Log.dart'; import 'package:aranduapp/core/network/AppInterceptors.dart'; import 'package:dio/dio.dart'; @@ -8,35 +8,49 @@ class BaseApi { static BaseApi? _instance; - BaseApi._internal() - : _dio = Dio() { - _dio.options.baseUrl = 'https://arandu-user-service.onrender.com/'; - _dio.options.connectTimeout = const Duration(seconds: 5); + BaseApi._internal() : _dio = Dio() { + _dio.options.baseUrl = 'http://192.168.1.107:3000'; + _dio.options.connectTimeout = const Duration(seconds: 5); _dio.options.receiveTimeout = const Duration(seconds: 5); - _dio.interceptors.add(AppInterceptors()); - _dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true)); + _dio.interceptors.add(LogInterceptor( + requestBody: true, + responseBody: true, + requestHeader: true, + error: true, + responseHeader: true, + request: true)); } static BaseApi getInstance() { return _instance ??= BaseApi._internal(); } - Future get({required String path, Map? queryParameters}) async { + Future get( {required String path, Map? queryParameters}) async { try { + _dio.options.headers = { + 'Authorization': + 'Bearer ${await StorageValue.getInstance().getAuthToken()}', + }; + return await _dio.get(path, queryParameters: queryParameters); } catch (e) { Log.e(e); - rethrow; + rethrow; } } - Future post({ required String path, Object? data}) async { + Future post({required String path, Object? data}) async { try { + _dio.options.headers = { + 'Authorization': + 'Bearer ${await StorageValue.getInstance().getAuthToken()}', + }; + return await _dio.post(path, data: data); } catch (e) { Log.e(e); - rethrow; + rethrow; } } } diff --git a/lib/ui/login/service/LoginService.dart b/lib/ui/login/service/LoginService.dart index 375f40e..1f550e5 100644 --- a/lib/ui/login/service/LoginService.dart +++ b/lib/ui/login/service/LoginService.dart @@ -25,7 +25,7 @@ class LoginService { await StorageValue.getInstance() .setAuthToken(loginResponse.authToken ?? ''); await StorageValue.getInstance() - .setAuthToken(loginResponse.refreshToken ?? ''); + .setRefreshToken(loginResponse.refreshToken ?? ''); return response; } @@ -33,7 +33,7 @@ class LoginService { static Future refreshToken() async { Log.d('init reresh token'); - await BaseApi.getInstance().post(path: '/auth/validate-token'); + await BaseApi.getInstance().get(path: '/auth/validate-token'); String? refresh = await StorageValue.getInstance().getRefreshToken(); From 34ba1c44d15b9590674b19b9023d2cc73ff8e057 Mon Sep 17 00:00:00 2001 From: Gabriel Costa de Oliveira Date: Sun, 15 Dec 2024 09:58:33 -0300 Subject: [PATCH 3/3] =?UTF-8?q?fix(#58):=20adicionar=20autentica=C3=A7?= =?UTF-8?q?=C3=A3o=20do=20dispositivo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/src/main/AndroidManifest.xml | 3 +++ .../kotlin/com/example/aranduapp/MainActivity.kt | 4 ++-- lib/ui/login/view/LoginView.dart | 13 ++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 05bdb04..516e392 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,7 @@ + + + { width: 291, height: 64, child: ElevatedButton( - onPressed: () { - viewModel.loginWithDeviceAuth(); + onPressed: () async { + viewModel.loginWithDeviceAuth().then((ok) { + if (ok) + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (context) => const HomeView(), + ), + ); + }); }, child: const Text('Usar senha do celular'), ), @@ -154,7 +161,7 @@ class _LoginState extends State<_Login> { onTap: () { Navigator.of(context).push( MaterialPageRoute( - builder: (context) => RecoverAccount(), + builder: (context) => RecoverAccount(), ), ); },