Customize Laravel 7 series Auth authentication routing
Aug 29, 2020
PHP
Laravel
authentication
#§1.First of all It is an Auth facade that can easily implement the login function with Laravel, but if you tamper with the contents, the hurdle will rise at a stretch.
There is also the difficulty that the directory structure of the Auth body differs depending on the version of Laravel. Even if I look it up, most of the information is for Laravel 5 or 6 systems, and I cannot apply it for 7 systems. As is often the case with frameworks. ..
Therefore, this time I would like to share the information when customizing Auth routing in Laravel 7 series. The version of Laravel is 7.25.0.
#§2. Things to do The scope of this article is the following two points. (1) Customize the redirect destination when logging in (2) Customize the URL of the Login screen
The former is easy to do only by modifying the area around the controller, but the latter is a little difficult to understand because it works deep inside the Vender directory while understanding the mechanism of Auth.
Therefore, it may be better for beginners to finish by (1).
Let’s start with the former.
#§3. Customize the redirect destination when you log in
The page that transitions after logging in from the Auth login form is the URL’/home’, and I think that a dashboard like this will be displayed. We will change this to any page such as the posted article list screen.
##3.1 Confirm redirect processing First, let’s check the redirect processing after login. The middleware that does this redirect is “app/Http/Middleware/RedirectIfAuthenticated.php”.
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
return $next($request);
}
}
Middleware was an interrupt action before and after the controller.
And redirect(RouteServiceProvider::HOME);
in this shows the transition destination after login.
You can directly specify the URL you want to display after login like redirect('/articles');
, but this time, customize the constant RouteServiceProvider::HOME
.
*Note
This constant affects not only login but also the transition destination at the time of user registration, so if you do not want to change that (if you want to change only the redirect destination of login), write the redirect destination URL directly here. May be better.
By the way, there is a redirect process in “app/Http/Controllers/Auth/LoginController.php” as well, and there is also a description of RouteServiceProvider::HOME
, but if you specify a transition destination different from Middleware, write it this time. It seems that the middleware has priority.
##3.2 Change HOME constant
Now, let’s actually change the constant called HOME
.
The constant RouteServiceProvider::HOME
is in “app/Providers/RouteServiceProvider.php”.
class RouteServiceProvider extends ServiceProvider
{
protected $namespace ='App\Http\Controllers';
/**
* The path to the "home" route for your application.
*
* @var string
*/
public const HOME ='/home';
public const HOME ='/home';
in this
public const HOME ='/articles';
It is OK if you change to the URL of the page you want to display like.
*Note
As I mentioned earlier, this HOME constant affects not only login but also screen transitions during user registration.
This completes the work of customizing the redirect at login!
#§4. Customize the login screen URL
Next, let’s look at customizing the URL of the login screen, which is the second scope.
I think that the screen of the Auth login form has the URL “'/login’”. This time, we will change it to an arbitrary URL “'/top’”.
##4.1 Check Auth routing What is the routing of the Auth facade in the first place? To customize the URL, you first need to understand this.
The main description of routing in Laravel is “resources/routes/web.php”, I think if you implement Auth, the following code is automatically generated in web.php.
Auth::routes();
However, I can’t find a concrete URL or action description, and I don’t know how to route it.
Apparently we need to find a method called routes()
in the Auth class.
Looking for this routes()
It is in “vendor/laravel/framework/src/Illuminate/Support/Facades/Auth.php”.
public static function routes(array $options = [])
{
if (! static::$app->providerIsLoaded(UiServiceProvider::class)) {
throw new RuntimeException('In order to use the Auth::routes() method, please install the laravel/ui package.');
}
static::$app->make('router')->auth($options);
}
However, there is no specific routing description here either.
Apparently, the last static method called make('router')->auth($options)
seems to be the real thing, and it seems that you can customize the URL by playing around with this.
##4.2 Change URL To mess with the URL is a method called auth, which is found in “vendor/laravel/ui/src/AuthRouteMethods.php”.
public function auth()
{
return function ($options = []) {
// Login Routes...
if ($options['login'] ?? true) {
$this->get('login','Auth\LoginController@showLoginForm')->name('login');
$this->post('login','Auth\LoginController@login');
}
Finally, I arrived at the description of routing.
It is OK if you change the first argument of get and post methods and the argument of name to 'top'
etc. as follows.
public function auth()
{
return function ($options = []) {
// Login Routes...
if ($options['login'] ?? true) {
$this->get('top','Auth\LoginController@showLoginForm')->name('top');
$this->post('top','Auth\LoginController@login');
}
##4.3 Modify the spillover points
If you change the URL of the login page, you also need to modify the following file as a spillover point. (1) “app/Http/Middleware/Authenticate.php” (2) “resources/views/auth/login.blade.php”
Let’s take turns.
###4.3.1 Fix access restriction redirect First of all, it is correction of “app/Http/Middleware/Authenticate.php” of (1). This is a file that regulates the page to be redirected when accessing the page to which authentication is applied without logging in.
Perhaps the page after login is written as'‘middleware'=>'auth'` in the route of “web.php”, isn’t it?
This means “Please do not bite the auth middleware when accessing this URL”, but the page that bounces (redirects) when you access without logging in is specified in this “Authenticate.php” doing.
Modify the route('login')
in this to the URL changed this time.
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('top');
}
}
}
4.3.2 Modify View
Next, don’t forget to modify the view.
The target is “resources/views/auth/login.blade.php”.
Change the route('login')
of the login form in this to the arbitrary URL route('top')
.
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="login-box card">
<div class="login-header card-header float-left">{{ __('messages.Login') }}</div>
<div class="login-body card-body">
<form method="POST" action="{{ route('login') }}">
route was a function that returns the argument as a URL.
If you have created a link to '/login'
in addition to this, correct the URL of the corresponding part as well.
This completes the routing customization work!
#§5 Finally Until the end Thank you for reading. If you have any advice or suggestions, please tell us.
I would be happy if you could LGTM!