296 lines
9.5 KiB
Dart
296 lines
9.5 KiB
Dart
import 'package:faucethub/Backend/DebugHelper.dart';
|
|
import 'package:faucethub/Backend/Dialogs.dart';
|
|
import 'package:faucethub/Backend/login_mgr.dart';
|
|
|
|
import 'package:faucethub/Backend/googleSignIn.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
TextEditingController usernameTxtController = TextEditingController();
|
|
TextEditingController passwordTxtController = TextEditingController();
|
|
|
|
bool isLogin = true;
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ChangeNotifierProvider(
|
|
create:(context)=>GoogleSignInProvider(),
|
|
builder: (context, snapshot) {
|
|
return Scaffold(
|
|
body: Container(
|
|
alignment: Alignment.center,
|
|
child: Card(
|
|
child: (isLogin) ? LoginCard() : RegisterCard(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
);
|
|
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
|
|
autoLogin();
|
|
}
|
|
|
|
void autoLogin() async{
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
try{
|
|
if(prefs.getBool("googleLogin") ?? false){
|
|
SignWithGoogle();
|
|
}
|
|
}catch(e){
|
|
|
|
}
|
|
}
|
|
|
|
Widget LoginCard(){
|
|
return Container(
|
|
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 50),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text("Welcome!",style: TextStyle(fontSize: 25),), SizedBox(height: 10,),
|
|
|
|
Text("Login to continue"),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.max,
|
|
children: [
|
|
Container(child: Icon(Icons.person)),
|
|
Expanded(child: TextField(controller: usernameTxtController,decoration: const InputDecoration(
|
|
labelText: 'Username',
|
|
))),
|
|
],
|
|
),
|
|
SizedBox(height: 10,),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.max,
|
|
children: [
|
|
Container(child: Icon(Icons.key)),
|
|
Expanded(child: TextField(controller: passwordTxtController,obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
),)),
|
|
],
|
|
),
|
|
MaterialButton(onPressed: (){
|
|
if(usernameTxtController.value.text.length < 3){
|
|
Dialogs.showAlertDialog(context, "Invalid", "Username entered is invalid, It must be longer than 3 letters");
|
|
return;
|
|
}
|
|
if(passwordTxtController.value.text.length < 5){
|
|
Dialogs.showAlertDialog(context, "Invalid", "Password entered is invalid, It must be longer than 5 letters");
|
|
return;
|
|
}
|
|
Login();
|
|
setState(() {
|
|
|
|
});
|
|
},child: Text("Login"),
|
|
color: Colors.green,
|
|
),
|
|
MaterialButton(onPressed: (){
|
|
isLogin=false;
|
|
setState(() {
|
|
|
|
});
|
|
},child: Text("Don't have an account? Register"),
|
|
color: Colors.grey,
|
|
),
|
|
SizedBox(height: 50,),
|
|
|
|
MaterialButton(onPressed: (){
|
|
SignWithGoogle();
|
|
setState(() {
|
|
|
|
});
|
|
},child: Text("Continue with Google"),
|
|
color: Colors.blueAccent,
|
|
)
|
|
],
|
|
)
|
|
);
|
|
}
|
|
Widget RegisterCard(){
|
|
return Container(
|
|
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 50),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text("Welcome!",style: TextStyle(fontSize: 25),), SizedBox(height: 10,),
|
|
Text("Create a new Account"),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
mainAxisSize: MainAxisSize.max,
|
|
children: [
|
|
Container(child: Icon(Icons.person)),
|
|
Expanded(child: TextField(controller: usernameTxtController,decoration: const InputDecoration(
|
|
labelText: 'Username',
|
|
))),
|
|
],
|
|
),
|
|
SizedBox(height: 10,),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.max,
|
|
children: [
|
|
Container(child: Icon(Icons.key)),
|
|
Expanded(child: TextField(controller: passwordTxtController,obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
),)),
|
|
],
|
|
),
|
|
MaterialButton(onPressed: (){
|
|
if(usernameTxtController.value.text.length < 3){
|
|
Dialogs.showAlertDialog(context, "Invalid", "Username entered is invalid, It must be longer than 3 letters");
|
|
return;
|
|
}
|
|
if(passwordTxtController.value.text.length < 5){
|
|
Dialogs.showAlertDialog(context, "Invalid", "Password entered is invalid, It must be longer than 5 letters");
|
|
return;
|
|
}
|
|
Register();
|
|
|
|
setState(() {
|
|
|
|
});
|
|
},child: Text("Register"),
|
|
color: Colors.green,
|
|
),
|
|
MaterialButton(onPressed: (){
|
|
isLogin=true;
|
|
setState(() {
|
|
|
|
});
|
|
},child: Text("Has an account? Login Here"),
|
|
color: Colors.grey,
|
|
),
|
|
SizedBox(height: 50,),
|
|
|
|
MaterialButton(onPressed: (){
|
|
SignWithGoogle();
|
|
setState(() {
|
|
|
|
});
|
|
},child: Text("Continue with Google"),
|
|
color: Colors.blueAccent,
|
|
)
|
|
],
|
|
)
|
|
);
|
|
}
|
|
|
|
Future<void> SignWithGoogle() async {
|
|
GoogleSignIn _googleSignIn = GoogleSignIn(
|
|
scopes: [
|
|
'email',
|
|
'https://www.googleapis.com/auth/contacts.readonly',
|
|
],
|
|
);
|
|
|
|
try {
|
|
GoogleSignInAccount? account = await _googleSignIn.signIn();
|
|
|
|
print(account?.email);
|
|
|
|
int registerResult = await LoginManager.GoogleLogin(account!.email);
|
|
Dialogs.hide();
|
|
|
|
if(registerResult == 0){
|
|
// Dialogs.showAlertDialog(context, "Success", "Login done, congrats!");
|
|
Navigator.of(context).pushReplacementNamed('/home');
|
|
}else if(registerResult == 1){
|
|
Dialogs.showAlertDialog(context, "Failed", "Servers are unreachable. Please check internet connection and try again.");
|
|
}else if(registerResult == 5){
|
|
Dialogs.showAlertDialog(context, "Failed", "Couldn't Login with this google account");
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
print(error);
|
|
print("google sign in failed");
|
|
}
|
|
|
|
}
|
|
|
|
void Register({String? email = null}) async{
|
|
|
|
Debug.Log("Register clicked");
|
|
Dialogs.waiting();
|
|
int registerResult = 0;
|
|
if(email == null) {
|
|
Debug.Log("Continuing with native login");
|
|
Debug.Log(usernameTxtController.value.text + " : " + passwordTxtController.value.text);
|
|
registerResult = await LoginManager.Register(
|
|
usernameTxtController.value.text, passwordTxtController.value.text);
|
|
}else{
|
|
registerResult = await LoginManager.Register(email!, email!);
|
|
}
|
|
|
|
Dialogs.hide();
|
|
|
|
if(registerResult == 0){
|
|
// Dialogs.showAlertDialog(context, "Success", "Registration done, congrats!");
|
|
Login();
|
|
}else if(registerResult == 1){
|
|
Dialogs.showAlertDialog(context, "Failed", "Servers are unreachable. Please check internet connection and try again.");
|
|
}else if(registerResult == 5){
|
|
Dialogs.showAlertDialog(context, "Failed", "Username already exists.");
|
|
}
|
|
|
|
}
|
|
|
|
void Login() async{
|
|
|
|
Debug.Log("Login clicked");
|
|
Dialogs.waiting();
|
|
|
|
int registerResult = await LoginManager.Login(usernameTxtController.value.text, passwordTxtController.value.text);
|
|
Dialogs.hide();
|
|
|
|
if(registerResult == 0){
|
|
// Dialogs.showAlertDialog(context, "Success", "Login done, congrats!");
|
|
Navigator.of(context).pushReplacementNamed('/home');
|
|
}else if(registerResult == 1){
|
|
Dialogs.showAlertDialog(context, "Failed", "Servers are unreachable. Please check internet connection and try again.");
|
|
}else if(registerResult == 5){
|
|
Dialogs.showAlertDialog(context, "Failed", "Username or password is incorrect.");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void GoogleLogin() async{
|
|
Debug.Log("Google Login clicked");
|
|
Dialogs.waiting();
|
|
|
|
int registerResult = await LoginManager.Login(usernameTxtController.value.text, passwordTxtController.value.text);
|
|
Dialogs.hide();
|
|
|
|
if(registerResult == 0){
|
|
// Dialogs.showAlertDialog(context, "Success", "Login done, congrats!");
|
|
Navigator.of(context).pushReplacementNamed('/home');
|
|
}else if(registerResult == 1){
|
|
Dialogs.showAlertDialog(context, "Failed", "Servers are unreachable. Please check internet connection and try again.");
|
|
}else if(registerResult == 5){
|
|
Dialogs.showAlertDialog(context, "Failed", "Username or password is incorrect.");
|
|
}
|
|
}
|
|
|
|
}
|