LoginSignup
8
10

More than 5 years have passed since last update.

Laravel5.4でマルチ認証

Last updated at Posted at 2017-03-15

概要

基本的にhttp://dog-ears.net/laravel/160523/ の方の記事の内容でマルチ認証を実装可能です
* adminのテーブル、モデル追加
* config/auth.php にadminを追加
* コントローラ、ビューのAuthをAdminAuthをコピー
* コピーしたファイルの修正

ただし、5.3もしくは5.4の場合Laravel5.2から5.3にかけてauth周りのファイル構成等が変わっているので
追加設定を記載します。

ルーティングの設定

routes/web.phpに以下を追加

app/routes/web.php
$this->get('admin/login', 'AdminAuth\LoginController@showLoginForm')->name('admin.login');
$this->post('admin/login', 'AdminAuth\LoginController@login');
$this->post('admin/logout', 'AdminAuth\LoginController@logout')->name('admin.logout');
// Registration Routes...
$this->get('admin/register', 'AdminAuth\RegisterController@showRegistrationForm')->name('admin.register');
$this->post('admin/register', 'AdminAuth\RegisterController@register');
// Password Reset Routes...
$this->post('admin/password/email', 'AdminAuth\ForgotPasswordController@sendResetLinkEmail')->name('admin.password.email');
$this->get('admin/password/reset', 'AdminAuth\ForgotPasswordController@showLinkRequestForm')->name('admin.password.request');
$this->post('admin/password/reset', 'AdminAuth\ResetPasswordController@reset');
$this->get('admin/password/reset/{token}', 'AdminAuth\ResetPasswordController@showResetForm')->name('admin.password.reset');

コントローラの設定

新規に作ったadmin用のcontrollerの設定はメンバ変数ではなくメソッドのオーバーライドで設定

LoginController.phpに以下のメソッドを追加

app/Http/Controllers/AdminAuth/LoginController.php
    public function showLoginForm()
    {
        return view('adminAuth.login');
    }
    protected function guard()
    {
        return \Auth::guard('admin');
    }
    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();

        return redirect('/admin/login');
    }

RegisterController.phpに以下のメソッドを追加

app/Http/Controllers/AdminAuth/RegisterController.php
    public function showRegistrationForm()
    {
        return view('adminAuth.register');
    }
    protected function guard()
    {
        return \Auth::guard('admin');
    }

ResetPasswordController.phpに以下のメソッドを追加

app/Http/Controllers/AdminAuth/ResetPasswordController.php
    protected function guard()
    {
        return \Auth::guard('admin');
    }
    public function broker()
    {
        return \Password::broker('admins');
    }

ForgotPasswordController.phpに以下のメソッドを追加

app/Http/Controllers/AdminAuth/ForgotPasswordController.php
    public function showLinkRequestForm()
    {
        return view('adminAuth.passwords.email');
    }
    public function broker()
    {
        return \Password::broker('admin');
    }

ミドルウェアの設定

例外ファイルのunauthenticatedメソッドの修正

app/Exceptions/Handler.php
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
+        if(in_array('admin',$exception->guards()))
+            return redirect()->guest('admin/login');

        return redirect()->guest('login');
    }

参考
http://takayukii.me/post/20160914887

8
10
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
8
10