Merge Loginpage to master #9
@ -4,19 +4,28 @@ import 'package:firebase_auth/firebase_auth.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_applicationdemo/BottomNavPage.dart';
|
import 'package:flutter_applicationdemo/BottomNavPage.dart';
|
||||||
import 'package:flutter_applicationdemo/GoogleSignInProvider.dart';
|
import 'package:flutter_applicationdemo/GoogleSignInProvider.dart';
|
||||||
|
import 'package:flutter_applicationdemo/mysql.dart';
|
||||||
import 'package:google_sign_in/google_sign_in.dart';
|
import 'package:google_sign_in/google_sign_in.dart';
|
||||||
import 'package:flutter_applicationdemo/HomePage.dart';
|
import 'package:flutter_applicationdemo/HomePage.dart';
|
||||||
import 'package:flutter_signin_button/flutter_signin_button.dart';
|
import 'package:flutter_signin_button/flutter_signin_button.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'GoogleSignInProvider.dart';
|
import 'GoogleSignInProvider.dart';
|
||||||
|
import 'signInPage.dart';
|
||||||
|
import 'globals.dart';
|
||||||
|
import 'user.dart';
|
||||||
|
|
||||||
class LoginPage extends StatefulWidget {
|
class CreateAccountPage extends StatefulWidget {
|
||||||
_LoginPageState createState() => _LoginPageState();
|
_CreateAccountPageState createState() => _CreateAccountPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _LoginPageState extends State<LoginPage> {
|
class _CreateAccountPageState extends State<CreateAccountPage> {
|
||||||
Color pinkBackgroundColor = const Color.fromARGB(255, 240, 229, 229);
|
Color pinkBackgroundColor = const Color.fromARGB(255, 240, 229, 229);
|
||||||
Color textColor = const Color.fromARGB(255, 79, 98, 114);
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -64,7 +73,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) =>
|
builder: (context) =>
|
||||||
LoginPage()), //Replace Container() with call to Map-page.
|
CreateAccountPage()), //Replace Container() with call to Map-page.
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
@ -97,17 +106,17 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
|
|
||||||
InputField createUsernameField() {
|
InputField createUsernameField() {
|
||||||
return InputField(
|
return InputField(
|
||||||
text: "Username:", isPassword: false, icon: Icon(Icons.person));
|
text: "Username:", isPassword: false, icon: Icon(Icons.person), controller: userNameController);
|
||||||
}
|
}
|
||||||
|
|
||||||
InputField createEmailField() {
|
InputField createEmailField() {
|
||||||
return InputField(
|
return InputField(
|
||||||
text: "Email:", isPassword: false, icon: Icon(Icons.email));
|
text: "Email:", isPassword: false, icon: Icon(Icons.email), controller: emailController);
|
||||||
}
|
}
|
||||||
|
|
||||||
InputField createPasswordField() {
|
InputField createPasswordField() {
|
||||||
return InputField(
|
return InputField(
|
||||||
text: "Password:", isPassword: true, icon: Icon(Icons.lock));
|
text: "Password:", isPassword: true, icon: Icon(Icons.lock), controller: passwordController);
|
||||||
}
|
}
|
||||||
|
|
||||||
Padding createBackButton() {
|
Padding createBackButton() {
|
||||||
@ -131,7 +140,19 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
|
|
||||||
ElevatedButton createCreateAccountButton() {
|
ElevatedButton createCreateAccountButton() {
|
||||||
return ElevatedButton(
|
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(
|
child: Text(
|
||||||
"Create Account",
|
"Create Account",
|
||||||
style: TextStyle(color: Colors.black, fontWeight: FontWeight.w400),
|
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 {
|
class InputField extends StatelessWidget {
|
||||||
final Icon icon;
|
final Icon icon;
|
||||||
final String text;
|
final String text;
|
||||||
final bool isPassword;
|
final bool isPassword;
|
||||||
|
final TextEditingController controller;
|
||||||
const InputField({
|
const InputField({
|
||||||
Key? key,
|
Key? key,
|
||||||
required this.text,
|
required this.text,
|
||||||
required this.isPassword,
|
required this.isPassword,
|
||||||
required this.icon,
|
required this.icon,
|
||||||
|
required this.controller
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -187,6 +280,7 @@ class InputField extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
child: TextField(
|
child: TextField(
|
||||||
obscureText: isPassword,
|
obscureText: isPassword,
|
||||||
|
controller: controller,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: text,
|
hintText: text,
|
||||||
icon: icon,
|
icon: icon,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'BottomNavPage.dart';
|
import 'BottomNavPage.dart';
|
||||||
import 'package:flutter_applicationdemo/CreateAccountPage.dart';
|
import 'package:flutter_applicationdemo/CreateAccountPage.dart';
|
||||||
|
import 'globals.dart';
|
||||||
import 'signInPage.dart';
|
import 'signInPage.dart';
|
||||||
import 'user.dart';
|
import 'user.dart';
|
||||||
import 'globals.dart' as globals;
|
import 'globals.dart' as globals;
|
||||||
@ -18,6 +19,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
Color textColor = const Color.fromARGB(255, 79, 98, 114);
|
Color textColor = const Color.fromARGB(255, 79, 98, 114);
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
print(LOGGED_IN_USER.userID);
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
backgroundColor: pinkBackgroundColor,
|
backgroundColor: pinkBackgroundColor,
|
||||||
body: SafeArea(
|
body: SafeArea(
|
||||||
@ -136,7 +138,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
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',
|
child: const Text('Create account',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user