Laravel 5.3 Auth:user():以guest身份登录而不是用户

时间:2017-01-05 10:31:47

标签: laravel laravel-5.3

我正在使用laravel 5.3,并使用Hesto / multi-auth软件包。 我的home.blade工作正常,在我的导航栏中它返回当前的登录用户。但每次我创建一个新视图,并将我的导航栏扩展到它,我的导航栏显示“登录注册”而不是用户名,即使我已经登录。

所以,当我在home.blade时,我成功以用户身份登录,但当我导航到另一个视图时,我被读作客人。 作为样本,我试验了welcome.blade。

这是我的一位用户的路线。

Route::group(['prefix' => 'customer', 'middleware' => 'customer'], function () {


Route::model( 'customer' , 'App\Customer' );
  Route::get('/login', 'CustomerAuth\LoginController@showLoginForm');
  Route::post('/login', 'CustomerAuth\LoginController@login');
  Route::post('/logout', 'CustomerAuth\LoginController@logout');

    Route::get('registration_form', function()
    {
        return view('customer/auth/registration_form');
    });
    Route::post("register_customer", 'CustomerAuth\RegisterController@register');

  Route::post('/password/email', 

'CustomerAuth\ForgotPasswordController@sendResetLinkEmail');
      Route::post('/password/reset', 'CustomerAuth\ResetPasswordController@reset');
      Route::get('/password/reset', 'CustomerAuth\ForgotPasswordController@showLinkRequestForm');
      Route::get('/password/reset/{token}', 'CustomerAuth\ResetPasswordController@showResetForm');
    });

我的kernel.php

 protected $routeMiddleware = [
        'business' => \App\Http\Middleware\RedirectIfNotBusiness::class,
        'business.guest' => \App\Http\Middleware\RedirectIfBusiness::class,
        'customer' => \App\Http\Middleware\RedirectIfNotCustomer::class,
        'customer.guest' => \App\Http\Middleware\RedirectIfCustomer::class,
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];

控制器

公共职能showWelcome(客户$客户)     {          返回视图('welcome'); }

public function showWelcome(Customer $customer)
    {
         return view('welcome');
}


  public function __construct()
    {
        $this->middleware('customer');
    }

     protected function guard()
    {
        return Auth::guard('customer');

这是路线文件夹中的customer.php

Route::get('/home', function () {
    $users[] = Auth::user();
    $users[] = Auth::guard()->user();
    $users[] = Auth::guard('customer')->user();

    //dd($users);

    return view('customer.home');

})->name('home');

导航栏的一部分

 @if (Auth::guest())
                        <li><a href="{{ url('/customer/login') }}">Login</a></li>
                        <li><a href="{{ url('/customer/registration_form') }}">Register</a></li>

                    @else
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                {{ Auth::user()->name }} <span class="caret"></span>
                            </a>

                            <ul class="dropdown-menu" role="menu">
                                <li>
                                    <a href="{{ url('/customer/logout') }}"
                                        onclick="event.preventDefault();
                                                 document.getElementById('logout-form').submit();">
                                        Logout
                                    </a>

                                    <form id="logout-form" action="{{ url('/customer/logout') }}" method="POST" style="display: none;">
                                        {{ csrf_field() }}
                                    </form>
                                </li>
                            </ul>
                        </li>
                    @endif
                </ul> 

2 个答案:

答案 0 :(得分:0)

检查用户是否已登录使用Auth::check()

试试这是你的观点

@if(Auth::check())
    //user loggedIn
    {!! auth()->user()->username !!}
@else
    Guest User
@endif

答案 1 :(得分:0)

由于您使用了hesto / multi-auth软件包并为客户模型生成了auth,因此您需要在if条件下使用这样的guard进行检查。

Auth::guard('customer')->check()

对于您制作的任何身份验证都是一样的,例如Auth::guard('business')->check()

并获取您将使用的特定警卫的用户模型

Auth::guard('customer')->user()

用于访问姓名

Auth::guard('customer')->user()->name