0

我已经添加了@csrf令牌和表单以及标题中的元标记,但仍然得到相同的错误。当我注册它工作并将我重定向到主页。没关系,但是当我登录时它不会将我重定向到主页,尽管凭据很好而且很好。

登录.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Login') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('login') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>

                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

                                @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col-md-6 offset-md-4">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

                                    <label class="form-check-label" for="remember">
                                        {{ __('Remember Me') }}
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Login') }}
                                </button>

                                @if (Route::has('password.request'))
                                    <a class="btn btn-link" href="{{ route('password.request') }}">
                                        {{ __('Forgot Your Password?') }}
                                    </a>
                                @endif
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

WEB.PHP

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/admin', function(){
    echo "Hello Admin";
})->middleware('auth','admin');

Route::get('/agent', function(){
    echo "Hello Agent";
})->middleware('auth','agent');

Route::get('/customer', function(){
    echo "Hello Customer";
})->middleware('auth','customer');

登录控制器.php

<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller
{


    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;


    public function __construct()
    {
        $this->middleware('guest')->except('logout');
//        $this->middleware('auth');
//        $this->middleware('admin');
    }


    protected function redirectTo( ) {
        if (Auth::check() && Auth::user()->role == 'customer') {
            return redirect('/customer');
        }
        elseif (Auth::check() && Auth::user()->role == 'agent') {
            return redirect('/agent');
        }
        else {
            return redirect('/admin');
        }
    }
}
4

2 回答 2

0
## LoginController.php ##

问题中的上述代码存在版本差异,适用于 laravel 框架 5.6,我已经更新了 laravel,而 laravel 框架 6.15.1 或 6 的代码低于这是唯一的问题。

 protected function redirectTo( ) {

//        dd(Auth::user()->role);

        if (Auth::check() && Auth::user()->role == 'customer') {
            $this->redirectTo = '/customer';
            return $this->redirectTo;
        }
        elseif (Auth::check() && Auth::user()->role == 'agent') {
            $this->redirectTo = '/agent';
            return $this->redirectTo;
        }
        else {
            $this->redirectTo = '/admin';
            return $this->redirectTo;
        }

    }
于 2020-02-18T13:22:59.843 回答
0

尝试使用 清除缓存php artisan cache:clear,这通常对我有用。

于 2020-02-18T10:53:22.117 回答