Skip to content

Commit

Permalink
chore(0.0.11): release 0.0.11 (#23)
Browse files Browse the repository at this point in the history
* feat: new type connection to proxy (#9)

* refactor: change type variable .env

* feat: connection to the proxy via HTTP & check new endpoints

* feat: added local biometric authentication (#19)

* feat: new mapper for return proxy

* feat: add local_auth & add permission.USE_BIOMETRIC

* feat: add button biometric - input [#12] - authenticate [#13]

* feat: implement validation input confirm account

* fix: add body token for endpoint #17

* refactor: change ScaffoldMessenger & showSnackbar - add Toast for next use

* fix: showSnackbar visible in the showModalBottomSheet and error is visible here (#21)

* BREAKING CHANGE: trigger breaking change (#22)
  • Loading branch information
SantiagoGaonaC authored Oct 4, 2023
1 parent 2e2d68d commit f442ec4
Show file tree
Hide file tree
Showing 20 changed files with 522 additions and 127 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### 0.0.11 (2023-10-04)

### 0.0.10 (2023-10-04)


### Bug Fixes

* showSnackbar visible in the showModalBottomSheet and error is visible here ([#21](https://github.com/hawks-atlanta/frontend-flutter/issues/21)) ([fd649f1](https://github.com/hawks-atlanta/frontend-flutter/commit/fd649f14a5eb7ab5751daee81c79c61bc1ce9ff2))

### 0.0.9 (2023-10-03)


### Features

* added local biometric authentication ([#19](https://github.com/hawks-atlanta/frontend-flutter/issues/19)) ([0218c88](https://github.com/hawks-atlanta/frontend-flutter/commit/0218c88bb68a92507015a640a08a6eb7df9f81c1)), closes [#12](https://github.com/hawks-atlanta/frontend-flutter/issues/12) [#13](https://github.com/hawks-atlanta/frontend-flutter/issues/13) [#17](https://github.com/hawks-atlanta/frontend-flutter/issues/17)

### 0.0.8 (2023-09-24)

### 0.0.7 (2023-09-24)


Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ flutter pub get
```

- Finally compile and copy (or hot-reload) the binary to your phone/emulator

7 changes: 5 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
<application
android:label="login_mobile"
android:name="${applicationName}"
Expand All @@ -7,10 +8,12 @@
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
android:windowSoftInputMode="adjustResize"
android:enableOnBackInvokedCallback="true"
>
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.login_mobile

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragmentActivity

class MainActivity: FlutterActivity() {
class MainActivity: FlutterFragmentActivity() {
// ...
}

4 changes: 2 additions & 2 deletions lib/features/auth/domain/entities/user.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class User {
//final String uuid;
//final String username;
final String? username;
//final int statusCode;
//final String? message;
final String token;

//TODO?: add required
User({
//required this.uuid,
//required this.username,
required this.username,
//required this.statusCode,
//required this.message,
required this.token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import 'package:login_mobile/features/auth/infrastructure/infrastructure.dart';
class AuthDataSourceImpl extends AuthDataSource {
final dio = Dio(BaseOptions(
baseUrl: Enviroment.apiURL,

));

@override
Future<User> checkAuthStatus(String token) async {
try {
final response = await dio.post('/challenge',
final response = await dio.post('/refreshtoken',
data: {'token': token},
options: Options(headers: {'Authorization': 'Bearer $token'}));
final user = UserMapper.userJsonToEntity(response.data);
return user;
Expand Down
5 changes: 3 additions & 2 deletions lib/features/auth/infrastructure/mappers/user_mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ class UserMapper {

static User userJsonToEntity(Map<String, dynamic> json) {
return User(
//TODO: setup new user model with new fields returned from Proxy...
//uuid: json['uuid'],
//username: json['username'],
username: json['username'],
//statusCode: json['statusCode'],
//message: json['message'],
token: json['jwt']
token: json['token']
);
}
}
144 changes: 126 additions & 18 deletions lib/features/auth/presentation/providers/auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ Nos permite a nosotros llegar a nuestro backend directamente
DataSource tiene las conexiones e implementaciones necesarias
*/

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:local_auth/local_auth.dart';
import 'package:login_mobile/features/auth/infrastructure/infrastructure.dart';
import 'package:login_mobile/features/shared/infrastructure/inputs/services/key_value_storage_impl.dart';
import 'package:login_mobile/features/shared/infrastructure/inputs/services/key_value_storage_services.dart';
Expand All @@ -28,29 +32,46 @@ final authProvider = StateNotifierProvider<AuthNotifier, AuthState>((ref) {
class AuthNotifier extends StateNotifier<AuthState> {
final AuthRepository authRepository;
final KeyValueStorageService keyValueStorageService;
final LocalAuthentication auth = LocalAuthentication();
final Function? closeModalCallback;

AuthNotifier({
required this.authRepository,
required this.keyValueStorageService,
this.closeModalCallback,
}) : super(AuthState()) {
checkAuthStatus();
} //no hace falta mandar nada porque todo son valores opcinonales
//estas funciones terminan delegando en el repositorio

Future<void> loginUser(String email, String password) async {
Future<void> loginUser(String username, String password,
{bool biometric = false, bool authBiometric = false}) async {
await Future.delayed(const Duration(milliseconds: 500));

try {
final user = await authRepository.login(email, password);
_setLoggedUser(user);
final user = await authRepository.login(username, password);
_setUsername(username);
if (biometric == false) {
_setTempLoggedUser(user);
}
if (biometric == true && state.user != null) {
_setBiometricLoggedUser(user);
}
if (closeModalCallback != null) {
closeModalCallback!();
}
} on CustomError catch (e) {
logout(e.message);
if (biometric == false) {
logout(e.message);
} else {
biometricError(e.message);
}
} catch (e) {
logout('Something went wrong');
if (biometric == false) {
logout('Something went wrong');
} else {
biometricError('Your username or password is incorrect');
}
}

//final user = await authRepository.login(email, password);
//state = state.copyWith(user: user, authStatus: AuthStatus.authenticated); //copia del usuario donde ya tengo el estado
}

//TODO: implementar el registro
Expand All @@ -60,49 +81,136 @@ class AuthNotifier extends StateNotifier<AuthState> {
void checkAuthStatus() async {
final token = await keyValueStorageService.getValue<String>('token');
if (token == null) return logout();

try {
//el repositorio hace la auth
final user = await authRepository.checkAuthStatus(token);
_setLoggedUser(user);
_setBiometricLoggedUser(user);
} catch (e) {
logout();
}
}

void _setLoggedUser(User user) async {
void _setTempLoggedUser(User user) async {
state = state.copyWith(
user: user, errorMessage: '', authStatus: AuthStatus.authenticated);
}

Future<void> _setBiometricLoggedUser(User user) async {
await keyValueStorageService.setKeyValue('token', user.token);
await keyValueStorageService.setKeyValue('hasBiometric', true);
state = state.copyWith(
user: user,
errorMessage: '',
authStatus: AuthStatus.authenticated,
hasBiometric: true,
);
}

void disableBiometric(ref) async {
await keyValueStorageService.removeKey('token');
await keyValueStorageService.removeKey('hasBiometric');
state = state.copyWith(
user: user, errorMessage: '', authStatus: AuthStatus.authenticated);
user: null,
errorMessage: '',
authStatus: AuthStatus.authenticated,
hasBiometric: false);
//Aqui puedo abrir otro modal
// showModalBottomSheet(
// isScrollControlled: true,
// context: ref,
// builder: (context) => const CustomLogin());
}

void _setUsername(String username) async {
await keyValueStorageService.setKeyValue('username', username);
}

Future<void> logout([String? errorMessage]) async {
await keyValueStorageService.removeKey('token');
//await keyValueStorageService.removeKey('token');
await keyValueStorageService.removeKey('username');
state = state.copyWith(
user: null,
errorMessage: errorMessage,
authStatus: AuthStatus.notAuthenticated);
}

Future<void> biometricError([String? errorMessage]) async {
state = state.copyWith(
errorMessage: errorMessage,
);
}

Future<bool> authWithBiometrics() async {
bool authenticated = false;
try {
state = state.copyWith(authStatus: AuthStatus.checking);
authenticated = await auth.authenticate(
localizedReason:
'Scan your fingerprint (or face or whatever) to authenticate',
options: const AuthenticationOptions(
stickyAuth: true,
biometricOnly: true,
),
);
if (authenticated) {
state = state.copyWith(authStatus: AuthStatus.authenticated);
} else {
state = state.copyWith(authStatus: AuthStatus.notAuthenticated);
}
} on PlatformException catch (e) {
state = state.copyWith(
errorMessage: 'Error - ${e.message}',
authStatus: AuthStatus.notAuthenticated);
}
return authenticated;
}

Future<bool> loginWithBiometrics() async {
try {
final token = await keyValueStorageService.getValue<String>('token');
if (token == null) {
return false;
}
final user = await authRepository.checkAuthStatus(token);
state = state.copyWith(
user: user,
errorMessage: '',
authStatus: AuthStatus.authenticated,
);
authWithBiometrics();
return true;
} catch (e) {
state = state.copyWith(
errorMessage: 'Error: $e',
authStatus: AuthStatus.notAuthenticated,
);
return false;
}
}
}

enum AuthStatus { checking, authenticated, notAuthenticated }
enum AuthStatus { checking, authenticated, notAuthenticated, hasBiometric }

class AuthState {
final AuthStatus authStatus;
final User? user;
final String errorMessage;
final bool? hasBiometric;

AuthState(
{this.authStatus = AuthStatus.checking,
this.user,
this.errorMessage = ''});
this.errorMessage = '',
this.hasBiometric});

AuthState copyWith(
{AuthStatus? authStatus, User? user, String? errorMessage}) =>
{AuthStatus? authStatus,
User? user,
String? errorMessage,
bool? hasBiometric}) =>
AuthState(
authStatus: authStatus ?? this.authStatus,
user: user ?? this.user,
errorMessage: errorMessage ?? this.errorMessage,
hasBiometric: hasBiometric ?? this.hasBiometric,
);
}
28 changes: 26 additions & 2 deletions lib/features/auth/presentation/providers/login_form_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class LoginFormState {
// LoginFormNotifier es el que controla cuando cambia el email y el password
//! 2 - Cómo implemenrtamos un notifier provider
class LoginFormNotifier extends StateNotifier<LoginFormState> {
final Function(String, String) loginUserCallback;
final Function(String, String, {bool biometric}) loginUserCallback;
LoginFormNotifier({required this.loginUserCallback})
: super(LoginFormState());
onUsernameChange(String value) {
Expand All @@ -73,8 +73,32 @@ class LoginFormNotifier extends StateNotifier<LoginFormState> {

onFormSubmitted() async {
_touchEveryField();
if (!state.isValid) return;
if (!state.isValid) {
return;
}
state = state.copyWith(isPosting: true); //actualiza el estado

await loginUserCallback(state.username.value, state.password.value);

state = state.copyWith(isPosting: false);
}

onFormSubmittedBiometric() async {
_touchEveryField();
if (state.isValid) {
return await loginUserCallback(state.username.value, state.password.value,
biometric: true);
}
state = state.copyWith(isPosting: true); //actualiza el estado

state = state.copyWith(isPosting: false);
}

onFormAuthBiometric() async {
_touchEveryField();
if (!state.isValid) {
return;
}
state = state.copyWith(isPosting: true); //actualiza el estado

await loginUserCallback(state.username.value, state.password.value);
Expand Down
Loading

0 comments on commit f442ec4

Please sign in to comment.