feat: Add initial backend server with user authentication, session management, and playlist CRUD functionality.

This commit is contained in:
Michael committed 2025-12-06 01:51:07 +01:00
1 parent a1ce183cd3
commit ac4f274031
19 files changed
+1324 -75

No files matched your search

+54 -21
View File
@@ -1,5 +1,5 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/database/hive_service.dart';
import '../../../core/services/auth_api_service.dart';
import '../../../core/models/app_user.dart';
/// Auth state
@@ -7,11 +7,13 @@ class AuthState {
final AppUser? currentUser;
final bool isLoading;
final String? errorMessage;
final bool isInitialized;
const AuthState({
this.currentUser,
this.isLoading = false,
this.errorMessage,
this.isInitialized = false,
});
bool get isAuthenticated => currentUser != null;
@@ -21,52 +23,83 @@ class AuthState {
AppUser? currentUser,
bool? isLoading,
String? errorMessage,
bool? isInitialized,
bool clearUser = false,
}) {
return AuthState(
currentUser: currentUser ?? this.currentUser,
currentUser: clearUser ? null : (currentUser ?? this.currentUser),
isLoading: isLoading ?? this.isLoading,
errorMessage: errorMessage,
isInitialized: isInitialized ?? this.isInitialized,
);
}
}
/// Auth notifier
/// Auth notifier using API
class AuthNotifier extends StateNotifier<AuthState> {
AuthNotifier() : super(const AuthState());
final AuthApiService _authService = AuthApiService();
AuthNotifier() : super(const AuthState()) {
// Auto-check for existing session
checkSession();
}
/// Check if there's an existing valid session
Future<void> checkSession() async {
state = state.copyWith(isLoading: true);
try {
final user = await _authService.getCurrentUser();
if (user != null) {
state = AuthState(
currentUser: user,
isLoading: false,
isInitialized: true,
);
} else {
state = const AuthState(isInitialized: true);
}
} catch (e) {
state = const AuthState(isInitialized: true);
}
}
/// Login with username and password
Future<bool> login(String username, String password) async {
state = state.copyWith(isLoading: true, errorMessage: null);
try {
final usersBox = HiveService.usersBox;
final result = await _authService.login(username, password);
// Search for user by username
final user = usersBox.values.firstWhere(
(user) => user.username == username,
orElse: () => throw Exception('User not found'),
);
// Verify password using salt-based hashing
if (!HiveService.verifyPassword(password, user.passwordHash)) {
throw Exception('Invalid password');
if (result.success && result.user != null) {
state = AuthState(
currentUser: result.user,
isLoading: false,
isInitialized: true,
);
return true;
} else {
state = AuthState(
isLoading: false,
isInitialized: true,
errorMessage: result.error ?? 'Login failed',
);
return false;
}
state = AuthState(currentUser: user, isLoading: false);
return true;
} catch (e) {
state = AuthState(
isLoading: false,
errorMessage: 'Invalid username or password',
isInitialized: true,
errorMessage: 'Network error: $e',
);
return false;
}
}
/// Logout current user
void logout() {
state = const AuthState();
Future<void> logout() async {
await _authService.logout();
state = const AuthState(isInitialized: true);
}
/// Get current user