简体   繁体   English

Laravel Auth :: attempt()始终将我重定向到登录页面

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

I'm trying to use Laravel Auth::attempt() function but it's always redecting me to login page, even when I use valide username and password . 我正在尝试使用Laravel Auth::attempt()函数,但是即使我使用valide用户名和password ,它也总是使我重定向到登录页面。
This's my Route : 这是我的路线:

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

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

And this's my loginAction function : 这是我的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']);}
        }

    }

When I use wrong username and password it show me the error message Login Failed please try again , and when I use valide data it rederict me to the Login page without showing any error 当我使用错误的usernamepassword ,会显示错误消息“ Login Failed please try again ;当我使用有效数据时,它将rederict me to the Login page without showing any error

Update The route filter : 更新路由过滤器:

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

Update 2 : This my Profile Page (where I want to be redirected after login) : 更新2:这是我的个人资料页面(我要在登录后重定向到该页面):

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

So the problem, as I think is that the users authentication is not saved after logged in. 我认为问题在于登录后未保存用户身份验证。

After deep digging I've figured that the problem I was using different name for the primary key, user_id instead of id , in my database. 深入研究之后,我发现问题是我在数据库中为主键使用了不同的名称user_id而不是id So simply I've added this line my USER model 所以简单地我将这一行添加到了我的USER模型中

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

Notice If you are using a different name for the primary key rather id then don't forget to add this propriety 注意如果您使用其他名称作为主键而不是id,那么不要忘记添加此专有性

protected $primaryKey = 'PrimaryKeyName'; protected $ primaryKey ='PrimaryKeyName';

This will solve tones of issues 这将解决问题的音调

Could you send us the relevant part of your filters.php 您能寄给我们您filters.php的相关部分吗

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

I believe there is something in there that prevents the proper redirection. 我相信其中有些东西会阻止正确的重定向。

For testing you could remove the filter from your route. 为了进行测试,您可以从路线中删除过滤器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM