0
1

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.

Laravel10 マルチ認証 未ログイン時の遷移先の指定

Posted at

対応したときのソース

app\Http\Middleware\Authenticate.php
<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Facades\Auth;

class Authenticate extends Middleware
{    
    protected function unauthenticated($request, array $guards)
    {
        throw new AuthenticationException(
            'Unauthenticated.', $guards, $this->redirectToByGuards($request, $guards)
            );
    }

    /**
     * Get the path the user should be redirected to when they are not authenticated.
     */
    protected function redirectToByGuards($request, $guards) {
        if (!$request->expectsJson()) {
            foreach ($guards as $guard) {
                if ($guard === 'admin') {
                    // guardごとに設定
                    return route('admin.login');
                } else {
                    // 初期設定と同じ
                    return route('login');
                }
            }
        }
    }
}

参考サイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?