Laravel 5 customizing authentication

349 views Asked by At

In Laravel 5 how can you customize the default authentication which comes out of the box? For example I have multiple types of users to authenticate against. Each type of user is defined by a role i.e Jobseeker, Recruiter etc. Each type of user will have a different type of registration form to capture some of the profile details as well. So I have the following tables:

users
roles
role_user
jobseeker_profile
recruiter_profile

The default authcontroller and passwordcontroller in Laravel 5 uses traits for all the authentication methods. How would you go about customizing it - do you guys edit the existing trait files? So for example the getRegister method returns the register view but I would want it to check the route before deciding which view to show.

// default method
public function getRegister()
{
    return view('auth.register');
}

// custom method
public function getRegister()
{
  if (Request::is('jobseeker/register'))
  {
    return view('auth.jobseeker_register');
  }
  elseif (Request::is('recruiter/register'))
  {
    return view('auth.recruiter_register');
  }

}

Similarly the default postLogin method is as follows:

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')))
    {
        return redirect()->intended($this->redirectPath());
    }

    return redirect($this->loginPath())
                ->withInput($request->only('email', 'remember'))
                ->withErrors([
                    'email' => 'These credentials do not match our records.',
                ]);
}

But I would want the method to also check the user roles as follows:

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')))
    {
        if(Auth::user()->role->name == 'recruiter')
        {
           return redirect()->to('/recruiter/dashboard');
        }
        elseif(Auth::user()->role->name == 'jobseeker')
        {
           return redirect()->to('jobseeker/dashboard');
        }

    }

    return redirect($this->loginPath())
                ->withInput($request->only('email', 'remember'))
                ->withErrors([
                    'email' => 'These credentials do not match our records.',
                ]);
}

So my question is how do you go about customizing the existing authentication? Do you guys create a new controller perhaps CustomAuthController, CustomPasswordController and copy all the traits from the default auth controllers into these custom controllers and edit them as appropriate? I'm unable to find any Laravel 5 tutorials on how to acheive this - they all simply talk about the default out of the box authentication. If anyone has done something similar before I would love to hear about how you went about it and which files were edited to wire this custom auth up.

1

There are 1 answers

1
Martin Bean On

You have a couple of options:

  1. Override the methods in the existing auth controller.
  2. Just don’t implement the AuthenticatesAndRegistersUsers trait at all, and implement authentication logic entirely yourself.

With regards to redirect, I’d listen on the auth.login event, check your user’s type there, and then redirect to the specific dashboard there and then.