2017-01-05 3 views
1

私はlaravel 5.3を使用しており、Hesto/multi-authパッケージを使用しています。 私のhome.bladeはうまく動いています。ナビゲーションバーに現在のログインユーザーが返されます。しかし、私は新しいビューを作成し、ナビゲーションバーを拡張するたびに、既にログインしているにもかかわらず、ユーザー名の代わりにナビゲーションバーにユーザー名の代わりに "LOGIN REGISTER"が表示されます。Laravel 5.3 Auth:user():ユーザーではなくゲストとしてログインしました

私はhome.bladeユーザーとして正常にログインしましたが、別のビューに移動するとゲストとして読み込まれます。 サンプルとして、私はwelcome.bladeを試しています。

これは私のユーザーのための私のルートです。

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


Route::model('customer' , 'App\Customer'); 
    Route::get('/login', 'CustomerAuth\[email protected]'); 
    Route::post('/login', 'CustomerAuth\[email protected]'); 
    Route::post('/logout', 'CustomerAuth\[email protected]'); 

    Route::get('registration_form', function() 
    { 
     return view('customer/auth/registration_form'); 
    }); 
    Route::post("register_customer", 'CustomerAuth\[email protected]'); 

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

'CustomerAuth\[email protected]'); 
     Route::post('/password/reset', 'CustomerAuth\[email protected]'); 
     Route::get('/password/reset', 'CustomerAuth\[email protected]'); 
     Route::get('/password/reset/{token}', 'CustomerAuth\[email protected]'); 
    }); 

マイ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(顧客の$顧客) { リターンビュー( '歓迎'); }

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


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

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

これは、ルートフォルダ

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

    //dd($users); 

    return view('customer.home'); 

})->name('home'); 

でcustomer.phpユーザーが使用Auth::check()をLOGGEDINているかどうかを確認するにはナビゲーションバー

@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> 

答えて

0

の一部です。

はあなたがhesto /マルチ認証パッケージを使用して、顧客のモデルのための認証を生成しているので、これはあなたのビュー

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

何も表示されません – Ylla

+0

ユーザーテーブルのユーザー名列名は何ですか? –

+0

私はelse部分を追加しました。ゲストユーザーとして返されます。私は{!!を使用していますauth() - > user() - > name !!} – Ylla

0

で試してみてください、あなたはあなたの場合の状態で、このようなガードを使用してこれをチェックする必要があります。それはあなたが作る任意の認証のために同じです

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

、たとえばAuth::guard('business')->check()

、あなたが名前

Auth::guard('customer')->user()->name 
にアクセスするための

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

を使用しますその特定のガードのユーザモデルを取得します

関連する問題