title: integrate custom REST api login with another website
body: <p>I've trying to integrate my Laravel PASSPORT API with my another website.</p>
  
  <p>Login route "<a href="https://api.myapi.com/login" rel="nofollow noreferrer">https://api.myapi.com/login</a>"</p>
  
  <p>I created website where I want use API route for login.</p>
  
  <p>I think I've done wrongly it, I dont know how to check it if logged in and use correct way bearer token in header.</p>
  
  <p>Api class where is login function and curl post method</p>
  
  <pre><code>class Api {
  
      private $errors = array(); // array to hold validation errors
      private $data = array(); // array to pass back data
  
      private $cookies;
      private $headers = [];
      private $token;
      private $user_agent;
      private $compression;
      private $proxy;
  
      function __construct($cookies = true, $cookie = "cookie.c", $compression = 'gzip', $proxy = '') {
          $this-&gt;headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
          $this-&gt;headers[] = 'Connection: Keep-Alive';
          $this-&gt;headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
          $this-&gt;token = $this-&gt;checkAuth();
          if ($this-&gt;token !== null) { $this-&gt;headers[] = 'Authorization: Bearer ' . $token; }
          $this-&gt;user_agent = $this-&gt;http_user_agent();
          $this-&gt;compression = $compression;
          $this-&gt;proxy = $proxy;
          $this-&gt;cookies = $cookies;
  
          if ($this-&gt;cookies == true) $this-&gt;cookie($cookie);
      }
  
      //this function is done wrongly..
      public function checkAuth () {
          $auth = json_decode($this-&gt;login());
  
          if ($auth-&gt;success === true) {
              return $auth-&gt;token;
          }
  
          return null;
      }
  
      public function login($vars) {
  
          if (empty($vars['email'])) $this-&gt;errors[$vars['email']] = 'Email field is required.';
  
          if (empty($vars['password'])) $this-&gt;errors[$vars['password']] = 'Password field is required.';
  
          if ( ! empty($this-&gt;errors)) { 
              $this-&gt;data['success'] = false;
              $this-&gt;data['errors']  = $this-&gt;errors;
          } else {
              $email = $vars['email'];
              $password = $vars['password'];
  
              $fields = array(
                  "email" =&gt; $email, 
                  "password" =&gt; $password
              );
  
              $fields_string = http_build_query($fields);
  
              $res = $this-&gt;post("http://kris.dev/apify/public/stylist/login", $fields_string);
              $logPost = json_decode($res);
  
              if (isset($logPost-&gt;error)) {
                  $this-&gt;data['success'] = false;
                  $this-&gt;data['errors']  = $logPost-&gt;error;
  
                  return json_encode($this-&gt;data);
              } elseif (isset($logPost-&gt;token)) {
                  $this-&gt;data['success'] = true;
                  $this-&gt;data['token']  = $logPost-&gt;token;
  
                  return json_encode($this-&gt;data);
              }
          }
          // return all our data to an AJAX call
          return json_encode($this-&gt;data);
      }
  
      private function cookie($cookie_file) {
          if (file_exists($cookie_file)) {
              $this-&gt;cookie_file = $cookie_file;
          } else {
              fopen($cookie_file, 'w') or $this-&gt;error('The cookie file could not be opened. Make sure this directory has the correct permissions');
              $this-&gt;cookie_file = $cookie_file;
              fclose($this-&gt;cookie_file);
          }
      }
  
      public static function http_user_agent() {
          return ((isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : NULL);
      }
  
      public function post($url, $data) {
  
          $process = curl_init($url);
          curl_setopt($process, CURLOPT_HTTPHEADER, $this-&gt;headers);
  
          curl_setopt($process, CURLOPT_HEADER, 1);
          curl_setopt($process, CURLOPT_USERAGENT, $this-&gt;user_agent);
  
          if ($this-&gt;cookies == true) curl_setopt($process, CURLOPT_COOKIEFILE, $this-&gt;cookie_file);
          if ($this-&gt;cookies == true) curl_setopt($process, CURLOPT_COOKIEJAR, $this-&gt;cookie_file);
  
          curl_setopt($process, CURLOPT_ENCODING, $this-&gt;compression);
          curl_setopt($process, CURLOPT_TIMEOUT, 30);
  
          if ($this-&gt;proxy) curl_setopt($process, CURLOPT_PROXY, $this-&gt;proxy);
  
          curl_setopt($process, CURLOPT_POST, 1);
          curl_setopt($process, CURLOPT_POSTFIELDS, $data);
          curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
          $response = curl_exec($process);
  
          $header_size = curl_getinfo($process, CURLINFO_HEADER_SIZE);
          $return = substr($response, $header_size);
          curl_close($process);
  
          return $return;
      }
  </code></pre>
  
  <p>login function what passes form parameters to Api class.</p>
  
  <pre><code>require __DIR__ . 'api.php';
  
  $api = new Api();
  
  if (isset($_POST)) {
      $vars = array("email" =&gt; $_POST['email'], "password" =&gt; $_POST['password']);
      $api-&gt;login($vars);
  }
  </code></pre>
  
  <p>ajax passes form data to login function</p>
  
  <pre><code>$(document).ready(function() {
  
      // process the form
      $('#subButton').click(function(e) {
          // get the form data
          // there are many ways to get this data using jQuery (you can use the class or id also)
          var email = $('#email').val();
          var password = $('#password').val();
  
          // process the form
          $.ajax({
              type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
              url         : 'functions/login.php', // the url where we want to POST
              data: {
                  'email' : email,
                  'password' : password
              },
              dataType:'JSON',
              success: function(data) {
                  console.log(data);
  
                  // here we will handle errors and validation messages
                  if ( ! data.success) {
  
                      // handle errors for email ---------------
                      if (data.errors.email) {
                          $('#email').addClass('has-error'); // add the error class to show red input
                          $('#email').append('&lt;div class="help-block"&gt;' + data.errors.email + '&lt;/div&gt;'); // add the actual error message under our input
                      }
  
                      // handle errors for email ---------------
                      if (data.errors.password) {
                          $('#password').addClass('has-error'); // add the error class to show red input
                          $('#password').append('&lt;div class="help-block"&gt;' + data.errors.password + '&lt;/div&gt;'); // add the actual error message under our input
                      }
  
                  } else {
  
                      // ALL GOOD! just show the success message!
                      $('form').append('&lt;div class="alert alert-success"&gt;' + data.message + '&lt;/div&gt;');
                      $('#token').append('&lt;div class="alert alert-success"&gt;' + data.token + '&lt;/div&gt;');
  
                      // usually after form submission, you'll want to redirect
                      // window.location = '/thank-you'; // redirect a user to another page
                      alert('success'); // for now we'll just alert the user
  
                  }
              }
  
          });
          // stop the form from submitting the normal way and refreshing the page
          e.preventDefault();
      });
  
  });
  </code></pre>
  
  <hr>
  
  <p>Laravel passport api login controller is simple if user email and password correct then  creates accessToken, if password or email is wrong it throws http_code 422 and response text "Email or Password incorrect.</p>