120 lines
3.4 KiB
Dart
120 lines
3.4 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../../app.dart';
|
|
import '../../features/auth/models/auth_token.dart';
|
|
import '../../features/auth/models/auth_credentials.dart';
|
|
import '../../data/models/user.dart';
|
|
import '../../core/errors/exceptions.dart';
|
|
import 'package:local_auth/local_auth.dart';
|
|
|
|
import '../../features/dashboard/screens/dashboard_screen.dart';
|
|
|
|
class AuthService {
|
|
final Dio _dio;
|
|
AuthService(this._dio);
|
|
|
|
Future<AuthToken> login(AuthCredentials credentials) async {
|
|
try {
|
|
final response = await _dio.post(
|
|
'/auth/login',
|
|
data: credentials.toJson(),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return AuthToken.fromJson(response.data);
|
|
} else {
|
|
throw AuthException('Login failed');
|
|
}
|
|
} on DioException catch (e) {
|
|
if (e.response?.statusCode == 401) {
|
|
throw AuthException('Invalid credentials');
|
|
}
|
|
throw NetworkException('Network error during login');
|
|
} catch (e) {
|
|
throw UnexpectedException('Unexpected error during login: ${e.toString()}');
|
|
}
|
|
}
|
|
|
|
Future<AuthToken> refreshToken(String refreshToken) async {
|
|
try {
|
|
final response = await _dio.post(
|
|
'/auth/refresh',
|
|
data: {'refresh_token': refreshToken},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return AuthToken.fromJson(response.data);
|
|
} else {
|
|
throw AuthException('Token refresh failed');
|
|
}
|
|
} catch (e) {
|
|
throw AuthException('Failed to refresh token: ${e.toString()}');
|
|
}
|
|
}
|
|
|
|
Future<void> logout(String refreshToken) async {
|
|
try {
|
|
await _dio.post(
|
|
'/auth/logout',
|
|
data: {'refresh_token': refreshToken},
|
|
);
|
|
} catch (e) {
|
|
// We might want to just log this error rather than throwing,
|
|
// as logout should succeed even if the server call fails
|
|
log('Logout error: ${e.toString()}');
|
|
}
|
|
}
|
|
|
|
Future<User> getUserProfile() async {
|
|
try {
|
|
final response = await _dio.get('/auth/profile');
|
|
|
|
if (response.statusCode == 200) {
|
|
return User.fromJson(response.data);
|
|
} else {
|
|
throw AuthException('Failed to get user profile');
|
|
}
|
|
} catch (e) {
|
|
throw AuthException('Error fetching user profile: ${e.toString()}');
|
|
}
|
|
}
|
|
|
|
static Future<void> authenticateWithBiometrics(BuildContext context) async {
|
|
final LocalAuthentication localAuth = LocalAuthentication();
|
|
try {
|
|
bool isBiometricAvailable = await localAuth.canCheckBiometrics;
|
|
bool isAuthenticated = false;
|
|
|
|
if (isBiometricAvailable) {
|
|
isAuthenticated = await localAuth.authenticate(
|
|
localizedReason: 'Touch the fingerprint sensor',
|
|
options: const AuthenticationOptions(
|
|
biometricOnly: true,
|
|
stickyAuth: true,
|
|
),
|
|
);
|
|
}
|
|
|
|
if (isAuthenticated) {
|
|
// Navigate to Dashboard
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const NavigationScaffold()),
|
|
);
|
|
} else {
|
|
// Show error/snack bar
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text("Authentication failed")),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print("Biometric error: $e");
|
|
}
|
|
}
|
|
}
|
|
} |