Skip to content

Commit

Permalink
Finalized phone varification
Browse files Browse the repository at this point in the history
  • Loading branch information
srockstech committed Sep 10, 2022
1 parent a06054a commit a90b815
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified android/.gradle/6.7.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified android/.gradle/6.7.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified android/.gradle/6.7.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified android/.gradle/6.7.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified android/.gradle/6.7.1/javaCompile/javaCompile.lock
Binary file not shown.
Binary file modified android/.gradle/6.7.1/javaCompile/taskHistory.bin
Binary file not shown.
Binary file modified android/.gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified android/.gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion android/local.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sdk.dir=C:\\Users\\sarva\\AppData\\Local\\Android\\sdk
flutter.sdk=C:\\src\\flutter
flutter.buildMode=debug
flutter.buildMode=release
flutter.versionName=1.0.0
flutter.versionCode=1
3 changes: 1 addition & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'package:dyeus/screens/welcome_screen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'constants.dart';
import 'screens/welcome_screen.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down
16 changes: 15 additions & 1 deletion lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class HomeScreen extends StatelessWidget {
class HomeScreen extends StatefulWidget {
@override
State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
FirebaseAuth _auth = FirebaseAuth.instance;

@override
void dispose() {
_auth.signOut();
super.dispose();
}

@override
Widget build(BuildContext context) {
final screenHeight = MediaQuery.of(context).size.height;
Expand Down
57 changes: 23 additions & 34 deletions lib/screens/otp_verification.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:async';

import 'package:dyeus/services/firebase_auth_methods.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:sms_autofill/sms_autofill.dart';

Expand All @@ -24,6 +27,7 @@ class _OTPVerificationState extends State<OTPVerification> {
TextEditingController otpController;
FirebaseAuthMethods firebaseAuthMethod;
FirebaseAuth _auth = FirebaseAuth.instance;
UserCredential userCredential;

Widget bottomHelperOptions(screenHeight) {
if (buttonText == 'Done') {
Expand Down Expand Up @@ -62,53 +66,39 @@ class _OTPVerificationState extends State<OTPVerification> {
}
}

void sendAndReadOTP() {
firebaseAuthMethod = FirebaseAuthMethods(
void triggerFirebaseAuthVerification() async {
//todo: Make it future and await in case of error
await Future.sync(() {
firebaseAuthMethod = FirebaseAuthMethods(
context: context,
otpController: otpController,
otpCode: (value) {
setState(() {
otp = value;
});
});
}

Future<void> autoFillOTPAndVerifyUser() async {
bool verify = await firebaseAuthMethod.phoneSignin(widget.phoneNumber);
if (verify == true) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
},
);
}
}

void verifyUserFromAutoDetectedOTP() async {
sendAndReadOTP();
await autoFillOTPAndVerifyUser();
}

Future<void> verifyUserFromManuallyEnteredOTP() async {
bool verify = await firebaseAuthMethod.verifyManuallyEnteredOtp();
if (verify == true) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
);
}
});
userCredential = await firebaseAuthMethod.phoneSignIn(widget.phoneNumber);
_auth.authStateChanges().listen((user) {
if (user != null) {
Fluttertoast.showToast(msg: 'Verification Successful!');
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
);
}
});
}

@override
void initState() {
buttonText = 'Resend OTP';
otpController = TextEditingController();
startResendOTPTimer();
sendAndReadOTP();
autoFillOTPAndVerifyUser();
triggerFirebaseAuthVerification();
super.initState();
}

Expand Down Expand Up @@ -236,7 +226,6 @@ class _OTPVerificationState extends State<OTPVerification> {
),
onPressed: () async {
if (buttonText == 'Done') {
await verifyUserFromManuallyEnteredOTP();
} else if (buttonText == 'Resend OTP' && resendOTPGap == 0) {
initState();
}
Expand Down
58 changes: 27 additions & 31 deletions lib/services/firebase_auth_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,17 @@ class FirebaseAuthMethods {
FirebaseAuthMethods(
{@required this.context, @required this.otpController, this.otpCode});

Future<bool> verifyManuallyEnteredOtp() async {
bool success = false;
PhoneAuthCredential credential;
try {
credential = PhoneAuthProvider.credential(
verificationId: FirebaseAuthMethods.codeVerificationId,
smsCode: otpController.text.trim(),
);
} catch (e) {
Fluttertoast.showToast(msg: e.toString());
}
try {
final user = await _auth.signInWithCredential(credential);
if (user != null) {
Fluttertoast.showToast(msg: 'Verification Successful!');
success = true;
}
} catch (e) {
Fluttertoast.showToast(msg: e.toString());
}
return success;
}

Future<bool> phoneSignin(String phoneNumber) async {
Future<UserCredential> phoneSignIn(String phoneNumber) async {
String otp;
bool success = false;
UserCredential userCredential;
await _auth.verifyPhoneNumber(
phoneNumber: '+91$phoneNumber',
verificationCompleted: (credential) async {
try {
final user = await _auth.signInWithCredential(credential);
if (user != null) {
if (_auth.currentUser != null) {
otp = credential.smsCode;
otpCode = (otp) {};
Fluttertoast.showToast(msg: 'Verified Successfully!');
success = true;
}
} catch (e) {
Fluttertoast.showToast(msg: e);
Expand All @@ -57,11 +32,32 @@ class FirebaseAuthMethods {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(e.message)));
},
// autoRetrievedSmsCodeForTesting: '987654',
codeSent: (verificationId, resendToken) async {
codeVerificationId = verificationId;
PhoneAuthCredential credential;
try {
credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: otpController.text.trim(),
);
} catch (e) {
Fluttertoast.showToast(msg: e.toString() + 'ok');
}
try {
await _auth.signInWithCredential(credential);
} catch (e) {
Fluttertoast.showToast(msg: e.toString());
}
},
codeAutoRetrievalTimeout: (value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Unable to automatically read the code. Please enter it manually.'),
),
);
},
codeAutoRetrievalTimeout: (value) {},
);
return success;
return userCredential;
}
}

0 comments on commit a90b815

Please sign in to comment.