If i put wrong email and password...I can still login

Viewed 16

I am getting proper response, but still I can login with wrong email and password

    //var jsonData;

    var jsonResponse = null;

    var bodyDecode = json.encode(data);
    final response = await http.post(
        Uri.parse("https://durotechindustries.com.au/logins.php"),
        body: bodyDecode);

    if (response.statusCode == 200) {
      jsonResponse = jsonDecode(response.body);
      print(jsonResponse);
      if (jsonResponse != null) {
        print("this is ");
        setState(() {
          _isLoading = false;
        });
        Navigator.pushNamed(context, '/home');
        //sharedPreferences.setString("token", jsonResponse['token']);
        /* Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(builder: (BuildContext context) => const MyApp()),
            (Route<dynamic> route) => false);  */
        //}
      }
    } else {
      setState(() {
        _isLoading = false;
      });
      print("Hello");
    }
  }

Here is the full code Please check

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'home.dart';
//import 'dart:html';

void main(List<String> args) {
  runApp(MaterialApp(
    home: const LoginPage(),
    routes: {
      "/home": (context) => const MyApp(),
    },
  ));
}

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  //
  bool _isLoading = false;
  GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
  signIn(
    String email,
    password,
  ) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {
      "email": email,
      "password": password,
    };

    //var jsonData;

    var jsonResponse = null;

    var bodyDecode = json.encode(data);
    final response = await http.post(
        Uri.parse("https://durotechindustries.com.au/logins.php"),
        body: bodyDecode);

    if (response.statusCode == 200) {
      jsonResponse = jsonDecode(response.body);
      print(jsonResponse);
      if (jsonResponse != null) {
        print("this is ");
        setState(() {
          _isLoading = false;
        });
        Navigator.pushNamed(context, '/home');
        //sharedPreferences.setString("token", jsonResponse['token']);
        /* Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(builder: (BuildContext context) => const MyApp()),
            (Route<dynamic> route) => false);  */
        //}
      }
    } else {
      setState(() {
        _isLoading = false;
      });
      print("Hello");
    }
  }

  onTapChange() {
    if (emailController.text == "" || passwordController.text == "") {
      print("this is error");
    } else {
      setState(() {
        _isLoading = true;
      });
      signIn(emailController.text, passwordController.text);
    }
  }

  final TextEditingController emailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/app_bg.jpg"), fit: BoxFit.fill)),
        child: Scaffold(
            backgroundColor: Colors.transparent,
            body: _isLoading
                ? const Center(
                    child: CircularProgressIndicator(),
                  )
                : Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Container(
                        height: 350,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(20),
                          color: Colors.white,
                        ),
                        child: Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              //text field
                              const Text(
                                "Sign In",
                                style: TextStyle(
                                  fontSize: 22,
                                  fontWeight: FontWeight.bold,
                                  fontFamily: "WorkSans",
                                ),
                              ),
                              const SizedBox(height: 7),

                              const Text(
                                "Let's Get Started",
                                style: TextStyle(
                                  fontSize: 15,
                                  fontFamily: "WorkSans",
                                ),
                              ),
                              const SizedBox(height: 20),
                              //
                              // Email field
                              Form(
                                child: Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 25.0),
                                  child: Container(
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(25),
                                      boxShadow: const <BoxShadow>[
                                        BoxShadow(
                                            color: Colors.black12,
                                            blurRadius: 3.0,
                                            offset: Offset(0.30, 0.75))
                                      ],
                                    ),
                                    child: Padding(
                                      padding: const EdgeInsets.only(left: 7),
                                      //
                                      // email
                                      //
                                      child: TextFormField(
                                        controller: emailController,
                                        decoration: const InputDecoration(
                                          prefixIcon: Icon(Icons.mail),
                                          border: InputBorder.none,
                                          hintText: "Email",
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              const SizedBox(height: 20),
                              //
                              // Password field
                              Padding(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 25.0),
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(25),
                                    boxShadow: const <BoxShadow>[
                                      BoxShadow(
                                          color: Colors.black12,
                                          blurRadius: 3.0,
                                          offset: Offset(0.30, 0.75))
                                    ],
                                  ),
                                  child: Padding(
                                    padding: const EdgeInsets.only(left: 7),
                                    //
                                    // password
                                    //
                                    child: TextFormField(
                                      controller: passwordController,
                                      obscureText: true,
                                      decoration: const InputDecoration(
                                        prefixIcon: Icon(Icons.lock),
                                        border: InputBorder.none,
                                        hintText: "Password",
                                      ),
                                    ),
                                  ),
                                ),
                              ),

                              const SizedBox(height: 20),
                              //
                              //
                              // Sing in Button
                              Padding(
                                padding: const EdgeInsets.only(right: 25),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    InkWell(
                                      onTap: onTapChange,
                                      child: Container(
                                        padding: const EdgeInsets.only(
                                            left: 30,
                                            right: 30,
                                            top: 17,
                                            bottom: 17),
                                        decoration: BoxDecoration(
                                          color: Colors.blue,
                                          borderRadius:
                                              BorderRadius.circular(25),
                                        ),
                                        child: const Text(
                                          "Sign In",
                                          style: TextStyle(
                                              fontFamily: "WorkSans",
                                              color: Colors.white),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              const SizedBox(height: 12),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: const [
                                  Text(
                                    "Don't have an account ?",
                                    style: TextStyle(
                                      fontFamily: "WorkSans",
                                    ),
                                  ),
                                  Text(
                                    " SignUp",
                                    style: TextStyle(
                                        fontFamily: "WorkSans",
                                        color: Colors.blue,
                                        fontWeight: FontWeight.bold),
                                  ),
                                ],
                              ),

                              const SizedBox(height: 20),
                            ],
                          ),
                        ),
                      ),
                    ],
                  )),
      ),
    );
  }
}
0 Answers
Related