簡體   English   中英

Laravel Auth :: attempt()始終將我重定向到登錄頁面

[英]Laravel Auth::attempt() redirect me always to login page

我正在嘗試使用Laravel Auth::attempt()函數,但是即使我使用valide用戶名和password ,它也總是使我重定向到登錄頁面。
這是我的路線:

 Route::get('/login', 'PublicController@loginPage');
    Route::post('/login', 'PublicController@loginAction');

    Route::get('/users/members', array('before' => 'auth', function()
    {
        return View::make('users.members');
    })); 

這是我的loginAction函數:

public function loginAction() {
        $rules = array( 'username' => 'required',
                        'password' => 'required'
                        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
             $messages = $validator->messages();
            return Redirect::to('login')
                                ->withErrors($validator)
                                ->withInput(Input::except('password')); 
        }
    else {
        $user = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );
        $auth = Auth::attempt($user);

        if ($auth) {
            dd(Auth::check()); // this return always **True**
            return Redirect::to('users/members');
        } else {
            return Redirect::to('login')
                                ->withErrors(['Login Failed please try again']);}
        }

    }

當我使用錯誤的usernamepassword ,會顯示錯誤消息“ Login Failed please try again ;當我使用有效數據時,它將rederict me to the Login page without showing any error

更新路由過濾器:

Route::filter('auth', function()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        else
        {
            return Redirect::guest('login');
        }
    }
});

更新2:這是我的個人資料頁面(我要在登錄后重定向到該頁面):

Route::get('/users/members', function()
{
    dd(Auth::check()); This always return **false**
});

我認為問題在於登錄后未保存用戶身份驗證。

深入研究之后,我發現問題是我在數據庫中為主鍵使用了不同的名稱user_id而不是id 所以簡單地我將這一行添加到了我的USER模型中

class User extends Eloquent {
    protected $primaryKey = 'admin_id';
....}

注意如果您使用其他名稱作為主鍵而不是id,那么不要忘記添加此專有性

protected $ primaryKey ='PrimaryKeyName';

這將解決問題的音調

您能寄給我們您filters.php的相關部分嗎

Route::filter('auth', function()
{
    // Your filter here
 }

我相信其中有些東西會阻止正確的重定向。

為了進行測試,您可以從路線中刪除過濾器。

Route::get('/users/members', function() ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM