From all the tutorials, I'm supposed to be able to auth a user then jump to any other page, and the login is persisted. This however, does not work.
Custom compiled PHP LAMP stack. App storage is writable.
The only difference from the tutorials is that I'm using email instead of username. http://laravelbook.com/laravel-user-authentication/ http://codehappy.daylerees.com/authentication
Sessions work, as I was able to store a var to session and read it out on a different page.
models/User.php (stock)
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
echo $this->getKey();
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
config/auth.php
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
config/session.php
return array(
'driver' => 'native',
'lifetime' => 120,
'files' => storage_path().'/sessions',
'connection' => null,
'table' => 'sessions',
'lottery' => array(2, 100),
'cookie' => 'laravel_session',
'path' => '/',
'domain' => null,
);
routes.php
Route::get('/', array('as' => 'home', function(){
return View::make('home');
}));
Route::get('login', array('as' => 'login', function () {
return View::make('login');
}))->before('guest');
Route::post('login', function () {
$user = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($user, true)) {
/*
return Redirect::route('home')
->with('flash_notice', 'You are successfully logged in.');
*/
} else {
/*
// authentication failure! lets go back to the login page
return Redirect::route('login')
->with('flash_error', 'Your email/password combination was incorrect.')
->withInput();
*/
}
// This shows the user as logged in
echo (Auth::check()) ? 'Logged in' : 'Not logged in';
});
// This shows the user as not logged in
Route::get('test', function () {
echo (Auth::check() == true) ? 'Logged in' : 'Not logged in';
});
Table SQL
CREATE TABLE IF NOT EXISTS `users` (
`userId` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(250) DEFAULT NULL,
`password` varchar(124) DEFAULT NULL,
`name` varchar(250) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`userId`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `users` (`userId`, `email`, `password`, `name`, `created_at`, `updated_at`) VALUES
(1, '[email protected]', '$2y$10$591gwvQKSGXihKruH1s.weHNM1DR/xzavW46vUuSBxEF7Jk0zZe1G', 'Ben Dauphinee', '2013-08-03 23:25:01', '2013-08-07 01:32:46'),
(2, '[email protected]', NULL, 'Jim Dandy', '2013-08-03 23:25:01', NULL);
Resulting Auth::user() info
User Object
(
[table:protected] => users
[hidden:protected] => Array
(
[0] => password
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[userId] => 1
[email] => [email protected]
[password] => $2y$10$591gwvQKSGXihKruH1s.weHNM1DR/xzavW46vUuSBxEF7Jk0zZe1G
[name] => Ben Dauphinee
[created_at] => 2013-08-03 20:25:01
[updated_at] => 2013-08-06 22:32:46
)
[original:protected] => Array
(
[userId] => 1
[email] => [email protected]
[password] => $2y$10$591gwvQKSGXihKruH1s.weHNM1DR/xzavW46vUuSBxEF7Jk0zZe1G
[name] => Ben Dauphinee
[created_at] => 2013-08-03 20:25:01
[updated_at] => 2013-08-06 22:32:46
)
[relations:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[touches:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
The problem is you have used "userId" as your primary id - but you have not told Laravel.
Per the Laravel docs: "Eloquent will also assume that each table has a primary key column named id"
Either
Change userId
to id
in your table (my personal recommendation - it'll make your life easier if your table always has id)
or add this to your User.php file:
protected $primaryKey = "userId";
A copy of your User
Model would be very useful
Can you verify the following on the User
Model.
If the User
Model implements Illuminate\Auth\UserInterface. Once you implement the above interface 2 methods needs to be defined as below. Though it is not documented on Laravel, the Auth
driver makes use of this UserInterface
to validate the credentials. Take a look at http://laravel.com/api/class-Illuminate.Auth.UserProviderInterface.html
public function getAuthPassword() { return $this->password; }
public function getAuthIdentifier() { return $this->getKey(); }
Can you verify if the password on the DB is encrypted. There have been instances with many developers that they have a plain password on the password column. Auth::attempt
automatically hashes the password using Hash::make()
and then compares it with the value on the users
table.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With