2017-02-23 70 views
0

我正在使用自定義表進行用戶認證。我可以登錄成功,就像我在AuthController.phpLaravel沒有爲用戶創建認證會話

public function authenticated(Request $request, $user) 
{ 
    dd($user); 
} 

我正在獲取用戶詳細信息。

但是,當我訪問了一些其他的途徑,並當我做了相應的控制器

  • dd(Auth::user())回報null
  • dd(session()->all())回報_token
  • dd(Auth::check())回報false

我使用public_users認證表離子和public_email,做認證public_password領域,所以我有如下改變App\User.php文件:

class User extends Authenticatable 
{ 
    protected $table = 'public_users'; 
    protected $primaryKey = 'public_users_id'; 
    protected $fillable = [ 
     'public_email', 
     'public_password' 
    ]; 
    protected $hidden = [ 
     'public_password' 
    ]; 

    // Override required, Otherwise existing Authentication system will not match credentials 
    public function getAuthPassword() 
    { 
     return $this->public_password; 
    } 
} 

app/Http/Controllers/Auth/AuthController.php我已經添加了下面的代碼

public function loginUsername() 
{ 
    return property_exists($this, 'username') ? $this->username : 'public_email'; 
} 

public function authenticated(Request $request, $user) 
{ 
    dd($user); 
} 

和我config/auth.php具有

'guards' => [ 
    'web' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 

    'api' => [ 
     'driver' => 'token', 
     'provider' => 'users', 
    ], 
], 
'providers' => [ 
    'users' => [ 
     'driver' => 'eloquent', 
     'model' => App\User::class, 
    ] 
] 

我在做什麼錯?我如何創建用戶會話?

任何幫助表示讚賞!

回答

-1

解決!

我從app/Http/Controllers/Auth/AuthController.php中刪除dd($user)這是關閉Request LifeCycle並每次都創建一個新的令牌。我所需要的只是重定向,這是handleUserWasAuthenticated方法中的默認方法/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

protected function authenticated(Request $request, $user) 
{ 
    return redirect()->intended($this->redirectPath()); 
} 
0

嘗試auth()->user()登錄後可見