Skip to content

Commit

Permalink
Merge pull request #19 from aninarafath6/profile
Browse files Browse the repository at this point in the history
Profile
  • Loading branch information
aninarafath6 committed Oct 25, 2022
2 parents ecc5d64 + c31dfdf commit 125dadd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
53 changes: 35 additions & 18 deletions arnhss-app/lib/features/authentication/repo/auth_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';

import 'package:arnhss/common/routes/index_routes.dart';
import 'package:arnhss/models/user.model.dart';
Expand All @@ -13,9 +14,6 @@ class AuthService with HandleException {
static final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final SharedPrefService _prefService = SharedPrefService();

final CollectionReference _usersCollection =
FirebaseFirestore.instance.collection('users');

Stream<User?>? get user {
return _firebaseAuth.authStateChanges();
}
Expand Down Expand Up @@ -49,7 +47,7 @@ class AuthService with HandleException {
.catchError(
//* handle error
(e) {
print("from from from");
debugPrint("from from from");
throw e;
},
);
Expand All @@ -65,7 +63,6 @@ class AuthService with HandleException {
String? otp,
Callback? callback,
Callback? errorCallback}) async {
// print(vi);
//* verify otp with phone auth provider
AuthCredential credential =
PhoneAuthProvider.credential(verificationId: vi!, smsCode: otp!);
Expand All @@ -86,17 +83,6 @@ class AuthService with HandleException {
handleException(e);
return false;
}
// //* handle firebase exception

// try {
// // print(otp);

// _firebaseAuth.signInWithCredential(_credential);
// } catch (e) {
// print(e);
// handleException(e);
// }
// return _credential;
}

Future<void> signIn(AuthCredential? credential, UserModel user) async {
Expand Down Expand Up @@ -150,20 +136,51 @@ class AuthService with HandleException {
Future<List<UserModel>?> getListUsers(String phone) async {
// * get users who have the same number
QuerySnapshot? querySnapshot;
QuerySnapshot? studentsSnapshot;
QuerySnapshot? teacherSnapshot;
List<UserModel>? docs;

final CollectionReference _usersCollection =
FirebaseFirestore.instance.collection('users');
final CollectionReference _studentsCollection =
FirebaseFirestore.instance.collection('students');
final CollectionReference _teachersCollection =
FirebaseFirestore.instance.collection('teachers');
// final CollectionReference _studentCollection =
// FirebaseFirestore.instance.collection('users');

try {
// * fetch the document which have same phone number
await Future.delayed(const Duration(seconds: 1));

querySnapshot =
await _usersCollection.where("phone", isEqualTo: phone).get();
studentsSnapshot =
await _studentsCollection.where("phone", isEqualTo: phone).get();
teacherSnapshot =
await _teachersCollection.where("phone", isEqualTo: phone).get();

docs = [
...studentsSnapshot.docs
.map((e) => UserModel.fromRawJson(jsonEncode(e.data())))
.toList(),
...teacherSnapshot.docs
.map((e) => UserModel.fromRawJson(jsonEncode(e.data())))
.toList(),
...querySnapshot.docs
.map((e) => UserModel.fromRawJson(jsonEncode(e.data())))
.toList()
];
} catch (e) {
debugPrint(e.toString());
handleException(e);
}

// * if query snapshot has data then return data other wise return null
if (querySnapshot != null) {
return UserModel.listFromJson(querySnapshot);
if (docs != null) {
return docs;
} else {
debugPrint("docs is null");
return null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions arnhss-app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:arnhss/features/notifications/view_model/notification_view_model
import 'package:arnhss/features/planner/view_model/planner_view_model.dart';
import 'package:arnhss/firebase_options.dart';
import 'package:arnhss/services/db_service.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
Expand All @@ -27,6 +28,7 @@ void main() async {
name: 'arnhss',
options: DefaultFirebaseOptions.currentPlatform,
);

runApp(const MyApp());
},
);
Expand Down
2 changes: 1 addition & 1 deletion arnhss-app/lib/services/shared_pref_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SharedPrefService with HandleException {
pref.setBool("login", true);
pref.setString("id", user.id ?? "");
pref.setString("department", UserModel.fromDepartment(user.department!));
pref.setString("role", UserModel.fromRole(user.role ?? Role.student));
pref.setString("role", UserModel.toStringRole(user.role ?? Role.student));
pref.setString("name", user.name ?? "");
pref.setString("user", user.toRawJson());
} catch (e) {
Expand Down

0 comments on commit 125dadd

Please sign in to comment.