Laravel Custom Authentication by Using Different Table

Web Developer Kaustubh
3 min readDec 1, 2019

For Laravel custom login, we have to follow following steps.

1]Configuration:-

A]Providers:- This defines how the users are actually retrieved out of your database or other storage mechanisms.

Open config/auth.php file and create new providers in providers array.

‘AccountUser => [
‘driver’ => ‘eloquent’,
‘model’ => App\Models\AccountUser::class,
],

This means we are creating a new AccountUser provider using eloquent driver and the model used for the eloquent driver is App\Models\ AccountUser::class.

B]Guards :- Guards define how users are authenticated for each request.
add the AccountUser guard in the guard key in config/auth.php

‘AccountUser => [
‘driver’ => ‘session’,
‘provider’ => ‘AccountUser,
],

This means, we are creating new ` AccountUser ` guard which use`session` driver and using user provider called ` AccountUser `, which we created before

2]Create a model for your authentication table.

Command:-

php artisan make:model Model/AccountUser
AccountUser Model

Illuminate\Foundation\Auth\User is class that responsible to handle
Laravel authentication So we just need to extend our to get all the auth related methods.
I have override getAuthPassword() method and define password column as the password for hash password.

3]Create Routes

routes.php

4] Controller for Home page

HomeController

To restrict only account user can access to this HomeController, I’ve added in the constructor auth: account middleware — which means only authenticated user with account guard can access to this controller.

5]Create a login controller

<?phpnamespace App\Http\Controllers\expense;use App\AccountUser;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\Auth\LoginController as BaseLoginController;
use Illuminate\Support\Facades\Auth;
class LoginController extends BaseLoginController
{
protected $redirectTo = ‘expanse/home’;
public function __construct()
{
$this->middleware(‘guest:account’)->except([‘showLoginForm’,’myLogout’]);
}
public function showLoginForm()
{
return view(‘expanse.auth.login.login’);
}
public function username()
{
return ‘username’;
}
protected function guard()
{
return Auth::guard(‘account’);
}
public function myLogout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();return $this->loggedOut($request) ?: redirect(‘expanse/login’);}
}

In LoginController, I have added middleware guest: account, which it will redirect the user to the homepage if logged in as user with account guard, else will redirect back to the welcome page.
Then I have showLoginForm() overwrite, which to display Account User login .
The username() method get overwritten to because we want username column as user name rather
Then default email id

I do overwrite the guard() method, which should return account guard

6]Redirection:-

$this->middleware(‘guest:account’)->except([‘logout’,’login’]);

May redirect to home page to avoid it, change in RedirectIfAuthenticated
Middleware to

if (Auth::guard($guard)->check()) {
return redirect(‘expanse/home’);
}

After implementing this your functionality will be completed

--

--

Web Developer Kaustubh

Sr.Web developer. love to code. PHP , Javascript, Mysql, Laravel, Jquey, git, project planing,Linux