Tambahkan dependensi dan konfigurasi awal proyek
- Tambahkan dependensi utama: GetX, Supabase, SharedPreferences - Konfigurasi struktur awal aplikasi dengan GetX - Inisialisasi layanan Supabase - Perbarui konfigurasi plugin untuk berbagai platform - Ganti template default dengan struktur aplikasi baru
This commit is contained in:
81
lib/app/data/models/user_model.dart
Normal file
81
lib/app/data/models/user_model.dart
Normal file
@ -0,0 +1,81 @@
|
||||
class UserModel {
|
||||
final String id;
|
||||
final String email;
|
||||
final String? name;
|
||||
final String? avatar;
|
||||
final String role;
|
||||
final bool isActive;
|
||||
final DateTime? lastLogin;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
UserModel({
|
||||
required this.id,
|
||||
required this.email,
|
||||
this.name,
|
||||
this.avatar,
|
||||
required this.role,
|
||||
this.isActive = true,
|
||||
this.lastLogin,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
factory UserModel.fromJson(Map<String, dynamic> json) {
|
||||
return UserModel(
|
||||
id: json['id'],
|
||||
email: json['email'],
|
||||
name: json['name'],
|
||||
avatar: json['avatar'],
|
||||
role: json['role'] ?? 'WARGA',
|
||||
isActive: json['is_active'] ?? true,
|
||||
lastLogin: json['last_login'] != null
|
||||
? DateTime.parse(json['last_login'])
|
||||
: null,
|
||||
createdAt: json['CREATED_AT'] != null
|
||||
? DateTime.parse(json['CREATED_AT'])
|
||||
: null,
|
||||
updatedAt: json['UPDATED_AT'] != null
|
||||
? DateTime.parse(json['UPDATED_AT'])
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'email': email,
|
||||
'name': name,
|
||||
'avatar': avatar,
|
||||
'role': role,
|
||||
'is_active': isActive,
|
||||
'last_login': lastLogin?.toIso8601String(),
|
||||
'CREATED_AT': createdAt?.toIso8601String(),
|
||||
'UPDATED_AT': updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
UserModel copyWith({
|
||||
String? id,
|
||||
String? email,
|
||||
String? name,
|
||||
String? avatar,
|
||||
String? role,
|
||||
bool? isActive,
|
||||
DateTime? lastLogin,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
}) {
|
||||
return UserModel(
|
||||
id: id ?? this.id,
|
||||
email: email ?? this.email,
|
||||
name: name ?? this.name,
|
||||
avatar: avatar ?? this.avatar,
|
||||
role: role ?? this.role,
|
||||
isActive: isActive ?? this.isActive,
|
||||
lastLogin: lastLogin ?? this.lastLogin,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
updatedAt: updatedAt ?? this.updatedAt,
|
||||
);
|
||||
}
|
||||
}
|
147
lib/app/data/providers/auth_provider.dart
Normal file
147
lib/app/data/providers/auth_provider.dart
Normal file
@ -0,0 +1,147 @@
|
||||
import 'package:penyaluran_app/app/services/supabase_service.dart';
|
||||
import 'package:penyaluran_app/app/data/models/user_model.dart';
|
||||
|
||||
class AuthProvider {
|
||||
final SupabaseService _supabaseService = SupabaseService.to;
|
||||
|
||||
// Metode untuk mendaftar pengguna baru
|
||||
Future<UserModel?> signUp(String email, String password) async {
|
||||
try {
|
||||
final response = await _supabaseService.signUp(email, password);
|
||||
|
||||
if (response.user != null) {
|
||||
// Tunggu beberapa saat agar trigger di database berjalan
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
|
||||
// Ambil profil pengguna dari database
|
||||
final profileData = await _supabaseService.getUserProfile();
|
||||
|
||||
if (profileData != null) {
|
||||
return UserModel.fromJson({
|
||||
...profileData,
|
||||
'id': response.user!.id,
|
||||
'email': response.user!.email!,
|
||||
});
|
||||
}
|
||||
|
||||
// Jika profil belum tersedia, gunakan data default
|
||||
return UserModel(
|
||||
id: response.user!.id,
|
||||
email: response.user!.email!,
|
||||
role: 'WARGA', // Default role
|
||||
);
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Metode untuk login
|
||||
Future<UserModel?> signIn(String email, String password) async {
|
||||
try {
|
||||
final response = await _supabaseService.signIn(email, password);
|
||||
|
||||
if (response.user != null) {
|
||||
// Ambil profil pengguna dari database
|
||||
final profileData = await _supabaseService.getUserProfile();
|
||||
|
||||
if (profileData != null) {
|
||||
return UserModel.fromJson({
|
||||
...profileData,
|
||||
'id': response.user!.id,
|
||||
'email': response.user!.email!,
|
||||
});
|
||||
}
|
||||
|
||||
// Jika profil belum tersedia, gunakan data default
|
||||
return UserModel(
|
||||
id: response.user!.id,
|
||||
email: response.user!.email!,
|
||||
role: 'WARGA', // Default role
|
||||
);
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Metode untuk logout
|
||||
Future<void> signOut() async {
|
||||
try {
|
||||
await _supabaseService.signOut();
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Metode untuk mendapatkan user saat ini
|
||||
Future<UserModel?> getCurrentUser() async {
|
||||
final user = _supabaseService.currentUser;
|
||||
if (user != null) {
|
||||
// Ambil profil pengguna dari database
|
||||
final profileData = await _supabaseService.getUserProfile();
|
||||
|
||||
if (profileData != null) {
|
||||
return UserModel.fromJson({
|
||||
...profileData,
|
||||
'id': user.id,
|
||||
'email': user.email!,
|
||||
});
|
||||
}
|
||||
|
||||
// Jika profil belum tersedia, gunakan data default
|
||||
return UserModel(
|
||||
id: user.id,
|
||||
email: user.email!,
|
||||
role: 'WARGA', // Default role
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Metode untuk memeriksa apakah user sudah login
|
||||
bool isAuthenticated() {
|
||||
return _supabaseService.isAuthenticated;
|
||||
}
|
||||
|
||||
// Metode untuk mendapatkan data warga
|
||||
Future<Map<String, dynamic>?> getWargaData() async {
|
||||
return await _supabaseService.getWargaByUserId();
|
||||
}
|
||||
|
||||
// Metode untuk membuat profil warga
|
||||
Future<void> createWargaProfile({
|
||||
required String nik,
|
||||
required String namaLengkap,
|
||||
required String jenisKelamin,
|
||||
String? noHp,
|
||||
String? alamat,
|
||||
String? tempatLahir,
|
||||
DateTime? tanggalLahir,
|
||||
String? agama,
|
||||
}) async {
|
||||
await _supabaseService.createWargaProfile(
|
||||
nik: nik,
|
||||
namaLengkap: namaLengkap,
|
||||
jenisKelamin: jenisKelamin,
|
||||
noHp: noHp,
|
||||
alamat: alamat,
|
||||
tempatLahir: tempatLahir,
|
||||
tanggalLahir: tanggalLahir,
|
||||
agama: agama,
|
||||
);
|
||||
}
|
||||
|
||||
// Metode untuk mendapatkan notifikasi pengguna
|
||||
Future<List<Map<String, dynamic>>> getUserNotifications(
|
||||
{bool unreadOnly = false}) async {
|
||||
return await _supabaseService.getUserNotifications(unreadOnly: unreadOnly);
|
||||
}
|
||||
|
||||
// Metode untuk menandai notifikasi sebagai telah dibaca
|
||||
Future<void> markNotificationAsRead(int notificationId) async {
|
||||
await _supabaseService.markNotificationAsRead(notificationId);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user