5 Laravel E-Commerce Application Development - Backend Admin Authentication - LaraShout
5 Laravel E-Commerce Application Development - Backend Admin Authentication - LaraShout
TABLE OF CONTENT
https://www.larashout.com/backend-admin-authentication 1/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
09 Conclusion
https://www.larashout.com/backend-admin-authentication 2/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
https://www.larashout.com/backend-admin-authentication 3/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
As you can see in this array Laravel de ning the two different
guards one for web application and on for the api. We will add
our own guards for admin like below. Copy the below code and
replace with the default guards array.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
'hash' => false,
https://www.larashout.com/backend-admin-authentication 4/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
],
],
As you can see we have added the admin and admin-api guard so
our application can use two different types of model for
authentication purpose. In admin array we are de ning session as
a authentication driver which will use the admins provider and
same for the admin-api array, basically we just copied the
default guards and changed the values for them.
The next array element is the providers, which looks like below.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
],
'providers' => [
'users' => [
https://www.larashout.com/backend-admin-authentication 5/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
https://www.larashout.com/backend-admin-authentication 6/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
As you can see, for password we have just added the guard
name admins and used the same details as users one, which
means we will use the same password_reset table for the admins
password reset functionality.
That’s it for the guard’s changes, now we will add the login
functionality for the admin users.
Learn More
You can learn more about the Laravel Routing by reading the
Laravel Routing Made Easy post.
require 'admin.php';
https://www.larashout.com/backend-admin-authentication 7/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
In part two of this series, we added the below routes just for
checking the admin views.
Route::view('/admin', 'admin.dashboard.index');
Route::view('/admin/login', 'admin.auth.login');
Route::get('login', 'Admin\LoginController@showLoginForm')->name('admin.log
Route::post('login', 'Admin\LoginController@login')->name('admin.login.post
Route::get('logout', 'Admin\LoginController@logout')->name('admin.logout');
Route::get('/', function () {
return view('admin.dashboard.index');
});
});
send the post request for authentication. Next one is the logout
route (/admin/logout) which will be again a GET request.
The last one is the dashboard route which will map to /admin
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
https://www.larashout.com/backend-admin-authentication 9/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
/**
* Where to redirect admins after login.
*
* @var string
*/
protected $redirectTo = '/admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest:admin')->except('logout');
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showLoginForm()
{
return view('admin.auth.login');
}
}
https://www.larashout.com/backend-admin-authentication 10/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
use AuthenticatesUsers;
/**
* Where to redirect admins after login.
*
* @var string
*/
protected $redirectTo = '/admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
https://www.larashout.com/backend-admin-authentication 11/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
$this->middleware('guest:admin')->except('logout');
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showLoginForm()
{
return view('admin.auth.login');
}
folder.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
https://www.larashout.com/backend-admin-authentication 12/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
https://www.larashout.com/backend-admin-authentication 13/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
</div>
</div>
<div class="form-group btn-container">
<button class="btn btn-primary btn-block" type="submit"><i clas
</div>
</form>
</div>
</section>
<script src="{{ asset('backend/js/jquery-3.2.1.min.js') }}"></script>
<script src="{{ asset('backend/js/popper.min.js') }}"></script>
<script src="{{ asset('backend/js/bootstrap.min.js') }}"></script>
<script src="{{ asset('backend/js/main.js') }}"></script>
<script src="{{ asset('backend/js/plugins/pace.min.js') }}"></script>
</body>
</html>
Now if you visit /admin/login URL, you will be presented with the
login form something like below.
https://www.larashout.com/backend-admin-authentication 14/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Validation\ValidationException
*/
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
https://www.larashout.com/backend-admin-authentication 15/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
]);
if (Auth::guard('admin')->attempt([
'email' => $request->email,
'password' => $request->password
], $request->get('remember'))) {
return redirect()->intended(route('admin.dashboard'));
}
return back()->withInput($request->only('email', 'remember'));
}
use Auth;
In the login method, we are rstly validating the user input for
email and password. Then we are using the Auth:: facade to
de ne our admin guard and then making the attempt to
authenticate the admin user.
At this point, you can load the login page and test the login
page, but I recommend that you complete the full post and
then try it.
/**
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function logout(Request $request)
{
Auth::guard('admin')->logout();
$request->session()->invalidate();
return redirect()->route('admin.login');
}
to:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
switch($guard){
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect('/admin');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/');
https://www.larashout.com/backend-admin-authentication 18/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
}
break;
}
return $next($request);
}
/**
* @param \Illuminate\Http\Request $request
* @param AuthenticationException $exception
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Sym
*/
protected function unauthenticated($request, AuthenticationException $exception
{
https://www.larashout.com/backend-admin-authentication 19/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
if ($request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()], 401);
}
$guard = array_get($exception->guards(), 0);
switch($guard){
case 'admin':
$login = 'admin.login';
break;
default:
$login = 'login';
break;
}
return redirect()->guest(route($login));
}
In the above method, we are getting the current guard from the
AuthenticationException class and running the switch
statement on it. Based on the guard value we are redirecting
the user to the right login page.
use Illuminate\Auth\AuthenticationException;
https://www.larashout.com/backend-admin-authentication 20/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
Route::get('/', function () {
return view('admin.dashboard.index');
})->name('admin.dashboard');
});
Testing
Now you can go ahead and try to login the admin using
email [email protected] and password is password . Try to load the
login page when admin is logged in, you will be redirected to
dashboard view. Also try to load the dashboard page
without login as admin and you will be redirected to the
login page.
Conclusion
In this part, we have successfully created the login functionality
for admin users. In the next part of this series, we will start
adding some core functionalities which we will be using
https://www.larashout.com/backend-admin-authentication 21/46
2/13/2020 Laravel E-Commerce Application Development – Backend Admin Authentication | LaraShout
Code Repository
Boris says:
Hi,
Best regards
REPLY
https://www.larashout.com/backend-admin-authentication 22/46