How to authenticate the user base on the response of an API call?

Description:

I have been using Laravel for a bunch of project now.
Implementing User Authentication is simple in Laravel.

I’ve done that here :

// Posting the sign-in information and check it with the database
    public function postSignIn(){


        $validator = Validator::make(Input::only('username', 'password') , array(
          'username' => 'required',
          'password' => 'required'
        ));


        if ($validator->fails()) {
          return Redirect::to('/')->with('error', 'Please fill out your information ')->withErrors($validator)->withInput();
        }
        $username = strtolower(Input::get('username'));
        $password = Input::get('password');


        $auth = Auth::attempt(array(
          'username' => $username,
          'password' => $password
        ));

        if ($auth) {
          return Redirect::to('/dashboard')->with('success', 'Hi '. $username .' ! You have been successfully logged in.');
        }
        else {
          return Redirect::to('/')->with('error', 'Username/Password Wrong')->withInput(Request::except('password'))->with('username', $username)->withErrors($validator);
        }
      }

Problem:

Now, the structure that I am dealing with is a little different – I don’t have a database or a users table locally.

I have to make an API call to query what I need.


I have a log-in form. As soon as a user submit to that form. I make an API call to

http://www.mysite.com/api/sign-in

if the response showing that user is exist then I want to allow them in.


Can I still use Laravel to achieve the log-in feature ?
Can someone shed some lights on this ?


I’ve tried

public function postSignIn(){

    $username     = strtolower(Input::get('username'));
    $password_api = VSE::user('password',$username);
    $password     = Input::get('password');


    if ( $password == $password_api ) {
        //Log user in
        $auth = Auth::attempt();
    }

    if ($auth) {
      return Redirect::to('/dashboard')->with('success', 'Hi '. $username .' ! You have been successfully logged in.');
    }
    else {
      return Redirect::to('/')->with('error', 'Username/Password Wrong')->withInput(Request::except('password'))->with('username', $username);
    }
  }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s