'Flutter login page authentication using mysql
I am trying to connect my login page so that it fetches data from my users table in mysql and logs the user in but I am having some problems with my code
Future login() async {
var url = "http://192.168.1.2/xxxxxxxxxxxxx/xxxxxxx.php";
var response = await http.post(Uri.parse(url), body: {
"username": emailController.text,
"password": passController.text,
});
var data = json.decode(response.body);
if (data == "Success") {
Fluttertoast.showToast(
msg: "Login Succesful",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => StageOne(),
),
);
} else {
Fluttertoast.showToast(
msg: "Invalid credentials",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
}
Using this code I am getting this error in the debug console no matter what code or method I use I keep getting this error, but for the web version that one of my co workers has made the api seems to be working alright and one can login and logout without any problems.
E/flutter ( 5135): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 5135): <br />
E/flutter ( 5135): ^
This is my LoginAuth.php file
<?php
/**
* Created by PhpStorm.
* User: Udean
* Date: 12/14/2019
* Time: 10:43 AM
*/
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
// Include confi.php
include_once('../func/Data/DAO.php');
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// Include confi.php
include_once('../func/Data/DAO.php');
$inputJSON = file_get_contents('php://input');
$decode=json_decode($inputJSON);
$username=$decode->username;
$password=$decode->password;
$u = new DAO() ;
$qur = "select * from users where users_email='$username' and users_password='$password' ";
$result = mysqli_query($u->conn, $qur);
$rows = array();
header('Content-type: application/json');
if(mysqli_num_rows($result)==0){
$jsonobj = '{"status":"false","massage":"user not found","data":"null "}';
echo $jsonobj;
} else{
$jsonobj = '{"status":"true","massage":"user found","data":'.json_encode(mysqli_fetch_object($result)).'}';
echo $jsonobj;
}
/* while($r = mysqli_fetch_object($result)) {
$rows[] = $r;
}
$json =$rows;
*/
/* Output header */
?>
Solution 1:[1]
Try below code hope its help to you.
Declare your TextEditingController
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
Your signIn
function:
signIn(String username, password) async {
Map data = {'username': username, 'password': password};
//in above line 'username' and 'password' are the end Points of the API
var body = json.encode(data);
var jsonData;
var response = await http.post(
'your login API URL',
body: body,
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
);
if (response.statusCode == 200) {
jsonData = json.decode(response.body);
if (jsonData != null) {
print('Login Successfull...!');
// Or add path to your home page using await method
}
} else {
print('Wrong Credentials...!');
}
}
Your Button and call your signIn function on button pressed
ElevatedButton(
onPressed: () {
signIn(usernameController.text, passwordController.text);
},
child: Text(
'Sign In',
),
),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ravindra S. Patil |