中间件身份验证一直让我注销?编辑:RedirectIfAuthenticated 将我注销?

Middleware auth keep logging me out? Edit: RedirectIfAuthenticated logging me out?

刚接触 Laravel 并尝试制作登录页面。

编辑 2:

尝试使用 Guard,当我被重定向到 RedirectIfAuthenticated 时,似乎 auth()->check() 或 Auth::check() return false 在 handle 函数

这是我的 RedirectIfAuthenticated.php

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  ...$guards
     * @return mixed
     */

    public function handle($request, Closure $next, $guard = null)
    {
        //dd(Auth::check());
        if ($guard == "admin" && Auth::guard($guard)->check()) {
            return redirect('/home');
        }
        if ($guard == "management" && Auth::guard($guard)->check()) {
            return redirect('/home');
        }
        if ($guard == "member" && Auth::guard($guard)->check()) {
            return redirect('/home');
        }
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }
}

Auth 能够存储用户,但是当我去 HomeController 时它一直让我注销,那里有 'auth' 中间件

web.php

Route::get('/login', [LoginController::class, 'login'])->name('login');
Route::post('/check_user', [LoginController::class, 'check_user'])->name('check_user');
Route::get('/home', [HomeController::class, 'index'])->name('home');

一切顺利,使用 dd() 我仍然可以看到 auth()->check() 在登录 LoginController

后仍然 returning 为真

LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\GrantAccess;
// use App\Models\Member;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function login(){
        return view('auth.login');
    }

    public function check_user(Request $request){
        $email = $request->email;
        $password = $request->password;
        $auth = Auth::attempt(['email' => $email, 'password' => $password]);

        if($auth){
            $ga = auth()->user()->access_grant;
            $uga = GrantAccess::where('grant_id', $ga)->get();
            $ugd = $uga[0]->grant_desc;
            $uemail = auth()->user()->email;
            $umcard = auth()->user()->matrix_card;

            $request->session()->put('user_access', $ugd);
            $request->session()->put('user_email', $uemail);
            $request->session()->put('user_matric', $umcard);

            return redirect('home');
        }else{
            return redirect('login');
        }
    }
    use AuthenticatesUsers;
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

重定向时它甚至没有到达 HomeController。所以,我怀疑是因为 $this->middleware('auth');

HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    public function index(Request $r)
    {
        $role = $r->session()->get('user_access');

        if($role == "admin"){
            return redirect('admin');
        } else if($role == "management"){
            return redirect('management');
        } else if($role == "member"){
            return redirect('member');
        } else {
            return redirect('logout');
        }

        return view('home');
    }

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
}

然后 auth()->check() 不知何故 return false here

Authenticate.php(中间件)

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

所以...有什么建议吗?

编辑:

补充一些我之前遇到的问题。所以它可能是连接的?我得到了一个 419 错误,查了一下他们说这主要是因为 csrf,已经在表格后面添加了 @csrf 但没有任何反应

<form class="user" method="POST" action="{{ route('check_user') }}">
@csrf
  <div class="form-group"><input
  class="form-control form-control-user @error('email') is-invalid @enderror" type="email" value="{{ old('email') }}" id="exampleInputEmail" aria-describedby="emailHelp" placeholder="Enter Email Address..." name="email">
    @error('email')
      <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
      </span>
    @enderror
  </div>
  <div class="form-group"><input class="form-control form-control-user @error('password') is-invalid @enderror" type="password" id="exampleInputPassword" placeholder="Password" name="password">
    @error('password')
      <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
      </span>
    @enderror
  </div>
  <button class="btn btn-primary btn-block text-white btn-user" type="submit" data-toggle="modal" data-target="#myModal" style="background: rgb(230,32,43);">Log in</button>
<hr>
</form>

所以我只是在 VerifyCrsfToken 上加上“*”

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
        '*'
    ];
}

你犯的错误很少。首先,Web 是受会话保护的,因此您不需要在会话中存储登录用户。我还可以看到您根据角色将用户重定向到不同的页面。改用守卫。

已经解决了我的“成员”模型中的一个错误,它是主键。 我用 $primary_key 而不是 $primaryKey