Add initial project structure and configuration files for iOS and Android

This commit is contained in:
2025-04-10 23:24:52 +05:30
commit e5ab751a74
86 changed files with 3762 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import 'package:dio/dio.dart';
import '../../data/repositories/auth_repository.dart';
class AuthInterceptor extends Interceptor {
final AuthRepository _authRepository;
final Dio _dio;
AuthInterceptor(this._authRepository, this._dio);
@override
Future<void> onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
// Skip auth header for login and refresh endpoints
if (options.path.contains('/auth/login') ||
options.path.contains('/auth/refresh')) {
return handler.next(options);
}
// Get token and add to request
final token = await _authRepository.getAccessToken();
if (token != null) {
options.headers['Authorization'] = 'Bearer $token';
}
return handler.next(options);
}
@override
Future<void> onError(
DioException err,
ErrorInterceptorHandler handler,
) async {
// Handle 401 errors by refreshing token and retrying
if (err.response?.statusCode == 401) {
// On 401, try to get a new token
final token = await _authRepository.getAccessToken();
if (token != null) {
// If we have a new token, retry the request
final opts = err.requestOptions;
opts.headers['Authorization'] = 'Bearer $token';
try {
final response = await _dio.fetch(opts);
return handler.resolve(response);
} on DioException catch (e) {
return handler.next(e);
}
}
}
return handler.next(err);
}
}

View File

@@ -0,0 +1,79 @@
import 'dart:developer';
import 'package:dio/dio.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';
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()}');
}
}
}