MultiAUth Provider
cashlinkml2021
plain_text
2 years ago
8.5 kB
4
Indexable
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:ecommerce/widgets_common/custom_textfield.dart'; import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; import '../../consts/colors.dart'; class PhoneInputScreen extends StatefulWidget { @override _PhoneInputScreenState createState() => _PhoneInputScreenState(); } class _PhoneInputScreenState extends State<PhoneInputScreen> { final _phoneNumberController = TextEditingController(); Future<void> verifyPhoneNumber(BuildContext context) async { await FirebaseAuth.instance.verifyPhoneNumber( phoneNumber: _phoneNumberController.text, verificationCompleted: (PhoneAuthCredential credential) { // This callback will be called when auto-retrieval completes // For example, auto sign-in (not implemented in this basic example) }, verificationFailed: (FirebaseAuthException e) { // Handle verification failure print('Verification Failed: ${e.message}'); }, codeSent: (String verificationId, int? resendToken) { // Navigate to the OTP screen with verificationId Navigator.push( context, MaterialPageRoute( builder: (context) => OTPScreen(verificationId), ), ); }, codeAutoRetrievalTimeout: (String verificationId) {}, ); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: redColor, appBar: AppBar( title: Text('Phone Number Verification'), ), body: Padding( padding: EdgeInsets.all(20.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ TextField( controller: _phoneNumberController, keyboardType: TextInputType.phone, decoration: InputDecoration(labelText: 'Enter Phone Number'), ), SizedBox(height: 20), ElevatedButton( onPressed: () => verifyPhoneNumber(context), child: Text('Send OTP'), ), ], ), ), ); } } class OTPScreen extends StatefulWidget { final String verificationId; OTPScreen(this.verificationId); @override _OTPScreenState createState() => _OTPScreenState(); } class _OTPScreenState extends State<OTPScreen> { final _otpController = TextEditingController(); Future<void> signInWithOTP(String smsCode) async { try { PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.credential( verificationId: widget.verificationId, smsCode: smsCode, ); await FirebaseAuth.instance.signInWithCredential(phoneAuthCredential); DocumentReference documentReference = FirebaseFirestore.instance.collection('Users').doc(FirebaseAuth.instance.currentUser?.uid); DocumentSnapshot snapshot = await documentReference.get(); if (snapshot.exists) { Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => HomeScreenZ()), ); } else { Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => ProfileData()), ); } } catch (e) { // Handle OTP verification failure print('Failed to verify OTP: $e'); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: redColor, appBar: AppBar( title: Text('Enter OTP'), ), body: Padding( padding: EdgeInsets.all(20.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ TextField( controller: _otpController, keyboardType: TextInputType.number, decoration: InputDecoration(labelText: 'Enter OTP'), ), SizedBox(height: 20), ElevatedButton( onPressed: () => signInWithOTP(_otpController.text), child: Text('Verify OTP'), ), ], ), ), ); } } class ProfileData extends StatefulWidget { const ProfileData({super.key}); @override State<ProfileData> createState() => _ProfileDataState(); } class _ProfileDataState extends State<ProfileData> { TextEditingController _nameController = TextEditingController(); TextEditingController _emailController = TextEditingController(); TextEditingController _passwordController = TextEditingController(); TextEditingController _rePasswordController = TextEditingController(); User? user = FirebaseAuth.instance.currentUser; @override Widget build(BuildContext context) { return Scaffold( backgroundColor: redColor, appBar: AppBar( title: Text('Complete Profile'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('${FirebaseAuth.instance.currentUser?.uid}'), TextField( controller: _nameController, keyboardType: TextInputType.text, decoration: InputDecoration(labelText: 'Enter Name'), ), TextField( controller: _emailController, keyboardType: TextInputType.emailAddress, decoration: InputDecoration(labelText: 'Enter Email'), ), TextField( controller: _passwordController, keyboardType: TextInputType.text, decoration: InputDecoration(labelText: 'Enter Password'), ), TextField( controller: _rePasswordController, keyboardType: TextInputType.text, decoration: InputDecoration(labelText: 'Enter Password Again'), ), ElevatedButton( onPressed: () async { final credential = await EmailAuthProvider.credential(email: _emailController.text, password: _passwordController.text); try { final userCredential = await FirebaseAuth.instance.currentUser?.linkWithCredential(credential); await FirebaseFirestore.instance.collection('Users').doc(FirebaseAuth.instance.currentUser?.uid).set({ 'Uid' : FirebaseAuth.instance.currentUser?.uid, 'Email' : _emailController.text, 'Password' : _passwordController.text, 'Name' : _nameController.text }); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => HomeScreenZ()), ); } on FirebaseAuthException catch (e) { switch (e.code) { case "provider-already-linked": print("The provider has already been linked to the user."); break; case "invalid-credential": print("The provider's credential is not valid."); break; case "credential-already-in-use": print("The account corresponding to the credential already exists, " "or is already linked to a Firebase User."); break; // See the API reference for the full list of error codes. default: print("Unknown error."); } } }, child: Text('Submit'), ) ], ), ), ); } } class HomeScreenZ extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( backgroundColor: redColor, appBar: AppBar( title: Text('Home'), ), body: Center( child: Text('Welcome to the Home Screen!'), ), ); } }
Editor is loading...