Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ntorship into dev-dev
  • Loading branch information
AshwinRNair-ARN committed Nov 29, 2023
2 parents 360281c + ccd4552 commit a28c76c
Show file tree
Hide file tree
Showing 32 changed files with 482 additions and 508 deletions.
4 changes: 3 additions & 1 deletion lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:iiitd_mentorship/app/views/screens/driver.dart';
import 'package:iiitd_mentorship/app/views/screens/home/home.dart';
import 'package:iiitd_mentorship/app/views/screens/profile/profile.dart';
import 'package:iiitd_mentorship/app/views/screens/schedule/schedule.dart';
import 'package:iiitd_mentorship/app/views/screens/search/search.dart';
import 'package:iiitd_mentorship/theme.dart';

class MyApp extends StatelessWidget {
Expand All @@ -36,12 +37,13 @@ class MyApp extends StatelessWidget {
'/profile': (BuildContext context) => const ProfileScreen(),
'/settings': (BuildContext context) => const MyHomePage(),
'/notifications': (BuildContext context) => const MyHomePage(),
'onboarding': (BuildContext context) => const OnBoardsScreen(),
'/onboarding': (BuildContext context) => const OnBoardsScreen(),
'/login': (BuildContext context) => const LoginScreen(),
'/signup': (BuildContext context) => const SignUpScreen(),
'/phoneauth': (BuildContext context) => const PhoneAuthScreen(),
'/otpscreen': (BuildContext context) => const OTPScreen(),
'/userdetails': (BuildContext context) => const UserDetailsScreen(),
'/search': (BuildContext context) => const SearchScreen(),
'/chat': (BuildContext context) =>
const ChatScreen(title: "Welcome to Chat"),
'/home/schedule': (BuildContext context) =>
Expand Down
96 changes: 60 additions & 36 deletions lib/app/bloc/auth/auth_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,59 +1,83 @@
import 'package:bloc/bloc.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:iiitd_mentorship/app/data/interfaces/responses.dart';
import 'package:iiitd_mentorship/app/data/model/auth_status.dart';
import 'package:iiitd_mentorship/app/data/model/user_auth.dart';
import 'package:iiitd_mentorship/app/data/repository/auth.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:meta/meta.dart';

part 'auth_event.dart';
part 'auth_state.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository _auth = AuthRepository();
final currentUser = FirebaseAuth.instance.currentUser;
AuthStatus status = AuthStatus.unknown;

AuthBloc() : super(AuthInitial()) {
on<AuthEvent>((event, emit) async {
on<AuthLogin>((event, emit) async {
emit(AuthLoading());
final user = (event).user;
final response = await _auth.login(user);

status = AuthStatus.authenticated;

emit(_validateResponse(response, status));
});

on<AuthSignUp>((event, emit) async {
emit(AuthLoading());
final user = (event).user;
final response = await _auth.signup(user);
status = AuthStatus.pending;

emit(_validateResponse(response, status));
});

on<AuthLogout>((event, emit) async {
emit(AuthLoading());
switch (event.runtimeType) {
case const (AuthLogin):
final user = (event as AuthLogin).user;
final response = await _auth.login(user);

emit(_validateResponse(response));
break;
case const (AuthSignUp):
final user = (event as AuthSignUp).user;
final response = await _auth.signup(user);

emit(_validateResponse(response));
break;
case const (AuthLogout):
final response = await _auth.signout();

emit(_validateResponse(response));
break;
case AuthLoginWithGoogle:
final response = await _auth.loginWithGoogle();

emit(_validateResponse(response));
break;
case AuthPhoneSignIn:
final phoneNumber = (event as AuthPhoneSignIn).phoneNumber;
final response = await _auth.phoneSignIn(phoneNumber);

emit(_validateResponse(response));
break;
default:
final response = await _auth.signout();

status = AuthStatus.unauthenticated;

emit(_validateResponse(response, status));
});

on<AuthSignUpWithGoogle>((event, emit) async {
emit(AuthLoading());
final response = await _auth.loginWithGoogle();

// check if user is already signed up
final email = (response.data as UserCredential).user!.email!;

final isSignedUp = await _auth.isAlreadySignedUp(email);

if (isSignedUp.hasError) {
status = AuthStatus.unknown;
emit(_validateResponse(isSignedUp, status));
} else {
if (isSignedUp.data as bool) {
status = AuthStatus.authenticated;
emit(_validateResponse(response, status));
} else {
status = AuthStatus.pending;
emit(_validateResponse(response, status));
}
}
});

on<AuthLoginWithGoogle>((event, emit) async {
emit(AuthLoading());
final response = await _auth.loginWithGoogle();
status = AuthStatus.authenticated;
emit(_validateResponse(response, status));
});
}

_validateResponse(FirebaseResponse response) {
_validateResponse(FirebaseResponse response, AuthStatus status) {
if (response.hasError) {
return UnAuthenticated(response.message!);
return UnAuthenticated(response.message!, status: AuthStatus.unknown);
} else {
return Authenticated(response);
return Authenticated(response, status: status);
}
}
}
6 changes: 1 addition & 5 deletions lib/app/bloc/auth/auth_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ final class AuthLogout extends AuthEvent {}

final class AuthLoginWithGoogle extends AuthEvent {}

final class AuthPhoneSignIn extends AuthEvent {
final String phoneNumber;

AuthPhoneSignIn({required this.phoneNumber});
}
final class AuthSignUpWithGoogle extends AuthEvent {}
6 changes: 4 additions & 2 deletions lib/app/bloc/auth/auth_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ final class AuthInitial extends AuthState {}

final class Authenticated extends AuthState {
final FirebaseResponse response;
final AuthStatus status;

Authenticated(this.response);
Authenticated(this.response, {this.status = AuthStatus.authenticated});
}

final class UnAuthenticated extends AuthState {
final String message;
final AuthStatus status;

UnAuthenticated(this.message);
UnAuthenticated(this.message, {this.status = AuthStatus.unauthenticated});
}

final class AuthLoading extends AuthState {}
2 changes: 1 addition & 1 deletion lib/app/data/interfaces/responses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sealed class ResponseBase {
};
}

bool get hasError => error != null;
bool get hasError => error.runtimeType != Null;
bool get hasData => data != null;
}

Expand Down
1 change: 1 addition & 0 deletions lib/app/data/model/auth_status.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum AuthStatus { authenticated, unauthenticated, pending, unknown }
File renamed without changes.
11 changes: 2 additions & 9 deletions lib/app/data/model/user_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,16 @@ final class UserAuthLogin extends UserAuth {
}

final class UserAuthSignUp extends UserAuth {
final bool isMentor;
final String name;

UserAuthSignUp(
{String? email,
String? password,
required this.isMentor,
required this.name})
UserAuthSignUp({String? email, String? password, required this.name})
: super(email!, password!);

UserAuthSignUp.fromJson(Map<String, dynamic> json)
: isMentor = json['isMentor'],
name = json['name'],
: name = json['name'],
super.fromJson(json);

Map<String, dynamic> toJson() => {
'isMentor': isMentor,
'name': name,
...super.toJson(),
};
Expand Down
18 changes: 11 additions & 7 deletions lib/app/data/repository/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@ import 'package:iiitd_mentorship/app/data/model/user_auth.dart';
import 'package:iiitd_mentorship/app/data/services/firebase_auth.dart';

class AuthRepository {
final FirebaseServiceAuth _FirebaseServiceAuth = FirebaseServiceAuth();
final FirebaseServiceAuth _firebaseServiceAuth = FirebaseServiceAuth();

Future<FirebaseResponse> login(UserAuthLogin user) async {
return await _FirebaseServiceAuth.signIn(user);
return await _firebaseServiceAuth.signIn(user);
}

Future<FirebaseResponse> loginWithGoogle() async {
return await _FirebaseServiceAuth.signUpWithGoogle();
return await _firebaseServiceAuth.signUpWithGoogle();
}

Future<FirebaseResponse> phoneSignIn(String phoneNumber) async {
return await _FirebaseServiceAuth.phoneSignIn(phoneNumber);
return await _firebaseServiceAuth.phoneSignIn(phoneNumber);
}

Future<FirebaseResponse> signup(UserAuthSignUp user) async {
return await _FirebaseServiceAuth.signUp(user);
return await _firebaseServiceAuth.signUp(user);
}

Future<FirebaseResponse> signout() async {
return await _FirebaseServiceAuth.signOut();
return await _firebaseServiceAuth.signOut();
}

Future<FirebaseResponse> isAlreadySignedUp(String email) async {
return await _firebaseServiceAuth.userAlreadySignedUp(email);
}

// Future<FirebaseResponse> resetPassword(String email) async {
// return await _FirebaseServiceAuth.rese(email);
// return await _firebaseServiceAuth.rese(email);
// }
}
32 changes: 3 additions & 29 deletions lib/app/data/repository/meeting.dart
Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:iiitd_mentorship/app/data/model/meeting.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

class MeetingData {
static final FirebaseFirestore _firestore = FirebaseFirestore.instance;
static final FirebaseAuth _auth = FirebaseAuth.instance;

static Future<void> addMeeting(Meeting meeting) async {
await _firestore.collection('meetings').add(meeting.toMap());
}

static Stream<List<Meeting>> getMeetingsStream() {
return _firestore
.collection('meetings')
.where('userId', isEqualTo: _auth.currentUser?.uid)
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => Meeting.fromMap(doc.data(), doc.id))
.toList());
}

static Future<void> deleteMeeting(String eventId) async {
await _firestore.collection('meetings').doc(eventId).delete();
}
}

class MeetingDataSource extends CalendarDataSource {
MeetingDataSource(List<Meeting> source) {
class MeetingServiceRepository extends CalendarDataSource {
MeetingServiceRepository(List<Meeting> source) {
appointments = source;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:iiitd_mentorship/app/views/screens/chat/message.dart';
import 'package:iiitd_mentorship/app/data/model/message.dart';

class ChatService extends ChangeNotifier {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Expand Down
48 changes: 36 additions & 12 deletions lib/app/data/services/firebase_auth.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:google_sign_in/google_sign_in.dart';
Expand All @@ -9,9 +10,6 @@ class FirebaseServiceAuth {
bool isSignedUp = false;
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
User? _currentUser;

User? get currentUser => _currentUser;

Future<FirebaseResponse> signUpWithGoogle() async {
try {
Expand All @@ -33,15 +31,13 @@ class FirebaseServiceAuth {
idToken: googleSignInAuthentication.idToken,
accessToken: googleSignInAuthentication.accessToken);

UserCredential userCredential =
await _auth.signInWithCredential(credential);
_currentUser = userCredential.user;
final userCreds = await _auth.signInWithCredential(credential);

return FirebaseResponse(
status: true,
message: 'User signed in with Google successfully',
data: _currentUser,
code: 200,
);
status: true,
message: 'User signed in with Google successfully',
code: 200,
data: userCreds);
} catch (exception) {
return FirebaseResponse(
status: false,
Expand Down Expand Up @@ -194,7 +190,6 @@ class FirebaseServiceAuth {
try {
await _auth.signOut();
_googleSignIn.disconnect();
_currentUser = null;
firebaseResponse.status = true;
firebaseResponse.message = 'User signed out successfully';
firebaseResponse.statusCode = 200;
Expand Down Expand Up @@ -239,4 +234,33 @@ class FirebaseServiceAuth {

return firebaseResponse;
}

Future<FirebaseResponse> userAlreadySignedUp(String email) async {
var firebaseResponse = FirebaseResponse(status: false);

try {
var response = await FirebaseFirestore.instance
.collection('users')
.where('email', isEqualTo: email)
.get();

if (response.docs.isNotEmpty) {
firebaseResponse.status = true;
firebaseResponse.message = 'User already signed up';
firebaseResponse.statusCode = 200;
firebaseResponse.data = true;
} else {
firebaseResponse.status = true;
firebaseResponse.message = 'User not signed up';
firebaseResponse.statusCode = 200;
firebaseResponse.data = false;
}
} on FirebaseException catch (e) {
firebaseResponse.message = 'Eror checking if user already signed up';
firebaseResponse.statusCode = 400;
firebaseResponse.error = e;
}

return firebaseResponse;
}
}
Loading

0 comments on commit a28c76c

Please sign in to comment.