Created
December 29, 2019 06:15
-
-
Save genuinemoses/7d6a2d31824d877fab0992ee7ea2f7a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:proseekr/src/models/globals.dart' as globals; | |
import 'package:proseekr/src/views/job_provider/provider_feed.dart'; | |
import 'package:proseekr/src/views/job_seeker/homepage.dart'; | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
class UserLogin extends StatefulWidget { | |
// UserLogin({Key key, this.title}) : super(key: key); | |
final String title = "Login"; | |
@override | |
State<StatefulWidget> createState() => UserLoginState(); | |
} | |
class UserLoginState extends State<UserLogin> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Colors.black, | |
title: Text(widget.title), | |
// actions: <Widget>[ | |
// Builder(builder: (BuildContext context) { | |
// return FlatButton( | |
// child: const Text('Sign out'), | |
// textColor: Theme.of(context).buttonColor, | |
// onPressed: () async { | |
// final FirebaseUser user = await _auth.currentUser(); | |
// if (user == null) { | |
// Scaffold.of(context).showSnackBar(SnackBar( | |
// content: const Text('No one has signed in.'), | |
// )); | |
// return; | |
// } | |
// _signOut(); | |
// final String uid = user.uid; | |
// Scaffold.of(context).showSnackBar(SnackBar( | |
// content: Text(uid + ' has successfully signed out.'), | |
// )); | |
// }, | |
// ); | |
// }) | |
// ], | |
), | |
body: Builder(builder: (BuildContext context) { | |
return Container( | |
child: PhoneSignInSection(Scaffold.of(context)), | |
); | |
}), | |
); | |
} | |
// Example code for sign out. | |
void _signOut() async { | |
await _auth.signOut(); | |
} | |
} | |
FirebaseUser user; | |
class PhoneSignInSection extends StatefulWidget { | |
PhoneSignInSection(this.scaffold); | |
final ScaffoldState scaffold; | |
@override | |
State<StatefulWidget> createState() => PhoneSignInSectionState(); | |
String userGetter() { | |
return user.uid.toString(); | |
} | |
} | |
class PhoneSignInSectionState extends State<PhoneSignInSection> { | |
TextStyle style = TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0); | |
final TextEditingController _phoneNumberController = TextEditingController(); | |
final TextEditingController _smsController = TextEditingController(); | |
String _message = ''; | |
String _verificationId; | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding: const EdgeInsets.symmetric(vertical: 30.0), | |
child: Column(children: <Widget>[ | |
Container( | |
width: MediaQuery.of(context).size.width - 100, | |
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20), | |
child: TextFormField( | |
style: TextStyle(color: Colors.white), | |
autofocus: true, | |
controller: _phoneNumberController, | |
keyboardType: TextInputType.numberWithOptions(), | |
decoration: InputDecoration( | |
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0), | |
hintText: "Phone Number", | |
hintStyle: TextStyle(color: Colors.grey), | |
fillColor: Colors.black, | |
filled: true, | |
enabledBorder: OutlineInputBorder( | |
borderSide: const BorderSide(style: BorderStyle.none), | |
borderRadius: BorderRadius.circular(4.0)), | |
prefixIcon: Icon( | |
Icons.call, | |
color: Colors.white, | |
), | |
prefixText: "+91", | |
prefixStyle: TextStyle(color: Colors.white)), | |
// onPressed: | |
), | |
), | |
//; | |
Container( | |
width: MediaQuery.of(context).size.width - 100, | |
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20), | |
child: TextField( | |
style: TextStyle(color: Colors.white), | |
controller: _smsController, | |
keyboardType: TextInputType.numberWithOptions(), | |
decoration: InputDecoration( | |
fillColor: Colors.black, | |
filled: true, | |
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0), | |
hintText: "Enter OTP", | |
hintStyle: TextStyle(color: Colors.grey), | |
enabledBorder: OutlineInputBorder( | |
borderSide: const BorderSide(style: BorderStyle.none), | |
borderRadius: BorderRadius.circular(4.0)), | |
prefixIcon: Icon( | |
Icons.dialpad, | |
color: Colors.white, | |
), | |
), | |
), | |
), | |
Container( | |
margin: EdgeInsets.symmetric(horizontal: 58), | |
child: Row( | |
children: <Widget>[ | |
Expanded( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Material( | |
elevation: 8.0, | |
borderRadius: BorderRadius.circular(4.0), | |
color: Colors.white, | |
child: MaterialButton( | |
// minWidth: MediaQuery.of(context).size.width / 2 - 100, | |
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0), | |
onPressed: () async { | |
_verifyPhoneNumber(); | |
}, | |
child: Text("Generate OTP", | |
textAlign: TextAlign.center, | |
style: style.copyWith( | |
color: Colors.black, | |
)), | |
), | |
), | |
), | |
), | |
Expanded( | |
child: Padding( | |
padding: const EdgeInsets.only( | |
top: 8.0, right: 16.0, bottom: 8.0, left: 16.0), | |
child: Material( | |
elevation: 8.0, | |
borderRadius: BorderRadius.circular(4.0), | |
color: Colors.black, | |
child: MaterialButton( | |
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0), | |
onPressed: () async { | |
_signInWithPhoneNumber(); | |
print(_phoneNumberController.text + "st1"); | |
}, | |
child: Text("Submit", | |
textAlign: TextAlign.center, | |
style: style.copyWith(color: Colors.white)), | |
), | |
), | |
), | |
), | |
], | |
)), | |
Container( | |
alignment: Alignment.center, | |
padding: const EdgeInsets.symmetric(horizontal: 16), | |
child: Text(_message, style: TextStyle(color: Colors.red)), | |
), | |
])); | |
} | |
// Exmaple code of how to veify phone number | |
void _verifyPhoneNumber() async { | |
setState(() { | |
_message = 'Verify Phone Number'; | |
}); | |
/*Redirects to Desired Page When sucessfull Login*/ | |
final PhoneVerificationCompleted verificationCompleted = | |
(AuthCredential phoneAuthCredential) { | |
_auth.signInWithCredential(phoneAuthCredential).then((user) { | |
// Navigator.push(context, MaterialPageRoute(builder: (context) => ProviderFeed())); | |
if (globals.SelectedActor.JobProvider.toString() == "Job Provider") { | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => ProviderFeed()), | |
); | |
} else { | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => HomePage.direct()), | |
); | |
} | |
}); | |
setState(() { | |
_message = 'Received phone auth credential: $phoneAuthCredential'; | |
}); | |
}; | |
final PhoneVerificationFailed verificationFailed = | |
(AuthException authException) { | |
setState(() { | |
_message = | |
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}'; | |
print(_phoneNumberController.text + "st2"); | |
}); | |
}; | |
final PhoneCodeSent codeSent = | |
(String verificationId, [int forceResendingToken]) async { | |
widget.scaffold.showSnackBar(SnackBar( | |
content: | |
const Text('Please check your phone for the verification code.'), | |
)); | |
_verificationId = verificationId; | |
}; | |
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout = | |
(String verificationId) { | |
_verificationId = verificationId; | |
}; | |
await _auth.verifyPhoneNumber( | |
phoneNumber: "+91" + _phoneNumberController.text, | |
timeout: const Duration(seconds: 30), | |
verificationCompleted: verificationCompleted, | |
verificationFailed: verificationFailed, | |
codeSent: codeSent, | |
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout); | |
print(_phoneNumberController.text + "st3"); | |
} | |
void _signInWithPhoneNumber() async { | |
final AuthCredential credential = PhoneAuthProvider.getCredential( | |
verificationId: _verificationId, | |
smsCode: _smsController.text, | |
); | |
user = (await _auth.signInWithCredential(credential)) as FirebaseUser; | |
final FirebaseUser currentUser = await _auth.currentUser(); | |
assert(user.uid == currentUser.uid); | |
setState(() { | |
if (user != null) { | |
_message = 'Successfully signed in, uid: ' + user.uid; | |
if (globals.SelectedActor.JobProvider.toString() == "Job Provider") { | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => ProviderFeed()), | |
); | |
} else { | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => HomePage(user.uid)), | |
); | |
} | |
} else { | |
_message = 'Sign in failed'; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment