Created
February 13, 2021 18:52
-
-
Save mulieriq/d19d7cd82d5025efcd8251980869462e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Author : Eng.Muli Eric | |
Project : The Epistles Fellowhip Mobile App | |
Platform : Mobile Application | |
Technology : Flutter | |
Target : Android And IoS Os | |
Date : Fri May 31 st 23:18:45 | |
Page Title : Login Section | |
*/ | |
import 'dart:convert'; | |
import 'package:the_epistles_alpha/ui/auth/disabled.dart'; | |
import 'package:toast/toast.dart'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/widgets.dart'; | |
import 'package:scoped_model/scoped_model.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
import 'package:the_epistles_alpha/controllers/core/scoped.dart'; | |
import 'package:the_epistles_alpha/model/users.dart'; | |
import 'package:the_epistles_alpha/routes/app.dart'; | |
import 'package:the_epistles_alpha/ui/auth/setProfile.dart'; | |
import 'package:the_epistles_alpha/ui/auth/singup.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
class Login extends StatefulWidget { | |
@override | |
_LoginState createState() => _LoginState(); | |
} | |
class _LoginState extends State<Login> { | |
String email; | |
String password; | |
String error; | |
bool login = false; | |
bool passwordview = true; | |
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; | |
Scoped model = Scoped(); | |
Future signIn(String email, String password) async { | |
await _firebaseAuth | |
.signInWithEmailAndPassword(email: email, password: password) | |
.then((user) async { | |
if (user.isEmailVerified) { | |
await Firestore.instance | |
.collection('users') | |
.document(user.uid) | |
.get() | |
.then((result) async { | |
print(result.data); | |
if (result.data == null) { | |
return Navigator.of(context).pushReplacement(MaterialPageRoute( | |
builder: (BuildContext context) => SetProfile( | |
uid: user.uid, | |
userEmail: user.email, | |
))); | |
} else { | |
if (result['verified']['verification'] == false) { | |
return Navigator.of(context).pushReplacement(MaterialPageRoute( | |
builder: (BuildContext context) => DisabledUser( | |
disabledUserId: user.uid, | |
disabledUserImage: result['userImage'], | |
disabledUserName: result['userName'], | |
reason: result['verified']['message'], | |
))); | |
} else { | |
Firestore.instance | |
.collection('users') | |
.document(user.uid) | |
.updateData({ | |
'verified': { | |
'verification': true, | |
'message': '', | |
'logout': false | |
} | |
}); | |
SharedPreferences autoAuthToken = | |
await SharedPreferences.getInstance(); | |
autoAuthToken.setString('emailToken', user.email); | |
autoAuthToken.setString('passwordToken', password); | |
autoAuthToken.setString('username', result['userName']); | |
autoAuthToken.setString('role', result['userBio']['role']); | |
autoAuthToken.setString('uid', user.uid); | |
autoAuthToken.setString('year', result['userBio']['year']); | |
autoAuthToken.setString('userImage', result['userImage']); | |
autoAuthToken.setString( | |
'birthday', result['userBio']['birthday']); | |
autoAuthToken.setString('collage', result['userBio']['collage']); | |
autoAuthToken.setString('phoneNumber', result['userPhoneNumber']); | |
autoAuthToken.setString( | |
'family', | |
result['userBio']['family'], | |
); | |
print("verified"); | |
return Navigator.of(context).pushReplacement(MaterialPageRoute( | |
builder: (BuildContext context) => App( | |
uid: user.uid, | |
username: result['userName'], | |
role: result['userBio']['role'], | |
email: result['userEmail'], | |
phonenumber: result['userPhoneNumber'], | |
collage: result['userBio']['collage'], | |
year: result['userBio']['year'], | |
family: result['userBio']['family'], | |
userImage: result['userImage'], | |
birthday: result['userBio']['birthday'], | |
))); | |
} | |
} | |
}); | |
} else if (!user.isEmailVerified) { | |
setState(() { | |
error = "EMAIL IS NOT VERIFIED"; | |
}); | |
setState(() { | |
login = false; | |
}); | |
return showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text('Error'), | |
content: Text(error), | |
actions: <Widget>[ | |
RaisedButton( | |
onPressed: () { | |
Navigator.pop(context); | |
}, | |
child: Text('Okay'), | |
) | |
], | |
); | |
}); | |
} | |
}).catchError((e) { | |
setState(() { | |
login = false; | |
}); | |
if (e.toString().contains('ERROR_INVALID_EMAIL')) { | |
setState(() { | |
error = "INVALID EMAIL"; | |
}); | |
} | |
if (e.toString().contains('ERROR_USER_NOT_FOUND')) { | |
setState(() { | |
error = "INVALID CREDENTIALS "; | |
}); | |
} | |
if (e.toString().contains('ERROR_WRONG_PASSWORD')) { | |
setState(() { | |
error = "INVALID CREDENTIALS"; | |
}); | |
} | |
if (e.toString().contains('ERROR_NETWORK_REQUEST_FAILED')) { | |
setState(() { | |
error = "PLEASE CHECK YOUR INTERNET CONNECTIONS"; | |
}); | |
} else { | |
setState(() { | |
error = | |
"AN ERROR OCCOURED.PLEASE CHECK YOUR CREDENTIALS OR NETWORK CONNECTIONS"; | |
}); | |
print(e); | |
} | |
return showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text('Error'), | |
content: Text(error), | |
actions: <Widget>[ | |
RaisedButton( | |
onPressed: () { | |
Navigator.pop(context); | |
}, | |
child: Text('Okay'), | |
) | |
], | |
); | |
}); | |
}); | |
} | |
String resetPassword; | |
var _formKeys = GlobalKey<FormState>(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Stack( | |
fit: StackFit.expand, | |
children: <Widget>[ | |
ListView( | |
children: <Widget>[ | |
Container( | |
padding: EdgeInsets.all(30), | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
SizedBox( | |
height: 70, | |
), | |
Hero( | |
tag: 'logo', | |
transitionOnUserGestures: true, | |
child: CircleAvatar( | |
backgroundImage: | |
AssetImage('lib/assets/ic_launcher.jpeg'), | |
// backgroundColor: Colors.blue, | |
radius: 50, | |
), | |
), | |
Form( | |
key: _formKeys, | |
child: Column( | |
children: <Widget>[ | |
TextFormField( | |
keyboardType: TextInputType.text, | |
onChanged: (value) { | |
email = value; | |
}, | |
decoration: InputDecoration( | |
prefixIcon: Icon(Icons.person), | |
labelText: 'Email', | |
hintText: 'Email'), | |
validator: (String input) { | |
if (input.isEmpty) { | |
return "Password"; | |
} | |
}), | |
TextFormField( | |
enabled: !login, | |
keyboardType: TextInputType.text, | |
obscureText: passwordview, | |
onChanged: (value) { | |
setState(() { | |
password = value; | |
}); | |
}, | |
decoration: InputDecoration( | |
suffixIcon: passwordview == true | |
? IconButton( | |
icon: Icon(Icons.remove_red_eye), | |
color: Colors.red, | |
onPressed: () { | |
passwordview = !passwordview; | |
}, | |
) | |
: IconButton( | |
icon: Icon(Icons.remove_red_eye), | |
color: Colors.green, | |
onPressed: () { | |
passwordview = !passwordview; | |
}, | |
), | |
enabled: !login, | |
prefixIcon: Icon(Icons.lock), | |
labelText: 'Password', | |
hintText: 'Password'), | |
validator: (String input) { | |
if (input.isEmpty) { | |
return "Password"; | |
} | |
}), | |
], | |
), | |
), | |
SizedBox( | |
height: 20, | |
), | |
Container( | |
child: login | |
? CupertinoActivityIndicator() | |
: RaisedButton( | |
onPressed: () { | |
if (_formKeys.currentState.validate()) { | |
setState(() { | |
login = true; | |
}); | |
signIn(email, password); | |
} | |
}, | |
child: new Text('Login'), | |
shape: StadiumBorder(), | |
color: Colors.blue), | |
), | |
SizedBox( | |
height: 20, | |
), | |
Container( | |
child: login | |
? Container() | |
: InkWell( | |
child: | |
Text(r"Don't have an account ? Sign Up. "), | |
onTap: () => Navigator.push( | |
context, | |
CupertinoPageRoute( | |
builder: (BuildContext context) => | |
SignUp())), | |
), | |
), | |
Container( | |
child: InkWell( | |
child: Text( | |
r"Forgot Password ?", | |
style: TextStyle(color: Colors.redAccent), | |
), | |
onTap: () { | |
return showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text('Enter Your Email '), | |
content: TextField( | |
maxLines: 1, | |
decoration: InputDecoration( | |
labelText: 'Email', | |
hintText: 'Email To Send Rest Link'), | |
onChanged: ((value) { | |
resetPassword = value; | |
}), | |
), | |
actions: <Widget>[ | |
RaisedButton( | |
shape: StadiumBorder(), | |
color: Colors.amberAccent, | |
child: Text('Reset Password'), | |
onPressed: () { | |
if (resetPassword == null) { | |
return Toast.show( | |
"Email Field Is Empty", context); | |
} else { | |
Toast.show("Sending Reset Link ...", | |
context); | |
FirebaseAuth.instance | |
.sendPasswordResetEmail( | |
email: resetPassword) | |
.then((r) { | |
Navigator.pop(context); | |
return Toast.show( | |
"Reset Link Sent Successfully", | |
context); | |
}).catchError((e) { | |
print(e); | |
return Toast.show( | |
"An Error Occoured.Try Again!", | |
context); | |
}); | |
} | |
}, | |
) | |
], | |
); | |
}); | |
}, | |
), | |
), | |
], | |
), | |
), | |
], | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment