title: User authentication via curl request
body: <p>I've got same login page for every CRM.</p>
  
  <p>These are the steps:</p>
  
  <ol>
  <li>Enter password and login</li>
  <li>Send curl to CRM on crm.dev/api/auth/check with user data, which checks if user exists in CRM</li>
  <li>CRM returns success if user exists, after this authenticates user via Auth::user('user_data_here') and redirects to main page of CRM</li>
  </ol>
  
  <p>My problem is that auth doesn't work. And no user data is stored in session (because of previous problem).</p>
  
  <p>CRM auth code:</p>
  
  <pre><code>public function checkUserExists(Request $request)
  {
      $this-&gt;redirectIfAuthorized();
  
      $user = User::find($request-&gt;uid);
      if ($user) {
          return ['response' =&gt; 'LOGIN_SUCCESS'];
      }
      return ['response' =&gt; 'DB_ERROR'];
  }
  
  public function login(Request $request)
  {
      $this-&gt;validateLogin($request);
      $user = User::find($request-&gt;uid);
      $user-&gt;remember_token = $request-&gt;token;
      if ($user-&gt;update()) {
          Auth::login($user, true);
          return redirect()-&gt;intended($this-&gt;redirectPath());
      } else {
          return redirect($this-&gt;redirectAfterLogout);
      }
  }
  
  public function redirectIfAuthorized()
  {
      if (Auth::check()) {
          return redirect($this-&gt;redirectTo);
      }
  }
  </code></pre>
  
  <p>Api route:</p>
  
  <pre><code>Route::group(['prefix' =&gt; 'auth'], function () {
      Route::post('check', 'LoginController@checkUserExists');
      Route::get('login', 'LoginController@login');
      Route::get('logout', 'LoginController@logout');
  });
  </code></pre>
  
  <p>And login page's logic for this CURL request</p>
  
  <pre><code>private function sendToken($action, $user, $token)
  {
      $query = DB::table('user_base')-&gt;join('bases', 'bases.id', '=', 'user_base.base_id')
                                     -&gt;where('user_base.user_id', $user);
  
      $result = $query-&gt;get();
      foreach ($result as $row) {
          $urlAPI = $row-&gt;url_api;
          if ($urlAPI == 'http://appliance.dev/api/auth') {
              $urlAPI .= '/check';
          }
          $rsp = $this-&gt;curl($urlAPI, array(
              'apiKey' =&gt; $this-&gt;apiKey,
              $action =&gt; true,
              'uid' =&gt; $row-&gt;base_uid,
              'token' =&gt; $token
          ));
      }
  }
  </code></pre>