224 lines
7.2 KiB
Dart
224 lines
7.2 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';
|
|
|
|
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(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
);
|
|
|
|
|
|
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 {
|
|
final provider = Provider.of<GoogleSignInProvider>(context, listen:false);
|
|
provider.googleLogin().then((v){
|
|
if(provider.user.id.length > 2){
|
|
if(provider.googleAuth!=null){
|
|
//login(username: provider.user.email,password: provider.user.id);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void Register() async{
|
|
|
|
Debug.Log("Register clicked");
|
|
Dialogs.waiting();
|
|
|
|
int registerResult = await LoginManager.Register(usernameTxtController.value.text, passwordTxtController.value.text);
|
|
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.");
|
|
}
|
|
|
|
}
|
|
}
|