简体   繁体   中英

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 .
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 :

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

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) :

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. So simply I've added this line my USER model

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

protected $primaryKey = 'PrimaryKeyName';

This will solve tones of issues

Could you send us the relevant part of your 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() ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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