update api

This commit is contained in:
Phuoc Nguyen
2025-10-10 17:15:40 +07:00
parent b94c158004
commit 04f7042b8d
24 changed files with 3322 additions and 8 deletions

View File

@@ -0,0 +1,171 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/auth_provider.dart';
/// Login page for user authentication
class LoginPage extends ConsumerStatefulWidget {
const LoginPage({super.key});
@override
ConsumerState<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends ConsumerState<LoginPage> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _obscurePassword = true;
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
Future<void> _handleLogin() async {
if (!_formKey.currentState!.validate()) return;
final success = await ref.read(authProvider.notifier).login(
email: _emailController.text.trim(),
password: _passwordController.text,
);
if (!mounted) return;
if (success) {
// Navigate to home or show success
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Login successful!')),
);
// TODO: Navigate to home page
} else {
final errorMessage = ref.read(authProvider).errorMessage;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(errorMessage ?? 'Login failed'),
backgroundColor: Colors.red,
),
);
}
}
@override
Widget build(BuildContext context) {
final authState = ref.watch(authProvider);
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Logo or app name
Icon(
Icons.shopping_cart,
size: 80,
color: Theme.of(context).primaryColor,
),
const SizedBox(height: 16),
Text(
'Retail POS',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 48),
// Email field
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.email),
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!value.contains('@')) {
return 'Please enter a valid email';
}
return null;
},
),
const SizedBox(height: 16),
// Password field
TextFormField(
controller: _passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
labelText: 'Password',
prefixIcon: const Icon(Icons.lock),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword
? Icons.visibility
: Icons.visibility_off,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 8) {
return 'Password must be at least 8 characters';
}
return null;
},
),
const SizedBox(height: 24),
// Login button
FilledButton(
onPressed: authState.isLoading ? null : _handleLogin,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: authState.isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
),
)
: const Text('Login'),
),
),
const SizedBox(height: 16),
// Register link
TextButton(
onPressed: () {
// TODO: Navigate to register page
// Navigator.push(context, MaterialPageRoute(builder: (_) => const RegisterPage()));
},
child: const Text('Don\'t have an account? Register'),
),
],
),
),
),
),
);
}
}

View File

@@ -0,0 +1,234 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/auth_provider.dart';
/// Register page for new user registration
class RegisterPage extends ConsumerStatefulWidget {
const RegisterPage({super.key});
@override
ConsumerState<RegisterPage> createState() => _RegisterPageState();
}
class _RegisterPageState extends ConsumerState<RegisterPage> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _confirmPasswordController = TextEditingController();
bool _obscurePassword = true;
bool _obscureConfirmPassword = true;
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_passwordController.dispose();
_confirmPasswordController.dispose();
super.dispose();
}
Future<void> _handleRegister() async {
if (!_formKey.currentState!.validate()) return;
final success = await ref.read(authProvider.notifier).register(
name: _nameController.text.trim(),
email: _emailController.text.trim(),
password: _passwordController.text,
);
if (!mounted) return;
if (success) {
// Navigate to home or show success
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Registration successful!')),
);
// TODO: Navigate to home page
} else {
final errorMessage = ref.read(authProvider).errorMessage;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(errorMessage ?? 'Registration failed'),
backgroundColor: Colors.red,
),
);
}
}
@override
Widget build(BuildContext context) {
final authState = ref.watch(authProvider);
return Scaffold(
appBar: AppBar(
title: const Text('Register'),
),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 24),
// Logo or app name
Icon(
Icons.shopping_cart,
size: 80,
color: Theme.of(context).primaryColor,
),
const SizedBox(height: 16),
Text(
'Create Account',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 48),
// Name field
TextFormField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Full Name',
prefixIcon: Icon(Icons.person),
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your name';
}
if (value.length < 2) {
return 'Name must be at least 2 characters';
}
return null;
},
),
const SizedBox(height: 16),
// Email field
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.email),
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!value.contains('@')) {
return 'Please enter a valid email';
}
return null;
},
),
const SizedBox(height: 16),
// Password field
TextFormField(
controller: _passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
labelText: 'Password',
prefixIcon: const Icon(Icons.lock),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword
? Icons.visibility
: Icons.visibility_off,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 8) {
return 'Password must be at least 8 characters';
}
// Check for uppercase, lowercase, and number
if (!RegExp(r'(?=.*[a-z])(?=.*[A-Z])(?=.*\d)').hasMatch(value)) {
return 'Password must contain uppercase, lowercase, and number';
}
return null;
},
),
const SizedBox(height: 16),
// Confirm password field
TextFormField(
controller: _confirmPasswordController,
obscureText: _obscureConfirmPassword,
decoration: InputDecoration(
labelText: 'Confirm Password',
prefixIcon: const Icon(Icons.lock_outline),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
_obscureConfirmPassword
? Icons.visibility
: Icons.visibility_off,
),
onPressed: () {
setState(() {
_obscureConfirmPassword = !_obscureConfirmPassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please confirm your password';
}
if (value != _passwordController.text) {
return 'Passwords do not match';
}
return null;
},
),
const SizedBox(height: 24),
// Register button
FilledButton(
onPressed: authState.isLoading ? null : _handleRegister,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: authState.isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
),
)
: const Text('Register'),
),
),
const SizedBox(height: 16),
// Login link
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Already have an account? Login'),
),
],
),
),
),
),
);
}
}

View File

@@ -0,0 +1,215 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../../../core/di/injection_container.dart';
import '../../domain/entities/user.dart';
import '../../domain/repositories/auth_repository.dart';
part 'auth_provider.g.dart';
/// Provider for AuthRepository
@riverpod
AuthRepository authRepository(Ref ref) {
return sl<AuthRepository>();
}
/// Auth state class
class AuthState {
final User? user;
final bool isAuthenticated;
final bool isLoading;
final String? errorMessage;
const AuthState({
this.user,
this.isAuthenticated = false,
this.isLoading = false,
this.errorMessage,
});
AuthState copyWith({
User? user,
bool? isAuthenticated,
bool? isLoading,
String? errorMessage,
}) {
return AuthState(
user: user ?? this.user,
isAuthenticated: isAuthenticated ?? this.isAuthenticated,
isLoading: isLoading ?? this.isLoading,
errorMessage: errorMessage ?? this.errorMessage,
);
}
}
/// Auth state notifier provider
@riverpod
class Auth extends _$Auth {
@override
AuthState build() {
_checkAuthStatus();
return const AuthState();
}
AuthRepository get _repository => ref.read(authRepositoryProvider);
/// Check if user is authenticated on app start
Future<void> _checkAuthStatus() async {
state = state.copyWith(isLoading: true);
final isAuthenticated = await _repository.isAuthenticated();
if (isAuthenticated) {
// Get user profile
final result = await _repository.getProfile();
result.fold(
(failure) {
state = const AuthState(
isAuthenticated: false,
isLoading: false,
);
},
(user) {
state = AuthState(
user: user,
isAuthenticated: true,
isLoading: false,
);
},
);
} else {
state = const AuthState(
isAuthenticated: false,
isLoading: false,
);
}
}
/// Login user
Future<bool> login({
required String email,
required String password,
}) async {
state = state.copyWith(isLoading: true, errorMessage: null);
final result = await _repository.login(email: email, password: password);
return result.fold(
(failure) {
state = state.copyWith(
isLoading: false,
errorMessage: failure.message,
);
return false;
},
(authResponse) {
state = AuthState(
user: authResponse.user,
isAuthenticated: true,
isLoading: false,
errorMessage: null,
);
return true;
},
);
}
/// Register new user
Future<bool> register({
required String name,
required String email,
required String password,
List<String> roles = const ['user'],
}) async {
state = state.copyWith(isLoading: true, errorMessage: null);
final result = await _repository.register(
name: name,
email: email,
password: password,
roles: roles,
);
return result.fold(
(failure) {
state = state.copyWith(
isLoading: false,
errorMessage: failure.message,
);
return false;
},
(authResponse) {
state = AuthState(
user: authResponse.user,
isAuthenticated: true,
isLoading: false,
errorMessage: null,
);
return true;
},
);
}
/// Get user profile (refresh user data)
Future<void> getProfile() async {
state = state.copyWith(isLoading: true, errorMessage: null);
final result = await _repository.getProfile();
result.fold(
(failure) {
state = state.copyWith(
isLoading: false,
errorMessage: failure.message,
);
},
(user) {
state = state.copyWith(
user: user,
isLoading: false,
);
},
);
}
/// Refresh access token
Future<bool> refreshToken() async {
final result = await _repository.refreshToken();
return result.fold(
(failure) {
// If token refresh fails, logout user
logout();
return false;
},
(authResponse) {
state = state.copyWith(user: authResponse.user);
return true;
},
);
}
/// Logout user
Future<void> logout() async {
state = state.copyWith(isLoading: true);
await _repository.logout();
state = const AuthState(
isAuthenticated: false,
isLoading: false,
);
}
}
/// Current authenticated user provider
@riverpod
User? currentUser(Ref ref) {
final authState = ref.watch(authProvider);
return authState.user;
}
/// Is authenticated provider
@riverpod
bool isAuthenticated(Ref ref) {
final authState = ref.watch(authProvider);
return authState.isAuthenticated;
}

View File

@@ -0,0 +1,204 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'auth_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
/// Provider for AuthRepository
@ProviderFor(authRepository)
const authRepositoryProvider = AuthRepositoryProvider._();
/// Provider for AuthRepository
final class AuthRepositoryProvider
extends $FunctionalProvider<AuthRepository, AuthRepository, AuthRepository>
with $Provider<AuthRepository> {
/// Provider for AuthRepository
const AuthRepositoryProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'authRepositoryProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$authRepositoryHash();
@$internal
@override
$ProviderElement<AuthRepository> $createElement($ProviderPointer pointer) =>
$ProviderElement(pointer);
@override
AuthRepository create(Ref ref) {
return authRepository(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(AuthRepository value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<AuthRepository>(value),
);
}
}
String _$authRepositoryHash() => r'0483b13ac95333b56a1a82f6c9fdb64ae46f287d';
/// Auth state notifier provider
@ProviderFor(Auth)
const authProvider = AuthProvider._();
/// Auth state notifier provider
final class AuthProvider extends $NotifierProvider<Auth, AuthState> {
/// Auth state notifier provider
const AuthProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'authProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$authHash();
@$internal
@override
Auth create() => Auth();
/// {@macro riverpod.override_with_value}
Override overrideWithValue(AuthState value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<AuthState>(value),
);
}
}
String _$authHash() => r'c88e150224fa855ed0ddfba30bef9e2b289f329d';
/// Auth state notifier provider
abstract class _$Auth extends $Notifier<AuthState> {
AuthState build();
@$mustCallSuper
@override
void runBuild() {
final created = build();
final ref = this.ref as $Ref<AuthState, AuthState>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<AuthState, AuthState>,
AuthState,
Object?,
Object?
>;
element.handleValue(ref, created);
}
}
/// Current authenticated user provider
@ProviderFor(currentUser)
const currentUserProvider = CurrentUserProvider._();
/// Current authenticated user provider
final class CurrentUserProvider extends $FunctionalProvider<User?, User?, User?>
with $Provider<User?> {
/// Current authenticated user provider
const CurrentUserProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'currentUserProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$currentUserHash();
@$internal
@override
$ProviderElement<User?> $createElement($ProviderPointer pointer) =>
$ProviderElement(pointer);
@override
User? create(Ref ref) {
return currentUser(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(User? value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<User?>(value),
);
}
}
String _$currentUserHash() => r'4c8cb60cef35a4fd001291434558037d6c85faf5';
/// Is authenticated provider
@ProviderFor(isAuthenticated)
const isAuthenticatedProvider = IsAuthenticatedProvider._();
/// Is authenticated provider
final class IsAuthenticatedProvider
extends $FunctionalProvider<bool, bool, bool>
with $Provider<bool> {
/// Is authenticated provider
const IsAuthenticatedProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'isAuthenticatedProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$isAuthenticatedHash();
@$internal
@override
$ProviderElement<bool> $createElement($ProviderPointer pointer) =>
$ProviderElement(pointer);
@override
bool create(Ref ref) {
return isAuthenticated(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(bool value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<bool>(value),
);
}
}
String _$isAuthenticatedHash() => r'003f7e85bfa5ae774792659ce771b5b59ebf04f8';