14
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel 認証のルート定義からログイン/ログアウトのみ残して他を削除する

Last updated at Posted at 2019-03-14

ログイン・ログアウトのみ残して、他の認証系機能を制限したかったので調べてみました。

検証バージョン

Laravel 5.7 5.8

認証スキャフォルド

下記のコマンドを実行するとroutes/web.php に1行、ルート定義を自動で行ってくれるメソッド呼び出しが追加されます。

$ php artisan make:auth
routes/web.php
Auth::routes();

Auth::routes() の実装

vendor/laravel/framework/src/Illuminate/Routing/Router.php
public function auth(array $options = [])
{
    // Authentication Routes...
    $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
    $this->post('login', 'Auth\LoginController@login');
    $this->post('logout', 'Auth\LoginController@logout')->name('logout');

    // Registration Routes...
    if ($options['register'] ?? true) {
        $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
        $this->post('register', 'Auth\RegisterController@register');
    }

    // Password Reset Routes...
    if ($options['reset'] ?? true) {
        $this->resetPassword();
    }

    // Email Verification Routes...
    if ($options['verify'] ?? false) {
        $this->emailVerification();
    }
}

認証に必要なルート定義が書かれています。

引数にオプションを渡せるようです。
register reset verify をそれぞれキーに boolean 値を渡せば良さそうです。

ちなみに $this->resetPassword() $this->emailVerification() の実装は下記の通りでした。

vendor/laravel/framework/src/Illuminate/Routing/Router.php
public function resetPassword()
{
    $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    $this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');
}
vendor/laravel/framework/src/Illuminate/Routing/Router.php
public function emailVerification()
{
    $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
    $this->get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify');
    $this->get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
}

やはりルート定義を行っているだけのようです。

結論

オプションを渡してルート定義を制御しましょう。

routes/web.php
Auth::routes(['register' => false, 'reset' => false, 'verify' => false]);

ルーティングリストからも消えています。

$ php artisan route:list
+--------+-----------+-----------------------------------------------+------------------------------------+---------------------------------------------------------------+------------------------------------------------------+
| Domain | Method    | URI                                           | Name                               | Action                                                        | Middleware                                           |
+--------+-----------+-----------------------------------------------+------------------------------------+---------------------------------------------------------------+------------------------------------------------------+
|        | GET|HEAD  | login                                         | login                              | App\Http\Controllers\Auth\LoginController@showLoginForm       | web,guest                                            |
|        | POST      | login                                         |                                    | App\Http\Controllers\Auth\LoginController@login               | web,guest                                            |
|        | POST      | logout                                        | logout                             | App\Http\Controllers\Auth\LoginController@logout              | web                                                  |
14
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?