简体   繁体   English

Laravel 5.8 Auth::guard()->check() 总是返回 false

[英]Laravel 5.8 Auth::guard()->check() always return false

I use Auth:: guard Authentication.我使用 Auth::guard 身份验证。 I entered the correct email and password but it still returns false !!我输入了正确的 email 和密码,但它仍然返回 false ! But Auth::guard($guard)->attempt($dataLogin) returns true .但是Auth::guard($guard)->attempt($dataLogin)返回true I can't find a solution.我找不到解决方案。 here is the login controller file:这是登录 controller 文件:

<?php

namespace App\Http\Controllers\Web;

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

class adminController extends Controller
{
     use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */

    protected $redirectTo = '/home';
    /**
     * Create a new controller instance.
     *
     * @return void
     */

     const ALL_GUARD = ['admin'];

    public function guard()
    {
        return Auth::guard('admin');
    }

    function login(Request $request) 
    {
      $dataLogin = $request->only(['email', 'password']);
        foreach (self::ALL_GUARD as $guard) {
            dd(Auth::guard($guard)->attempt($dataLogin));
            if (Auth::guard($guard)->attempt($dataLogin)) {
                return redirect('/'.$guard.'/');
            }
        }
      return redirect('/login')->with('error', 'error!');
    }

    public function showLoginForm()
    {
        return view('auth.login');
    }

}

here is the guest middleware:这是来宾中间件:

<?php

namespace App\Http\Middleware;

use App\Traits\ResponseTrait;
use Closure;
use Illuminate\Support\Facades\Auth;
class adminLogin
{
    const GUARD_ADMIN = 'admin';
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    use ResponseTrait;

    public function handle($request, Closure $next)
    {
        if (Auth::guard(self::GUARD_ADMIN)->check()) {
            return $next($request);
        }

        return abort(401);
    }
}

Kerenel.php:内核.php:

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'admin' => \App\Http\Middleware\adminLogin::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    ];

What did I do wrong?我做错了什么? help me, thanks!帮帮我,谢谢!

For the Login attempt try Auth::attempt($credentials) instead of the Auth::guard()->attempt()对于登录尝试尝试Auth::attempt($credentials)而不是Auth::guard()->attempt()

Check this out from laravel doc https://laravel.com/docs/8.x/authentication#authenticating-users从 laravel 文档https://laravel.com/docs/8.x/authentication#authenticating-users查看此内容

try this尝试这个

 Auth::guard()->attempt($dataLogin);

or this或这个

 Auth::guard('admin')->attempt($dataLogin)` 
  1. If you wish to use newly created admin guard throughout the application, you can change the value in defaults of config file.如果您希望在整个应用程序中使用新创建的admin防护,您可以更改配置文件的默认值

  1. If it is only about AuthController that uses Laravel's in built Auth system, you can add this line in the AuthController.php and PasswordController.php :如果只是关于AuthController使用 Laravel 内置的 Auth 系统,你可以在AuthController.phpPasswordController.php中添加这一行:
    protected $guard = 'admin';

Ref - Check Guard Customization here参考 -在此处检查Guard 自定义


  1. If you wish to you a guard other than default one for any Auth related task, you can specify it manually like this:如果您希望为任何 Auth 相关任务提供除默认值之外的守卫,您可以像这样手动指定它:
    // For route middleware
    Route::get('something', [
        'middleware' => 'auth:admin',
        'uses' => 'somethigController@show'
    ]);

    // For manually logging the user in
    if (Auth::guard('admin')->attempt($credentials)) {
        // Authenticated...
    }
    
    // To login specific user using eloquent model
    Auth::guard('admin')->login($user);

    // For getting logged in user
    Auth::guard('admin')->user();
    
    // To check if user is logged in
    if (Auth::guard('admin')->check()) {
        // Logged in
    }

Ref - https://laravel.com/docs/5.2/authentication参考 - https://laravel.com/docs/5.2/authentication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM