【问题标题】:laravel auth attempt work only with first recordlaravel auth 尝试仅适用于第一条记录
【发布时间】:2020-03-27 20:55:42
【问题描述】:

我在 laravel 6 中创建了一个多身份验证系统,一个供管理员使用,另一个供访问者使用

所以我做了第二个后卫,名字叫visitor

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

    'visitor' => [
        'driver' => 'session',
        'provider' => 'visitors',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'visitors' => [
        'driver' => 'eloquent',
        'model' => App\Visitor::class,
    ],

],

LoginController.php 是:

<?php

namespace App\Http\Controllers\web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class LoginController extends Controller
{


protected $loginRoute;

public function __construct()
{
    $this->middleware('guest:visitor');
}

public function showForm ()
{
    return view('web.login');
}

public function login(Request $request)
{
    $this->validate($request, [
        'phone' => 'nullable',
        'password' => 'required|min:8'
    ]);

    $credentials = ['phone' => $request->phone, 'password' => $request->password ];
    if(Auth::guard('visitor')->attempt($credentials, $request->remember) ){
        return redirect()->intended(route('visitor.dashboard'));
    }

    return redirect()->back()->withInput($request->only('phone', 'remember'));
}

}

我使用 bcrypt 函数对密码进行哈希处理

问题是,当我尝试登录第一个帐户时,它可以工作并将我重定向到包含所有帐户详细信息的 home 页面,但其余帐户无法正常工作,它不断将我重定向到 login 页面,例如因为我没有登录

这就是 Handler.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Arr;

class Handler extends ExceptionHandler
{
/**
 * A list of the exception types that are not reported.
 *
 * @var array
 */
protected $dontReport = [
    //
];

/**
 * A list of the inputs that are never flashed for validation exceptions.
 *
 * @var array
 */
protected $dontFlash = [
    'password',
    'password_confirmation',
];

/**
 * Report or log an exception.
 *
 * @param  \Exception  $exception
 * @return void
 *
 * @throws \Exception
 */
public function report(Exception $exception)
{
    parent::report($exception);
}

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Symfony\Component\HttpFoundation\Response
 *
 * @throws \Exception
 */
public function render($request, Exception $exception)
{
    return parent::render($request, $exception);
}


/**
 * @param \Illuminate\Http\Request $request
 * @param AuthenticationException $exception
 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    $guard = Arr::get($exception->guards(), 0);

    switch ($guard) {
        case 'visitor':
            $login='visitor.login';
            break;

        default:
            $login='admin.login';
            break;
    }

    return redirect()->guest(route($login));
}
}

还有 RedirectIfAuthenticated.php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    switch ($guard) {

        case 'visitor':
            if (Auth::guard($guard)->check()) {
                return redirect()->route('visitor.dashboard');
            }

        default:
            if (Auth::guard($guard)->check()) {
                return redirect('/admin/classrooms');
            }
            break;
    }

    return $next($request);
}
}

网络保护(为管理员设置)适用于所有帐户,问题出在访客保护

【问题讨论】:

    标签: laravel laravel-authentication custom-authentication


    【解决方案1】:
    • 创建新的 VisitorLoginController 而不是 LoginController
    • 默认用户的登录控制器

    【讨论】:

    • 不,控制器在另一个文件夹中,我正在使用不同的命名空间namespace App\Http\Controllers\web;
    猜你喜欢
    • 2018-09-13
    • 2019-05-22
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    相关资源
    最近更新 更多