Initial commit - app-padrao-1.0
This commit is contained in:
51
lib/features/auth/data/models/user_model.dart
Normal file
51
lib/features/auth/data/models/user_model.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'user_model.g.dart';
|
||||
|
||||
@HiveType(typeId: 0)
|
||||
class UserModel extends HiveObject {
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
@HiveField(1)
|
||||
final String email;
|
||||
|
||||
@HiveField(2)
|
||||
final String passwordHash;
|
||||
|
||||
@HiveField(3)
|
||||
final String barberName;
|
||||
|
||||
@HiveField(4)
|
||||
final String barberShopName;
|
||||
|
||||
@HiveField(5)
|
||||
final DateTime createdAt;
|
||||
|
||||
UserModel({
|
||||
required this.id,
|
||||
required this.email,
|
||||
required this.passwordHash,
|
||||
required this.barberName,
|
||||
required this.barberShopName,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
UserModel copyWith({
|
||||
String? id,
|
||||
String? email,
|
||||
String? passwordHash,
|
||||
String? barberName,
|
||||
String? barberShopName,
|
||||
DateTime? createdAt,
|
||||
}) {
|
||||
return UserModel(
|
||||
id: id ?? this.id,
|
||||
email: email ?? this.email,
|
||||
passwordHash: passwordHash ?? this.passwordHash,
|
||||
barberName: barberName ?? this.barberName,
|
||||
barberShopName: barberShopName ?? this.barberShopName,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
);
|
||||
}
|
||||
}
|
||||
56
lib/features/auth/data/models/user_model.g.dart
Normal file
56
lib/features/auth/data/models/user_model.g.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'user_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class UserModelAdapter extends TypeAdapter<UserModel> {
|
||||
@override
|
||||
final int typeId = 0;
|
||||
|
||||
@override
|
||||
UserModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return UserModel(
|
||||
id: fields[0] as String,
|
||||
email: fields[1] as String,
|
||||
passwordHash: fields[2] as String,
|
||||
barberName: fields[3] as String,
|
||||
barberShopName: fields[4] as String,
|
||||
createdAt: fields[5] as DateTime,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, UserModel obj) {
|
||||
writer
|
||||
..writeByte(6)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.email)
|
||||
..writeByte(2)
|
||||
..write(obj.passwordHash)
|
||||
..writeByte(3)
|
||||
..write(obj.barberName)
|
||||
..writeByte(4)
|
||||
..write(obj.barberShopName)
|
||||
..writeByte(5)
|
||||
..write(obj.createdAt);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is UserModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
130
lib/features/auth/data/repositories/auth_repository.dart
Normal file
130
lib/features/auth/data/repositories/auth_repository.dart
Normal file
@@ -0,0 +1,130 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:barber_app/core/database/database_service.dart';
|
||||
import 'package:barber_app/core/utils/password_utils.dart';
|
||||
import 'package:barber_app/features/auth/data/models/user_model.dart';
|
||||
import 'package:barber_app/features/settings/data/models/settings_model.dart';
|
||||
|
||||
class AuthRepository {
|
||||
final _uuid = const Uuid();
|
||||
|
||||
// Registrar novo usuário
|
||||
Future<UserModel?> register({
|
||||
required String email,
|
||||
required String password,
|
||||
required String barberName,
|
||||
required String barberShopName,
|
||||
}) async {
|
||||
try {
|
||||
// Verifica se email já existe
|
||||
final existingUser = DatabaseService.usersBoxInstance.values
|
||||
.where((user) => user.email.toLowerCase() == email.toLowerCase())
|
||||
.firstOrNull;
|
||||
|
||||
if (existingUser != null) {
|
||||
throw Exception('E-mail já cadastrado');
|
||||
}
|
||||
|
||||
final userId = _uuid.v4();
|
||||
final user = UserModel(
|
||||
id: userId,
|
||||
email: email.toLowerCase().trim(),
|
||||
passwordHash: PasswordUtils.hashPassword(password),
|
||||
barberName: barberName.trim(),
|
||||
barberShopName: barberShopName.trim(),
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
|
||||
await DatabaseService.usersBoxInstance.put(userId, user);
|
||||
|
||||
// Cria configurações padrão
|
||||
final settings = SettingsModel(userId: userId);
|
||||
await DatabaseService.settingsBoxInstance.put(userId, settings);
|
||||
|
||||
// Define como usuário atual
|
||||
await DatabaseService.setCurrentUserId(userId);
|
||||
|
||||
return user;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Login
|
||||
Future<UserModel?> login({
|
||||
required String email,
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
final user = DatabaseService.usersBoxInstance.values
|
||||
.where((u) => u.email.toLowerCase() == email.toLowerCase().trim())
|
||||
.firstOrNull;
|
||||
|
||||
if (user == null) {
|
||||
throw Exception('Usuário não encontrado');
|
||||
}
|
||||
|
||||
if (!PasswordUtils.verifyPassword(password, user.passwordHash)) {
|
||||
throw Exception('Senha incorreta');
|
||||
}
|
||||
|
||||
await DatabaseService.setCurrentUserId(user.id);
|
||||
return user;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Logout
|
||||
Future<void> logout() async {
|
||||
await DatabaseService.setCurrentUserId(null);
|
||||
}
|
||||
|
||||
// Obter usuário atual
|
||||
UserModel? getCurrentUser() {
|
||||
final userId = DatabaseService.getCurrentUserId();
|
||||
if (userId == null) return null;
|
||||
return DatabaseService.usersBoxInstance.get(userId);
|
||||
}
|
||||
|
||||
// Verificar se está logado
|
||||
bool isLoggedIn() {
|
||||
return DatabaseService.isLoggedIn();
|
||||
}
|
||||
|
||||
// Atualizar perfil
|
||||
Future<UserModel?> updateProfile({
|
||||
required String barberName,
|
||||
required String barberShopName,
|
||||
}) async {
|
||||
final currentUser = getCurrentUser();
|
||||
if (currentUser == null) return null;
|
||||
|
||||
final updatedUser = currentUser.copyWith(
|
||||
barberName: barberName.trim(),
|
||||
barberShopName: barberShopName.trim(),
|
||||
);
|
||||
|
||||
await DatabaseService.usersBoxInstance.put(currentUser.id, updatedUser);
|
||||
return updatedUser;
|
||||
}
|
||||
|
||||
// Alterar senha
|
||||
Future<bool> changePassword({
|
||||
required String currentPassword,
|
||||
required String newPassword,
|
||||
}) async {
|
||||
final currentUser = getCurrentUser();
|
||||
if (currentUser == null) return false;
|
||||
|
||||
if (!PasswordUtils.verifyPassword(currentPassword, currentUser.passwordHash)) {
|
||||
throw Exception('Senha atual incorreta');
|
||||
}
|
||||
|
||||
final updatedUser = currentUser.copyWith(
|
||||
passwordHash: PasswordUtils.hashPassword(newPassword),
|
||||
);
|
||||
|
||||
await DatabaseService.usersBoxInstance.put(currentUser.id, updatedUser);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user