almo
almo

Reputation: 6367

Laravel 4 Auth works but does not stay logged in

In my Laravel 4 App I use the Auth functionality.

I submit my login form to:

$data = array(
'email'     => Input::get('email'),
'password'  => Input::get('password')
);

if (Auth::attempt($data)) {
    echo 'SUCCESS!';
} 

This outputs SUCCESS, hence everything works. But if I next call another method the user is not logged in:

if (Auth::check()) {
echo "logged in";
}
else {
echo "not logged in";
}

This outputs 'not logged in'. I tried to add true to attempt to remember login but it does not make a diference. I have another Laravel App with Auth running that works perfectly.

Any ideas what the reason could be?

Upvotes: 1

Views: 902

Answers (3)

almo
almo

Reputation: 6367

I found the solution. Maybe this can help others:

After calling Auth:attempt you must not echo anything. You have do to a redirect. Does not make sense to me but solved the problem.

Upvotes: 2

Adeel Raza
Adeel Raza

Reputation: 636

1.First Authenticate with :

     if (Auth::attempt($data)) {
  return Redirect::to('admin'); 
}

2.Then place this check to the page you have redirected your user after successful authentication In this case say 'admin' .

 if (Auth::check()) {
    echo "process";
    }
    else {
    return Redirect::to('login');
    }

Upvotes: 2

Adeel Raza
Adeel Raza

Reputation: 636

Event::listen('auth.login', function($user)
{
    echo 'user logged in'; 
});

Place this after Auth::attempt($data) and check.

Upvotes: 0

Related Questions