Skip to content

Commit 9cf735f

Browse files
committed
Merge remote-tracking branch 'origin/auth_navtohomes'
Merging latest upstream branch with main detached and old
2 parents bfd3250 + 1a1a90d commit 9cf735f

File tree

55 files changed

+4964
-53
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4964
-53
lines changed

lib/features/auth/data/firebase_auth_repo.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:azt/features/auth/domain/entities/app_user.dart';
22
import 'package:azt/features/auth/domain/repos/auth_repo.dart';
33
import 'package:azt/features/auth/domain/repos/user_repo.dart';
44
import 'package:firebase_auth/firebase_auth.dart';
5+
// ignore: unused_import
56
import 'package:google_sign_in/google_sign_in.dart';
67
import 'package:flutter/foundation.dart' show kIsWeb;
78

@@ -30,6 +31,7 @@ class FirebaseAuthRepo implements AuthRepo {
3031
email: email,
3132
emailVerified: userCredential.user!.emailVerified,
3233
name: username,
34+
roleAllot: null,
3335
);
3436

3537
//return user
@@ -74,6 +76,7 @@ class FirebaseAuthRepo implements AuthRepo {
7476
email: email,
7577
emailVerified: userCredential.user!.emailVerified,
7678
name: name,
79+
roleAllot: null,
7780
);
7881

7982
//return user
@@ -136,13 +139,15 @@ class FirebaseAuthRepo implements AuthRepo {
136139
email: firebaseUser.email!,
137140
emailVerified: firebaseUser.emailVerified,
138141
name: username,
142+
roleAllot: null,
139143
);
140144
} catch (e) {
141145
// If fetch fails, return without username
142146
return AppUser(
143147
uid: firebaseUser.uid,
144148
email: firebaseUser.email!,
145149
emailVerified: firebaseUser.emailVerified,
150+
roleAllot: null,
146151
);
147152
}
148153
}
@@ -164,6 +169,7 @@ class FirebaseAuthRepo implements AuthRepo {
164169
}
165170
}
166171

172+
//sign in with google
167173
@override
168174
Future<AppUser?> signInWithGoogle() async {
169175
try {
@@ -209,6 +215,7 @@ class FirebaseAuthRepo implements AuthRepo {
209215
email: firebaseUser.email ?? '',
210216
emailVerified: firebaseUser.emailVerified,
211217
name: username,
218+
roleAllot: null,
212219
);
213220
return appUser;
214221
} catch (e) {
@@ -217,6 +224,7 @@ class FirebaseAuthRepo implements AuthRepo {
217224
}
218225
}
219226

227+
220228
Future<void> sendEmailVerification() async {
221229
try {
222230
final user = firebaseAuth.currentUser;
@@ -228,6 +236,7 @@ class FirebaseAuthRepo implements AuthRepo {
228236
}
229237
}
230238

239+
231240
Future<AppUser?> reloadUser() async {
232241
try {
233242
final user = firebaseAuth.currentUser;
@@ -253,6 +262,7 @@ class FirebaseAuthRepo implements AuthRepo {
253262
email: updatedUser.email!,
254263
emailVerified: updatedUser.emailVerified,
255264
name: username,
265+
roleAllot: null,
256266
);
257267
} catch (e) {
258268
throw Exception('Failed to reload user: $e');

lib/features/auth/data/firebase_user_repo.dart

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class FirebaseUserRepo implements UserRepo {
1111
await _firestore.collection('users').doc(uid).set({
1212
'email': email,
1313
'username': username,
14+
'roleAllot': null, //role initialise
1415
});
1516
} catch (e) {
1617
throw Exception('Failed to save user data: $e');
@@ -56,11 +57,37 @@ class FirebaseUserRepo implements UserRepo {
5657
@override
5758
Future<void> deleteUserData(String uid) async {
5859
try {
60+
//delete from users collection
5961
await _firestore.collection('users').doc(uid).delete();
62+
63+
//delete rom role collections if exists
64+
final userData = await getUserData(uid);
65+
final role = userData?['roleAllot'];
66+
67+
if (role != null) {
68+
String collection = '';
69+
if (role == 'customer') {
70+
collection = 'customers';
71+
}
72+
else if (role == 'retailer') {
73+
collection = 'retailers';
74+
}
75+
else if (role == 'wholesaler') {
76+
collection = 'wholesalers';
77+
}
78+
79+
80+
//delete data from collection
81+
if (collection.isNotEmpty) {
82+
await _firestore.collection(collection).doc(uid).delete();
83+
}
84+
}
6085
} catch (e) {
6186
throw Exception('Failed to delete user data: $e');
6287
}
6388
}
89+
90+
//check if user exists
6491
@override
6592
Future<bool> checkIfUserExists(String uid) async{
6693
try {
@@ -70,4 +97,88 @@ class FirebaseUserRepo implements UserRepo {
7097
throw Exception('Failed to check if user exists: $e');
7198
}
7299
}
100+
101+
//update/set user role in users collection
102+
@override
103+
Future<void> updateUserRole(String uid, String role) async {
104+
try {
105+
await _firestore.collection('users').doc(uid).update({
106+
'roleAllot': role,
107+
});
108+
} catch (e) {
109+
throw Exception('Failed to update user role: $e');
110+
}
111+
}
112+
113+
//get user role data from users collection
114+
@override
115+
Future<String?> getUserRole(String uid) async {
116+
try {
117+
final doc = await _firestore.collection('users').doc(uid).get();
118+
return doc.data()?['roleAllot'];
119+
} catch (e) {
120+
throw Exception('Failed to get user role: $e');
121+
}
122+
}
123+
124+
//save role specific data in particular collection
125+
@override
126+
Future<void> saveRoleData(String uid, String role, String address, String pincode, {String? businessName}) async {
127+
try {
128+
String collection = '';
129+
if (role == 'customer') {
130+
collection = 'customers';
131+
}
132+
else if (role == 'retailer'){
133+
collection = 'retailers';
134+
}
135+
else if (role == 'wholesaler'){
136+
collection = 'wholesalers';
137+
}
138+
139+
if (collection.isEmpty) {
140+
throw Exception('Invalid role: $role');
141+
}
142+
143+
final Map<String, dynamic> data = {
144+
'uid': uid,
145+
'address': address,
146+
'pincode': pincode,
147+
};
148+
149+
if (businessName != null && (role == 'retailer' || role == 'wholesaler')) {
150+
data['businessName'] = businessName;
151+
}
152+
153+
await _firestore.collection(collection).doc(uid).set(data);
154+
} catch (e) {
155+
throw Exception('Failed to save role data: $e');
156+
}
157+
}
158+
159+
//get role specific data from respective collection
160+
@override
161+
Future<Map<String, dynamic>?> getRoleData(String uid, String role) async {
162+
try {
163+
String collection = '';
164+
if (role == 'customer') {
165+
collection = 'customers';
166+
}
167+
else if (role == 'retailer'){
168+
collection = 'retailers';
169+
}
170+
else if (role == 'wholesaler'){
171+
collection = 'wholesalers';
172+
}
173+
174+
if (collection.isEmpty) return null;
175+
176+
final doc = await _firestore.collection(collection).doc(uid).get();
177+
if (!doc.exists) return null;
178+
return doc.data();
179+
} catch (e) {
180+
throw Exception('Failed to get role data: $e');
181+
}
182+
}
183+
73184
}

lib/features/auth/domain/entities/app_user.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ class AppUser {
22
final String uid;
33
final String email;
44
final bool emailVerified;
5-
final String name; // Add this field
5+
final String name;
6+
final String? roleAllot;
67

78
AppUser({
89
required this.uid,
910
required this.email,
1011
this.emailVerified = false,
11-
this.name = '', // Default to empty string
12+
this.name = '',
13+
this.roleAllot,
1214
});
1315

1416
// toJson converter
@@ -18,6 +20,7 @@ class AppUser {
1820
'email': email,
1921
'emailVerified': emailVerified,
2022
'name': name,
23+
'rollAllot':roleAllot,
2124
};
2225
}
2326

@@ -28,6 +31,7 @@ class AppUser {
2831
email: jsonUser['email'],
2932
emailVerified: jsonUser['emailVerified'] ?? false,
3033
name: jsonUser['name'] ?? '',
34+
roleAllot: jsonUser['rollAllot'],
3135
);
3236
}
3337
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class RoleData {
2+
3+
final String uid;
4+
final String address;
5+
final String pincode;
6+
final String? businessName;
7+
8+
RoleData({
9+
required this.uid,
10+
required this.address,
11+
required this.pincode,
12+
this.businessName
13+
}
14+
);
15+
16+
Map<String,dynamic> toJson(){
17+
final Map<String,dynamic> data = {
18+
'uid':uid,
19+
'address':address,
20+
'pincode':pincode,
21+
};
22+
23+
if (businessName != null){
24+
data['businessName']= businessName;
25+
}
26+
27+
return data;
28+
}
29+
factory RoleData.fromJson(Map<String, dynamic> json) {
30+
return RoleData(
31+
uid: json['uid'],
32+
address: json['address'],
33+
pincode: json['pincode'],
34+
businessName: json['businessName'],
35+
);
36+
}
37+
}

lib/features/auth/domain/repos/user_repo.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ abstract class UserRepo {
55
Future<void> updateUsername(String uid, String newUsername);
66
Future<void> deleteUserData(String uid);
77
Future<bool> checkIfUserExists(String uid);
8+
9+
//role management
10+
Future<void> updateUserRole(String uid, String role);
11+
Future<String?> getUserRole(String uid);
12+
Future<void> saveRoleData(String uid, String role, String address, String pincode, {String? businessName});
13+
Future<Map<String, dynamic>?> getRoleData(String uid, String role);
814
}

lib/features/auth/presentation/components/my_button.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class MyButton extends StatelessWidget {
1313
padding: const EdgeInsets.all(25),
1414
decoration: BoxDecoration(
1515
color: Theme.of(context).colorScheme.tertiary, //color of button
16-
16+
border: Border.all(
17+
color: Theme.of(context).colorScheme.primary,
18+
width: 1.0,
19+
),
1720
borderRadius: BorderRadius.circular(12),
1821
),
1922
child: Center(

0 commit comments

Comments
 (0)