Laravel身份验证失败

时间:2014-03-01 23:28:56

标签: php authentication laravel-4

我做错了什么?身份验证有效但不存储会话(但会话正常工作)无论如何,对数据库的连接是正确的,对我来说一切看起来都是正确的,链接但不起作用。

Route::get('login', function(){
    return View::make('user.login');
});


Route::post('login', function(){

    if (Auth::attempt( array('user' => Input::get('user'), 'password' => Input::get('password') ), true )){
        return Redirect::to('home');
    }else{
        return Redirect::to('login')->with('error', 'blah blah');
    }

});




Route::group(array('before' => 'auth'), function()
{

    Route::get('home', function(){
        echo 'welcome ' . '<br />' . PHP_EOL;

        echo 'welcome '. Auth::user()->name . ', your Id is: '.Auth::user()->id ;
    });
});

** * ** ---- ---------------------- ** * ** * *

<?php
//testing session if it's working
if (Session::has ( 'asdf')) {
    Session::put('asdf', Session::get('asdf') + 1);
} else {
    Session::put('asdf', 1);
}

echo Session::get ( 'asdf' );

//testing authentication
if (Auth::check())
{
    echo 'The user is logged in...';
}

?>

@if (Session::has('error'))
<span>{{ Session::get('error') }}</span>
@endif


{{ Form::open(array( 'url' => 'login')) }}

<table>
  <tr>
    <td>{{ Form::label('user', 'User'); }}</td>
    <td>{{ Form::text('user'); }}</td>
  </tr>
  <tr>
    <td>{{ Form::label('password', 'Password'); }}</td>
    <td>{{ Form::password('password'); }}</td>
  </tr>
  <tr>
    <td colspan="2">{{ Form::submit('Go!'); }}</td>
  </tr>
</table>

{{ Form::close() }}

** * ** * - 型号 - * ** * ****

<?php

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 = 'user';

/**
 * 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()
{
    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;
}

}

** * ** ** * **

CREATE TABLE IF NOT EXISTS `user` (
  `id_user` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `created_at` timestamp NOT NULL,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `user` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `name` varchar(30) NOT NULL,
  `last_name` varchar(30) NOT NULL,
  `unread_messages` tinyint(4) NOT NULL,
  `address` varchar(70) NOT NULL,
  `city` varchar(40) NOT NULL,
  `state` char(2) NOT NULL,
  `zip_code` char(5) NOT NULL,
  `social_security` varchar(255) NOT NULL,
  `birth_date` date NOT NULL,
  `salary` float(4,2) NOT NULL,
  `card_number` varchar(16) NOT NULL,
  `lang` enum('en','es') NOT NULL DEFAULT 'en',
  `user_type` enum('admin','user') NOT NULL,
  `status` enum('ON','OFF') NOT NULL,
  `id_branch` tinyint(4) NOT NULL,
  PRIMARY KEY (`id_user`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

1 个答案:

答案 0 :(得分:0)

是否会将您重定向到主页?

echo 'welcome '. Auth::user()->name . ', your Id is: '.Auth::user()->id ;

这输出是什么?


答案:

protected $primaryKey = "id_user";

已添加到用户模型