1

Am aware of this question Laravel 5.2 Authentication via API but it does not answer my question at all.

I'm new to Laravel framework and my question might be absolutely naive, please forgive me.
From what I understand, In a typical Laravel 5.2 authentication, a database should be created to contain the registered user details, then when a user attempts to login, his details will be compared to what is available in the database before he is allowed accesses to the app then his details are saved in a session to be used as required on other views.

Therefore my question is, How do I do the same authentication when the user's details (password and email) are obtained from an API response? Here is what I've done so far.

AT FRONT END:
When the user inputs his details, I used jquery and ajax to send these details to a function in the controller like this

$.ajax({
    url:baseUrl+'/getAPI',
    method: 'POST',
    dataType: 'json',
    data: {
        email: $("input#email").val(),
        password: $("input#password").val(),
    },
    success:function(response){
        //redirect to home page
        var response = JSON.parse(response);
        if(response.status == "ok")
          window.location.href = '/home';
        else 
          window.location.href = '/login';
    },
    error: function(response){
        $('body').html("Something went wrong!");
    }
});


My routes.php looks like this

Route::get('home', 'CharmeController@home');
Route::post('getAPI', "CharmeController@getAPI");

Route::group(['middleware' => ['web']], function () {});

My MyController.php looks like this.

function getAPI(Request $req){
  $details = $req->all();
  $client = new GuzzleHttp\Client();
  $url = 'url/to/api';
  $response = $client->request('POST', $url,['form_params' => $details])->getBody();
  $data = json_decode($response, true);
  $status =  array_get($data, 'status');
  if($status == 'error')
    return json_encode('{"status":"error"}');
  else if($status == 'ok'){
    $name =  array_get($data, 'name');
    $password=  array_get($data, 'password');

    //I have no idea how to perform authentication 
    //with the $name and $password details from here.

    return json_encode('{"status":"ok"}');
  }
}



I'm stuck here, I have no idea how to continue. I don't know how to perform authentication with the details I got from the API. Please I'll appreciate an explanatory answer. As I said earlier, am new to the Laravel framework.
NOTE: My question is similar to this but there is no precise answer on it. Thank you in anticipation of your responses.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.