title: How can I login on Flutter app with Laravel basic authenticaiton using email and password
body: <p>After creating a new Laravel project, what I did was only making authentication using <strong>php artisan make:auth</strong>. I needed to get CSRF token to log in on the Flutter app, so I simple commented out CSRF token in the middleware. </p>
  
  <p>The error I am getting is 302 which is redirection error. I was redirected to '/home'. So, I just simply returned 'User data' when successfully logged in, but I am still getting the same error. There is not enough documentation about Flutter of how to authenticate using email and password if i have web service already deployed on the web hosting company. </p>
  
  <p>If I installed basic authentication on Laravel, how can I authenticate on Flutter? I don't need CSRF token for now for testing purposes. </p>
  
  <p><strong>flutter login code</strong></p>
  
  <pre><code>import 'dart:async';
  import 'dart:convert';
  import 'dart:io';
  import 'package:embedded_mobile/screens/home.dart';
  import 'package:dio/dio.dart';
  import 'package:flutter/material.dart';
  import 'package:embedded_mobile/models/user.dart';
  
  var routes = &lt;String, WidgetBuilder&gt;{
    "/home": (BuildContext context) =&gt; HomeScreen(),
    "/login": (BuildContext context) =&gt; LoginWithRestfulApi(),
  };
  
  class LoginWithRestfulApi extends StatefulWidget {
    @override
    _LoginWithRestfulApiState createState() =&gt; _LoginWithRestfulApiState();
  }
  
  class _LoginWithRestfulApiState extends State&lt;LoginWithRestfulApi&gt;
      with TickerProviderStateMixin {
    static var uri = "http://blackboardembedded.dx.am";
  
    static BaseOptions options = BaseOptions(
        baseUrl: uri,
        responseType: ResponseType.plain,
        connectTimeout: 30000,
        receiveTimeout: 30000,
        followRedirects: false,
        validateStatus: (code) {
          return code &lt; 500;
        });
  
    static Dio dio = Dio(options);
  
    GlobalKey&lt;ScaffoldState&gt; _scaffoldKey = GlobalKey&lt;ScaffoldState&gt;();
  
    TextEditingController _emailController = TextEditingController();
  
    TextEditingController _passwordController = TextEditingController();
  
    bool _isLoading = false;
  
    Future&lt;bool&gt; _onBackPressed() {}
  
    Future&lt;dynamic&gt; _loginUser(String email, String password) async {
      try {
        Options options = Options(
          contentType: ContentType.parse('application/json'),
        );
  
        Response response = await dio.post('/login',
            data: {"email": email, "password": password}, options: options);
  
        if (response.statusCode == 200 ||
            response.statusCode == 201 )  {
          var responseJson = json.decode(response.data);
          return responseJson;
        } else if (response.statusCode == 401) {
          throw Exception("Incorrect Email/Password");
        } else
          throw Exception('Authentication Error');
      } on DioError catch (exception) {
        if (exception == null ||
            exception.toString().contains('SocketException')) {
          throw Exception("Network Error");
        } else if (exception.type == DioErrorType.RECEIVE_TIMEOUT ||
            exception.type == DioErrorType.CONNECT_TIMEOUT) {
          throw Exception(
              "Could'nt connect, please ensure you have a stable network.");
        } else {
          return null;
        }
      }
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
            leading: new IconButton(
              icon: new Icon(Icons.arrow_back, color: Colors.black),
              onPressed: () =&gt; Navigator.of(context).pushNamed('/login'),
            ),
            title: Text("Send Location")),
        body: Center(
          child: Container(
            child: _isLoading
                ? CircularProgressIndicator()
                : Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: &lt;Widget&gt;[
                      Padding(
                        padding: const EdgeInsets.all(20.0),
                        child: TextField(
                          controller: _emailController,
                          decoration: InputDecoration(
                            hintText: 'Email',
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(20.0),
                        child: TextField(
                          controller: _passwordController,
                          decoration: InputDecoration(
                            hintText: 'Password',
                          ),
                        ),
                      ),
                      new SizedBox(
                        width: 200,
                        height: 50,
                        child: RaisedButton(
                          shape: new RoundedRectangleBorder(
                              borderRadius: new BorderRadius.circular(50.0)),
                          child: Text("Login"),
                          color: Colors.amber,
                          onPressed: () async {
                            setState(() =&gt; _isLoading = true);
                            var res = await _loginUser(
                                _emailController.text, _passwordController.text);
                            setState(() =&gt; _isLoading = false);
  
                            JsonUser user = JsonUser.fromJson(res);
                            if (user != null) {
                              Navigator.of(context).push(MaterialPageRoute&lt;Null&gt;(
                                  builder: (BuildContext context) {
                                return new LoginScreen(
                                  user: user,
                                );
                              }));
                            } else {
                              Scaffold.of(context).showSnackBar(SnackBar(
                                  content:
                                      Text("Email or password is incorrect")));
                            }
                          },
                        ),
                      ),
                    ],
                  ),
          ),
        ),
      );
    }
  }
  
  class LoginScreen extends StatelessWidget {
    LoginScreen({@required this.user});
  
    final JsonUser user;
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text("Login Screen")),
        body: Center(
          child: user != null
              ? Text("Logged IN \n \n Email: ${user.email} ")
              : Text("Yore not Logged IN"),
        ),
      );
    }
  }
  </code></pre>