0
0

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 1 year has passed since last update.

laravel ui/auth マルチログイン対応

Posted at

##編集するファイルは以下

・routes/web.php (ルート設定)
・Providers/RouteServiceProvider.php(ログイン後の画面遷移設定)
・config/auth.php(ガード設定)
・Middleware/Authenticate.php(未ログインユーザーをリダイレクト)
・Middleware/RedirectIfAuthenticated.php(ログイン済ユーザーがlogin画面に来たときのリダイレクト)
・モデル(adminモデルとか)
・マイグレーションファイル(passwordリセットの分)

web.php

Auth::routes();

ログイン後の画面遷移先を各アカウントごとに設定する。

Providers/RouteServiceProvider.php

public const HOME = '/';
public const ADMIN_HOME = '/admin';

ガードの設定
ガードとは、
認証の際に、Auth::guard-nameと記述するだけで、どちらのアカウントで認証するかを簡単に書き換えられるっていうだけ。

config/auth.php

    'defaults' => [
        'guard' => 'users',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'users' => [
            'driver' => 'session',
            'provider' => 'users',
        ], 
        // 2021-08-04 19:12:30  S 
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        // 2021-08-04 19:12:30  E

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        // 2021-08-04 19:12:30  S 
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ]
        // 2021-08-04 19:12:30  E
        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],

        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ]
    ],

ログインしていないユーザーが認証付きページにアクセスした際の、リダイレクト先の設定。

Middleware/Authenticate.php

    protected $user_route  = 'login';
    protected $admin_route = 'admin.login';

    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {

            if ($request->is('admin*')) {
                return route($this->admin_route);
            } elseif ($request->is('*')) {
                return route($this->user_route);
            } 

        }
    }

ログインしたくせに、またログイン画面に来たユーザーのリダイレクト先の設定。

Middleware/RedirectIfAuthenticated.php
    private const GUARD_USER = 'users';
    private const GUARD_ADMIN = 'admin';

    public function handle(Request $request, Closure $next, ...$guards)
    {

        if(Auth::guard(self::GUARD_ADMIN)->check() && $request->routeIs('admin.*')){
            return redirect(RouteServiceProvider::ADMIN_HOME);
        }elseif(Auth::guard(self::GUARD_USER)->check() && $request->routeIs('*')){
            return redirect(RouteServiceProvider::HOME);
        }

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?