laravel 8 fortify Auth::user() 登录后返回null

时间:2021-03-16 10:55:48

标签: php laravel laravel-fortify

我在 Laravel 8.12 中遇到身份验证问题,我使用默认的 Laravel fortify 1.7.8,我使用正确的凭据登录,应用程序根据需要重定向到仪表板,但中间件身份验证似乎不是工作,还有 Auth::user() 返回 null,

enter image description here

用户模型

 <?php

namespace App\Models;

use App\Models\LoginHistoric;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory;
    use Notifiable;
    use Uuids;
    protected $table = 'staffs';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'staff_identification',
        'full_names',
        'email',
        'password',
        'gender',
        'address',
        'phone',
        'dateOfBirth',
        'role_id',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    } 
}

FortifyServiceProvider

    <?php

namespace App\Providers;

use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Fortify;

class FortifyServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Fortify::createUsersUsing(CreateNewUser::class);
        Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
        Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
        Fortify::resetUserPasswordsUsing(ResetUserPassword::class);

        RateLimiter::for('login', function (Request $request) {
            return Limit::perMinute(5)->by($request->email.$request->ip());
        });

        RateLimiter::for('two-factor', function (Request $request) {
            return Limit::perMinute(5)->by($request->session()->get('login.id'));
        });

        Fortify::loginView(function (){
            return view('auth.login');
        });
        Fortify::requestPasswordResetLinkView(function (){
            return view('auth.forget-password');
        });
        Fortify::resetPasswordView(function ($request){
            return view('auth.rest-password',['request' => $request]);
        });
    }
}

登录刀片

    @extends('layouts.app')

@section('content')
    <section class="hero">
        <div class="hero-body">
            <div class="container">
                <div class="columns is-vcentered   is-centered">
                    <div class="column  is-5-tablet is-4-desktop is-3-widescreen has-text-centered">
                        <img src="{{asset('assets/images/undraw_Login.svg')}}" alt="" srcset="">
                    </div>
                    <div class="column is-6-tablet is-5-desktop is-4-widescreen">
                        <form class="box" method="post" action="{{route('login')}}">
                            @csrf
                            <div class="field has-text-centered">
                                <img src="{{asset('assets/images/BTS_logo.png')}}" width="167">
                            </div>
                            <div class="field">
                                <label class="label">Email</label>
                                <div class="control has-icons-left">
                                    <input class="input @error('email') is-danger @enderror" name="email"   type="email" value="{{ old('email') }}" placeholder="e.g. alexjohnson@gmail.com" required>
                                    <span class="icon is-small is-left">
                                        <i class="fa fa-envelope"></i>
                                      </span>
                                    @error('email')
                                    <span class="invalid-feedback">
                                        {{$message}}
                                    </span>
                                    @enderror
                                </div>
                            </div>
                            <div class="field">
                                <label class="label">Password</label>
                                <div class="control has-icons-left">
                                    <input class="input @error('password') is-danger @enderror" name="password" type="password" placeholder="********" required>
                                    <span class="icon is-small is-left">
                                    <i class="fa fa-lock"></i>
                                  </span>
                                    @error('password')
                                    <span class="invalid-feedback">
                                        {{$message}}
                                    </span>
                                    @enderror
                                </div>
                            </div>
                            <div class="field">
                                <a class="btn-link" href="{{route('password.request')}}">
                                  {{ __('passwords.forgot_password') }}
                                </a>
                            </div>
                            <div class="field  has-text-centered">
                                <button name="submit" class="button is-success">
                                    {{__('auth.login')}}
                                </button>
                            </div>
                            <p>Don't have an account? Please <a href="#" class="is-link">Register</a></p>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

路线

Route::get('/dashboard',function(){
    dd(\Illuminate\Support\Facades\Auth::user());
//    return view('welcome');
});

我一直在寻找一个错误,但我找不到,我需要一些帮助,谢谢

2 个答案:

答案 0 :(得分:1)

要使用 Auth::user(),您需要应用 auth() 中间件

Route::get('/dashboard',function(){
    dd(auth()->user());
//    return view('welcome');
})->middleware('auth');

答案 1 :(得分:-1)

我遇到了一个问题,我的队友修改了 config/auth.php 默认保护是 api

    *'defaults' => [
         'guard' => 'web',
//        'guard' => 'api',
        'passwords' => 'users',
    ],*