first commit
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import 'package:mobx/mobx.dart';
|
||||
import '../../data/models/auth_session_model.dart';
|
||||
import '../../data/services/auth_service.dart';
|
||||
|
||||
part 'auth_store.g.dart';
|
||||
|
||||
// ignore: library_private_types_in_public_api
|
||||
class AuthStore = _AuthStoreBase with _$AuthStore;
|
||||
|
||||
abstract class _AuthStoreBase with Store {
|
||||
final AuthService authService;
|
||||
|
||||
_AuthStoreBase({AuthService? service})
|
||||
: authService = service ?? AuthService();
|
||||
|
||||
@observable
|
||||
bool isLoading = false;
|
||||
|
||||
@observable
|
||||
bool isPasswordVisible = false;
|
||||
|
||||
@observable
|
||||
AuthSessionModel? currentSession;
|
||||
|
||||
@observable
|
||||
String? errorMessage;
|
||||
|
||||
@computed
|
||||
bool get isAuthenticated => currentSession != null;
|
||||
|
||||
@action
|
||||
void togglePasswordVisibility() {
|
||||
isPasswordVisible = !isPasswordVisible;
|
||||
}
|
||||
|
||||
@action
|
||||
Future<bool> login({required String email, required String password}) async {
|
||||
isLoading = true;
|
||||
errorMessage = null;
|
||||
currentSession = null;
|
||||
try {
|
||||
final session = await authService.login(email: email, password: password);
|
||||
if (session != null) {
|
||||
currentSession = session;
|
||||
return true;
|
||||
}
|
||||
errorMessage = 'No se pudo iniciar sesión. Verifique sus credenciales.';
|
||||
return false;
|
||||
} catch (e) {
|
||||
errorMessage = e.toString().replaceAll('Exception: ', '');
|
||||
return false;
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> checkInitialSession() async {
|
||||
isLoading = true;
|
||||
try {
|
||||
final session = await authService.loadSavedSession();
|
||||
currentSession = session;
|
||||
} catch (_) {
|
||||
currentSession = null;
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
Future<bool> refreshToken() async {
|
||||
isLoading = true;
|
||||
try {
|
||||
final session = await authService.refreshToken();
|
||||
if (session != null) {
|
||||
currentSession = session;
|
||||
return true;
|
||||
}
|
||||
currentSession = null;
|
||||
return false;
|
||||
} catch (_) {
|
||||
currentSession = null;
|
||||
return false;
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> logout() async {
|
||||
isLoading = true;
|
||||
errorMessage = null;
|
||||
try {
|
||||
await authService.logout();
|
||||
currentSession = null;
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'auth_store.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// StoreGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// ignore_for_file: non_constant_identifier_names, unnecessary_brace_in_string_interps, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic, no_leading_underscores_for_local_identifiers
|
||||
|
||||
mixin _$AuthStore on _AuthStoreBase, Store {
|
||||
Computed<bool>? _$isAuthenticatedComputed;
|
||||
|
||||
@override
|
||||
bool get isAuthenticated => (_$isAuthenticatedComputed ??= Computed<bool>(
|
||||
() => super.isAuthenticated,
|
||||
name: '_AuthStoreBase.isAuthenticated',
|
||||
)).value;
|
||||
|
||||
late final _$isLoadingAtom = Atom(
|
||||
name: '_AuthStoreBase.isLoading',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
bool get isLoading {
|
||||
_$isLoadingAtom.reportRead();
|
||||
return super.isLoading;
|
||||
}
|
||||
|
||||
@override
|
||||
set isLoading(bool value) {
|
||||
_$isLoadingAtom.reportWrite(value, super.isLoading, () {
|
||||
super.isLoading = value;
|
||||
});
|
||||
}
|
||||
|
||||
late final _$isPasswordVisibleAtom = Atom(
|
||||
name: '_AuthStoreBase.isPasswordVisible',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
bool get isPasswordVisible {
|
||||
_$isPasswordVisibleAtom.reportRead();
|
||||
return super.isPasswordVisible;
|
||||
}
|
||||
|
||||
@override
|
||||
set isPasswordVisible(bool value) {
|
||||
_$isPasswordVisibleAtom.reportWrite(value, super.isPasswordVisible, () {
|
||||
super.isPasswordVisible = value;
|
||||
});
|
||||
}
|
||||
|
||||
late final _$currentSessionAtom = Atom(
|
||||
name: '_AuthStoreBase.currentSession',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
AuthSessionModel? get currentSession {
|
||||
_$currentSessionAtom.reportRead();
|
||||
return super.currentSession;
|
||||
}
|
||||
|
||||
@override
|
||||
set currentSession(AuthSessionModel? value) {
|
||||
_$currentSessionAtom.reportWrite(value, super.currentSession, () {
|
||||
super.currentSession = value;
|
||||
});
|
||||
}
|
||||
|
||||
late final _$errorMessageAtom = Atom(
|
||||
name: '_AuthStoreBase.errorMessage',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
String? get errorMessage {
|
||||
_$errorMessageAtom.reportRead();
|
||||
return super.errorMessage;
|
||||
}
|
||||
|
||||
@override
|
||||
set errorMessage(String? value) {
|
||||
_$errorMessageAtom.reportWrite(value, super.errorMessage, () {
|
||||
super.errorMessage = value;
|
||||
});
|
||||
}
|
||||
|
||||
late final _$loginAsyncAction = AsyncAction(
|
||||
'_AuthStoreBase.login',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<bool> login({required String email, required String password}) {
|
||||
return _$loginAsyncAction.run(
|
||||
() => super.login(email: email, password: password),
|
||||
);
|
||||
}
|
||||
|
||||
late final _$checkInitialSessionAsyncAction = AsyncAction(
|
||||
'_AuthStoreBase.checkInitialSession',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<void> checkInitialSession() {
|
||||
return _$checkInitialSessionAsyncAction.run(
|
||||
() => super.checkInitialSession(),
|
||||
);
|
||||
}
|
||||
|
||||
late final _$refreshTokenAsyncAction = AsyncAction(
|
||||
'_AuthStoreBase.refreshToken',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<bool> refreshToken() {
|
||||
return _$refreshTokenAsyncAction.run(() => super.refreshToken());
|
||||
}
|
||||
|
||||
late final _$logoutAsyncAction = AsyncAction(
|
||||
'_AuthStoreBase.logout',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<void> logout() {
|
||||
return _$logoutAsyncAction.run(() => super.logout());
|
||||
}
|
||||
|
||||
late final _$_AuthStoreBaseActionController = ActionController(
|
||||
name: '_AuthStoreBase',
|
||||
context: context,
|
||||
);
|
||||
|
||||
@override
|
||||
void togglePasswordVisibility() {
|
||||
final _$actionInfo = _$_AuthStoreBaseActionController.startAction(
|
||||
name: '_AuthStoreBase.togglePasswordVisibility',
|
||||
);
|
||||
try {
|
||||
return super.togglePasswordVisibility();
|
||||
} finally {
|
||||
_$_AuthStoreBaseActionController.endAction(_$actionInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '''
|
||||
isLoading: ${isLoading},
|
||||
isPasswordVisible: ${isPasswordVisible},
|
||||
currentSession: ${currentSession},
|
||||
errorMessage: ${errorMessage},
|
||||
isAuthenticated: ${isAuthenticated}
|
||||
''';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user