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}
|
||||
''';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../../../core/theme/app_colors.dart';
|
||||
import '../store/auth_store.dart';
|
||||
import '../widgets/login_form_widget.dart';
|
||||
import '../widgets/login_header_widget.dart';
|
||||
|
||||
class LoginView extends StatefulWidget {
|
||||
final AuthStore? store;
|
||||
|
||||
const LoginView({super.key, this.store});
|
||||
|
||||
@override
|
||||
State<LoginView> createState() => _LoginViewState();
|
||||
}
|
||||
|
||||
class _LoginViewState extends State<LoginView> {
|
||||
late final AuthStore _authStore;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authStore = widget.store ?? AuthStore();
|
||||
}
|
||||
|
||||
void _onSuccess() {
|
||||
context.go('/');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
body: SafeArea(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 32),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 440),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
border: Border.all(color: AppColors.border),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: AppColors.primary.withValues(alpha: 0.04),
|
||||
blurRadius: 24,
|
||||
offset: const Offset(0, 8),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const LoginHeaderWidget(),
|
||||
const SizedBox(height: 28),
|
||||
LoginFormWidget(
|
||||
store: _authStore,
|
||||
onLoginSuccess: _onSuccess,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text(
|
||||
'Sistema protegido · Exclusivo Consultorio Dr. Luis Castro',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: AppColors.textMuted,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||
import 'package:form_builder_validators/form_builder_validators.dart';
|
||||
import '../../../../core/theme/app_colors.dart';
|
||||
import '../store/auth_store.dart';
|
||||
|
||||
class LoginFormWidget extends StatefulWidget {
|
||||
final AuthStore store;
|
||||
final VoidCallback onLoginSuccess;
|
||||
|
||||
const LoginFormWidget({
|
||||
super.key,
|
||||
required this.store,
|
||||
required this.onLoginSuccess,
|
||||
});
|
||||
|
||||
@override
|
||||
State<LoginFormWidget> createState() => _LoginFormWidgetState();
|
||||
}
|
||||
|
||||
class _LoginFormWidgetState extends State<LoginFormWidget> {
|
||||
final _formKey = GlobalKey<FormBuilderState>();
|
||||
|
||||
Future<void> _submit() async {
|
||||
if (_formKey.currentState?.saveAndValidate() ?? false) {
|
||||
final values = _formKey.currentState!.value;
|
||||
final email = values['email'] as String;
|
||||
final password = values['password'] as String;
|
||||
|
||||
final success = await widget.store.login(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
|
||||
if (success && mounted) {
|
||||
widget.onLoginSuccess();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Observer(
|
||||
builder: (_) {
|
||||
return FormBuilder(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Mensaje de Error si existe
|
||||
if (widget.store.errorMessage != null) ...[
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.error.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: AppColors.error.withValues(alpha: 0.3)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.error_outline_rounded, color: AppColors.error, size: 20),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Text(
|
||||
widget.store.errorMessage!,
|
||||
style: const TextStyle(
|
||||
color: AppColors.error,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
// Campo Email
|
||||
const Text(
|
||||
'Correo Electrónico',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
FormBuilderTextField(
|
||||
name: 'email',
|
||||
initialValue: '[email protected]',
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
textInputAction: TextInputAction.next,
|
||||
decoration: const InputDecoration(
|
||||
hintText: '[email protected]',
|
||||
prefixIcon: Icon(Icons.mail_outline_rounded, color: AppColors.textMuted, size: 20),
|
||||
),
|
||||
validator: FormBuilderValidators.compose([
|
||||
FormBuilderValidators.required(errorText: 'El correo electrónico es requerido'),
|
||||
FormBuilderValidators.email(errorText: 'Ingrese un correo electrónico válido'),
|
||||
]),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Campo Contraseña
|
||||
const Text(
|
||||
'Contraseña',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
FormBuilderTextField(
|
||||
name: 'password',
|
||||
initialValue: 'LUIS',
|
||||
obscureText: !widget.store.isPasswordVisible,
|
||||
textInputAction: TextInputAction.done,
|
||||
onSubmitted: (_) => _submit(),
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Ingrese su contraseña',
|
||||
prefixIcon: const Icon(Icons.lock_outline_rounded, color: AppColors.textMuted, size: 20),
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(
|
||||
widget.store.isPasswordVisible
|
||||
? Icons.visibility_off_outlined
|
||||
: Icons.visibility_outlined,
|
||||
color: AppColors.textMuted,
|
||||
size: 20,
|
||||
),
|
||||
onPressed: widget.store.togglePasswordVisibility,
|
||||
),
|
||||
),
|
||||
validator: FormBuilderValidators.compose([
|
||||
FormBuilderValidators.required(errorText: 'La contraseña es requerida'),
|
||||
]),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Botón de Inicio de Sesión
|
||||
ElevatedButton(
|
||||
onPressed: widget.store.isLoading ? null : _submit,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.primary,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
elevation: 2,
|
||||
),
|
||||
child: widget.store.isLoading
|
||||
? const SizedBox(
|
||||
height: 20,
|
||||
width: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
||||
),
|
||||
)
|
||||
: const Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.login_rounded, size: 18),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'Ingresar al Consultorio',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import '../../../../core/theme/app_colors.dart';
|
||||
|
||||
class LoginHeaderWidget extends StatelessWidget {
|
||||
const LoginHeaderWidget({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Logo de Hidalyx
|
||||
SvgPicture.asset(
|
||||
'assets/images/hidalyx_logo_blue.svg',
|
||||
height: 48,
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
const Text(
|
||||
'Hidalyx Med',
|
||||
style: TextStyle(
|
||||
fontSize: 26,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: AppColors.textPrimary,
|
||||
letterSpacing: -0.5,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
const Text(
|
||||
'Portal de Gestión Clínica · Dr. Luis A. Castro Sosa',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: AppColors.textSecondary,
|
||||
fontWeight: FontWeight.w400,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user