Merge Loginpage to master #9

Merged
maen0574 merged 13 commits from LoginPage into master 2022-05-17 10:19:58 +02:00
2 changed files with 106 additions and 10 deletions
Showing only changes of commit d6b1c31a2e - Show all commits

View File

@ -4,19 +4,28 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_applicationdemo/BottomNavPage.dart';
import 'package:flutter_applicationdemo/GoogleSignInProvider.dart';
import 'package:flutter_applicationdemo/mysql.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_applicationdemo/HomePage.dart';
import 'package:flutter_signin_button/flutter_signin_button.dart';
import 'package:provider/provider.dart';
import 'GoogleSignInProvider.dart';
import 'signInPage.dart';
import 'globals.dart';
import 'user.dart';
class LoginPage extends StatefulWidget {
_LoginPageState createState() => _LoginPageState();
class CreateAccountPage extends StatefulWidget {
_CreateAccountPageState createState() => _CreateAccountPageState();
}
class _LoginPageState extends State<LoginPage> {
class _CreateAccountPageState extends State<CreateAccountPage> {
Color pinkBackgroundColor = const Color.fromARGB(255, 240, 229, 229);
Color textColor = const Color.fromARGB(255, 79, 98, 114);
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
TextEditingController userNameController = TextEditingController();
var db = mysql();
late user loggedInUser;
@override
Widget build(BuildContext context) {
@ -64,7 +73,7 @@ class _LoginPageState extends State<LoginPage> {
context,
MaterialPageRoute(
builder: (context) =>
LoginPage()), //Replace Container() with call to Map-page.
CreateAccountPage()), //Replace Container() with call to Map-page.
);
} else {
Navigator.push(
@ -97,17 +106,17 @@ class _LoginPageState extends State<LoginPage> {
InputField createUsernameField() {
return InputField(
text: "Username:", isPassword: false, icon: Icon(Icons.person));
text: "Username:", isPassword: false, icon: Icon(Icons.person), controller: userNameController);
}
InputField createEmailField() {
return InputField(
text: "Email:", isPassword: false, icon: Icon(Icons.email));
text: "Email:", isPassword: false, icon: Icon(Icons.email), controller: emailController);
}
InputField createPasswordField() {
return InputField(
text: "Password:", isPassword: true, icon: Icon(Icons.lock));
text: "Password:", isPassword: true, icon: Icon(Icons.lock), controller: passwordController);
}
Padding createBackButton() {
@ -131,7 +140,19 @@ class _LoginPageState extends State<LoginPage> {
ElevatedButton createCreateAccountButton() {
return ElevatedButton(
onPressed: () {},
onPressed: () async{
UserInput userInput = UserInput(isValid: false, errorMessage: "");
await verifyUserInput(emailController.text, userNameController.text, passwordController.text, userInput);
if(userInput.isValid){
await createUserInSQL(emailController.text, userNameController.text, passwordController.text);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
}else{
createUserError(userInput.errorMessage);
}
},
child: Text(
"Create Account",
style: TextStyle(color: Colors.black, fontWeight: FontWeight.w400),
@ -159,17 +180,89 @@ class _LoginPageState extends State<LoginPage> {
),
);
}
} // _LoginPageState
Future<void> createUserInSQL(String email, String username, String password) async{
await db.getConnection().then((conn) async{
String sql = "INSERT INTO maen0574.user (id, email, password, username) VALUES (null, '$email', '$password', '$username');";
await conn.query(sql);
sql = "Select id, email, username from maen0574.user where email = '$email'";
await conn.query(sql).then((results){
for(var row in results){
setState(() {
});
loggedInUser = new user(row[0], row[1], row[2]);
LOGGED_IN_USER.userID = loggedInUser.userID;
}
});
});
}
Future<void> verifyUserInput(String email, String username, String password, userInput) async {
userInput.isValid = false;
if(email.contains("'") || !email.contains("@") || email.length < 5){
userInput.errorMessage = "Incorrect email format";
return;
}else if(username.contains("'") || username.length < 6){
userInput.errorMessage = "Incorrect username. \nCharacters limited to a-z, A-Z, 0-9.";
return;
}else if(password.contains("'") || password.length < 6) {
userInput.errorMessage = "Incorrect password. \nPassword can't contain ' and needs to be atleast 6 characters long";
return;
}
await db.getConnection().then((conn) async{
String sql = "SELECT email from maen0574.user where email = '$email';";
var results = await conn.query(sql);
userInput.isValid = true;
for(var row in results){
userInput.isValid = false;
userInput.errorMessage = "email already registererd";
}
});
return;
}
void createUserError(String stringContext) {
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("Couldn't create user"),
content: Text(stringContext),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK'),
),
],
),
);
}
}
class UserInput {
bool isValid;
String errorMessage;
UserInput({
required this.isValid,
required this.errorMessage
});
bool getIsValid(){
return isValid;
}
} // _LoginPageState
class InputField extends StatelessWidget {
final Icon icon;
final String text;
final bool isPassword;
final TextEditingController controller;
const InputField({
Key? key,
required this.text,
required this.isPassword,
required this.icon,
required this.controller
}) : super(key: key);
@override
@ -187,6 +280,7 @@ class InputField extends StatelessWidget {
),
child: TextField(
obscureText: isPassword,
controller: controller,
decoration: InputDecoration(
hintText: text,
icon: icon,

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'BottomNavPage.dart';
import 'package:flutter_applicationdemo/CreateAccountPage.dart';
import 'globals.dart';
import 'signInPage.dart';
import 'user.dart';
import 'globals.dart' as globals;
@ -18,6 +19,7 @@ class _HomePageState extends State<HomePage> {
Color textColor = const Color.fromARGB(255, 79, 98, 114);
@override
Widget build(BuildContext context) {
print(LOGGED_IN_USER.userID);
return Scaffold(
backgroundColor: pinkBackgroundColor,
body: SafeArea(
@ -136,7 +138,7 @@ class _HomePageState extends State<HomePage> {
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginPage()), //Replace Container() with call to Map-page.
MaterialPageRoute(builder: (context) => CreateAccountPage()), //Replace Container() with call to Map-page.
);
},
child: const Text('Create account',