4

从所有教程中,我应该能够对用户进行身份验证,然后跳转到任何其他页面,并且登录是持久的。然而,这不起作用。

自定义编译的 PHP LAMP 堆栈。应用程序存储是可写的。

与教程的唯一区别是我使用的是电子邮件而不是用户名。 http://laravelbook.com/laravel-user-authentication/ http://codehappy.daylerees.com/authentication

会话有效,因为我能够将 var 存储到会话并在不同的页面上读出。

模型/User.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 = '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;
    }

}

配置/auth.php

return array(
    'driver' => 'eloquent',
    'model' => 'User',
    'table' => 'users',

    'reminder' => array(
        'email' => 'emails.auth.reminder',
        'table' => 'password_reminders',
        'expire' => 60,
    ),
);

配置/会话.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,
);

路由.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';
});

表 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, 'ben.test@smitty.com', '$2y$10$591gwvQKSGXihKruH1s.weHNM1DR/xzavW46vUuSBxEF7Jk0zZe1G', 'Ben Dauphinee', '2013-08-03 23:25:01', '2013-08-07 01:32:46'),
(2, 'jim@dandy.com', NULL, 'Jim Dandy', '2013-08-03 23:25:01', NULL);

产生的 Auth::user() 信息

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] => bookworm51@hotmail.com
            [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] => bookworm51@hotmail.com
            [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] => 
)
4

3 回答 3

15

问题是你使用了“userId”作为你的主要 id——但你没有告诉 Laravel。

根据 Laravel 文档“Eloquent 还将假设每个表都有一个名为 id 的主键列”

任何一个

userIdid你的桌子上更改(我个人的建议 - 如果你的桌子总是有 id,它会让你的生活更轻松)

或将其添加到您的 User.php 文件中:

protected $primaryKey = "userId";
于 2013-08-17T15:03:33.250 回答
1

您的模型副本User将非常有用

您能否在User模型上验证以下内容。

  1. 如果User模型实现了 Illuminate\Auth\UserInterface。实现上述接口后,需要如下定义 2 个方法。尽管 Laravel 上没有记录,但Auth驱动程序使用它UserInterface来验证凭据。看看http://laravel.com/api/class-Illuminate.Auth.UserProviderInterface.html

    public function getAuthPassword() { return $this->password; }

    公共函数 getAuthIdentifier() { return $this->getKey(); }

  2. 您能否验证数据库上的密码是否已加密。有很多开发人员在密码列上有一个普通密码的例子。Auth::attempt使用自动对密码进行哈希处理Hash::make(),然后将其与users表中的值进行比较。

希望这可以帮助。

于 2013-08-14T11:42:04.300 回答
0

我很久以前就遇到过这个问题。只需将您的 php 版本更改为低于 5.6 版的任何版本

于 2017-02-16T11:00:26.093 回答