LARAVEL5 Custom Login

I am working on an application that requires user login.

I have to follow this flow.

  • User enters login page.
  • The user submits the login page.
  • The application checks if the user is in the database 3.1 (If the user is not in the database, it will send a request to a third party and check if the login was successful) 3.2 If the user is in the database, check the password.

Now I made a class for third party and this code will work like

$third = new Libraries\ThirdParty();
$third->login($username, $password);

      

$third->login

will return true if login succeeded.

Now the question is how to tie this logic together. with laravel predefined functionAuth::check()

+3


source to share


1 answer


When you install laravel it comes with a default input that uses the trait:

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers;

    /**
     * Create a new authentication controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\Registrar  $registrar
     * @return void
     */
    public function __construct(Guard $auth, Registrar $registrar)
    {
        $this->auth = $auth;
        $this->registrar = $registrar;

        $this->middleware('guest', ['except' => 'getLogout']);
    }

}

      

this class uses the login tag stored in: vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php

you can overwrite methods from this class to put your own logic, for example in the class AuthController

you can define a new one:

function postLogin(){
   //your new logic for login
}

      

and it will respect your feature instead of the feature function. anyway, the logic is postLogin

from auth trait

:



public function postLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
        { //this if validate if the user is on the database line 1
            return redirect()->intended($this->redirectPath());
            //this redirect if user is the db line 2
        }

        return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors([
                        'email' => $this->getFailedLoginMessage(),
                    ]);
          //redirect again to login view with some errors line 3
    }

      

you can do two things:

  • edit the tag itself (bad practice) to put your own logic
  • define your own function postLogin

    in AuthController

    and copy the logic, but edit it with your own custom logic.

Edit to be more specific with your points:

  • User enters login page: you can use the default login page that laravel gives you, or you can overwrite the function getLogin

    and redirect to your own view.

  • User submits login page: form action must be: {{ url('/auth/login') }}

    or whatever route you specified inpostLogin()

  • The app checks if the user is in the database: on line 1 of code

    3.1 (If the user is not in the database, it will send a request to a third party and check if the login succeeded): in code line 3

3.2 If the user is in the database, check the password: in code line 2

+3


source







All Articles