Skip to content

Commit

Permalink
Fixes: Existing user check issue (dscnitrourkela#63)
Browse files Browse the repository at this point in the history
- Adds internet permissions
- Refactoring
  • Loading branch information
abhibhaw authored Nov 12, 2021
1 parent 0635d53 commit 47b6641
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion aptiche/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.dscnitrourkela.aptiche">

<uses-permission android:name="android.permission.INTERNET" />
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
Expand Down
8 changes: 4 additions & 4 deletions aptiche/lib/services/graphql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GraphQLService {
}

///A graphql query that checks whether a user is pre-existing in the database
Future<String?> checkUserbyPhone(String phoneNo) async {
Future<String> checkUserbyPhone(String phoneNo) async {
final QueryOptions options = QueryOptions(
document: gql(getUserbyPhone),
variables: <String, String>{'phone': phoneNo},
Expand All @@ -56,15 +56,16 @@ class GraphQLService {
final QueryResult result = await _client.query(options);

if (result.hasException) {
return Future<String?>.value('null');
return 'null';
} else {
final Map<String, dynamic>? user = result.data;
final String userId = user!['getUserByPhone']['_id'].toString();
_userController.name.value = user['getUserByPhone']['name'].toString();
return userId;
}
} catch (e) {
rethrow;
debugPrint(e.toString());
return 'null';
}
}

Expand Down Expand Up @@ -121,7 +122,6 @@ class GraphQLService {
if (result.hasException) {
debugPrint(result.exception.toString());
}
debugPrint(result.toString());
return result.data!['createUser']['_id'].toString();
}

Expand Down
9 changes: 5 additions & 4 deletions aptiche/lib/services/net/authservice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:aptiche/views/login/logincontroller.dart';
import 'package:aptiche/views/login/loginscreen.dart';
import 'package:aptiche/views/splashscreen/splashscreen.dart';
import 'package:aptiche/views/splashscreen/usercheck.dart';
import 'package:aptiche/widgets/snackbar.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
Expand All @@ -14,7 +15,7 @@ class AuthService extends GetxController {
if (FirebaseAuth.instance.currentUser != null) {
final String token = await getUserToken();
await _graphQL.initGraphQL(token);
return UserCheck();
return UserCheck().checkUserData();
} else
return const LoginView();
}
Expand All @@ -33,13 +34,13 @@ class AuthService extends GetxController {
smsCode: smsCode,
);
await FirebaseAuth.instance.signInWithCredential(authCredential);
await Get.off<UserCheck>(() => UserCheck());
await Get.offAll<SplashScreen>(() => const SplashScreen());
} catch (error) {
debugPrint(error.toString());
/* CustomLoaders().customSnackBar(
CustomLoaders().customSnackBar(
'Authentication Error - WRONG OTP',
'Please enter the correct OTP sent to your mobile number',
); */
);
}
}

Expand Down
11 changes: 6 additions & 5 deletions aptiche/lib/views/dataentry/dataentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import 'package:aptiche/utils/string.dart';
import 'package:aptiche/utils/theme.dart';
import 'package:aptiche/utils/ui_scaling.dart';
import 'package:aptiche/views/dataentry/dataentry_controller.dart';
import 'package:aptiche/views/home/homescreen.dart';
import 'package:aptiche/views/splashscreen/splashscreen.dart';
import 'package:aptiche/widgets/buttons.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
Expand Down Expand Up @@ -152,7 +151,7 @@ class DataEntryScreen extends GetView<DataEntryController> {
TextCapitalization.characters,
controller: controller.rollNoController,
validator: (String? value) {
if (value!.trim() == null ||
if (value!.trim().length != 9 ||
value.isEmpty ||
!value[3].isAlphabetOnly ||
!value[4].isAlphabetOnly) {
Expand Down Expand Up @@ -269,7 +268,7 @@ class DataEntryScreen extends GetView<DataEntryController> {
verticalPadding:
SizeConfig.safeBlockVertical! * 0.27,
text: 'PROCEED',
onTap: () {
onTap: () async {
if (controller.formKey.currentState!
.validate()) {
controller.formKey.currentState!.save();
Expand All @@ -279,7 +278,9 @@ class DataEntryScreen extends GetView<DataEntryController> {
controller.emailController.text,
controller.phoneNo.toString(),
);
Get.offAll<dynamic>(() => const HomeScreen());

await Get.offAll<SplashScreen>(
() => const SplashScreen());
}
}),
),
Expand Down
3 changes: 0 additions & 3 deletions aptiche/lib/views/dataentry/dataentry_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class DataEntryController extends GetxController {
String? jwttoken;

void writeUser(String name, String rollNo, String email, String phone) async {
debugPrint('write-user start');
if (FirebaseAuth.instance.currentUser != null) {
final String jwttoken =
await FirebaseAuth.instance.currentUser!.getIdToken(true);
Expand All @@ -41,8 +40,6 @@ class DataEntryController extends GetxController {
await localUserStorage.write('phoneNo', phone);
await localUserStorage.write('rollNo', rollNo);
await localUserStorage.write('dbID', docId);
debugPrint(localUserStorage.read('rollNo'));
debugPrint('docId: $docId');
}

Future<String?> readUser() async {
Expand Down
4 changes: 2 additions & 2 deletions aptiche/lib/views/login/logincontroller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:aptiche/services/net/authservice.dart';
import 'package:aptiche/views/splashscreen/usercheck.dart';
import 'package:aptiche/views/splashscreen/splashscreen.dart';
import 'package:aptiche/widgets/snackbar.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -31,7 +31,7 @@ class LoginController extends GetxController {
//await FirebaseAuth.instance.signInWithCredential(authResult);
CustomLoaders().customSnackBar('Authentication Successful',
'User Verified with mobile number $phoneNo');
Get.off<UserCheck>(() => UserCheck());
Get.offAll<SplashScreen>(() => const SplashScreen());
};

final PhoneVerificationFailed verificationFailed =
Expand Down
3 changes: 0 additions & 3 deletions aptiche/lib/views/quiz/quiz_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ class QuizController extends GetxController {
bool checkIfAttempted(String quizId) {
final Map? result = localQuizStorage.read<Map>('past');
if (result != null && result[quizId] != null) {
// result.forEach((dynamic key, dynamic value) {
// print('$key : $value');
// });
return true;
} else {
return false;
Expand Down
24 changes: 14 additions & 10 deletions aptiche/lib/views/splashscreen/usercheck.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import 'package:aptiche/views/home/homescreen.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class UserCheck extends StatelessWidget {
UserCheck({Key? key}) : super(key: key);

class UserCheck {
final AuthService _authService = Get.find();
final GraphQLService _graphQLService = Get.find();

@override
Widget build(BuildContext context) {
return _graphQLService.checkUserbyPhone(
_authService.getCurrentUserPhone().toString()) !=
null
? const HomeScreen()
: const DataEntryScreen();
Future<Widget> checkUserData() async {
final String result = await checkUser();
if (result != 'null') {
return const HomeScreen();
} else {
return const DataEntryScreen();
}
}

Future<String> checkUser() async {
final String result = await _graphQLService
.checkUserbyPhone(_authService.getCurrentUserPhone().toString());
return result;
}
}
16 changes: 8 additions & 8 deletions aptiche/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ packages:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.1"
version: "1.7.2"
archive:
dependency: transitive
description:
Expand All @@ -35,7 +35,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.1"
awesome_dropdown:
dependency: "direct main"
description:
Expand Down Expand Up @@ -63,7 +63,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
cli_util:
dependency: transitive
description:
Expand Down Expand Up @@ -463,7 +463,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
nm:
dependency: transitive
description:
Expand Down Expand Up @@ -554,7 +554,7 @@ packages:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
version: "4.3.0"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -636,7 +636,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.2"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -734,7 +734,7 @@ packages:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.2"
version: "5.3.0"
yaml:
dependency: transitive
description:
Expand All @@ -743,5 +743,5 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.13.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=2.0.0"

0 comments on commit 47b6641

Please sign in to comment.