【问题标题】:Can't authenticate with different table无法使用不同的表进行身份验证
【发布时间】:2013-06-28 11:49:47
【问题描述】:

我更改了auth.php 文件,以便根据authors 表对我的用户进行身份验证。但是当我运行 test 路由时,我不断收到 No account for you

auth.php

<?php

return array(

    'driver' => 'eloquent',

    'model' => 'Author',

    'table' => 'authors',

    'reminder' => array(

        'email' => 'emails.auth.reminder', 'table' => 'password_reminders',

    ),

);

routes.php

Route::get('test', function() {
    $credentials = array('username' => 'giannis',
        'password' => Hash::make('giannis'));
    if (Auth::attempt($credentials)) {
        return "You are a user.";
    }
    return "No account for you";
});

AuthorsTableSeeder.php

<?php

class AuthorsTableSeeder extends Seeder {

    public function run()
    {
        // Uncomment the below to wipe the table clean before populating
      DB::table('authors')->delete();

      $authors = array(
         [ 
         'username' => 'giannis',
         'password' => Hash::make('giannis'),
         'name' => 'giannis',
         'lastname' => 'christofakis'],
         [
         'username' => 'antonis',
         'password' => Hash::make('antonis'),
         'name' => 'antonis',
         'lastname' => 'antonopoulos']
         );

        // Uncomment the below to run the seeder
      DB::table('authors')->insert($authors);
  }

}

附录


我在另一篇文章中看到您必须实现 UserInterface RemindableInterface 接口。但结果是一样的。

作者.php

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Author extends Eloquent implements UserInterface, RemindableInterface {

    protected $guarded = array();

    public static $rules = array();

    public function posts() {
        return $this->hasMany('Post');
    }

    /**
         * 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 "giannis@hotmail.com";
        }
    }

【问题讨论】:

  • 您是否创建了 Author 模型?
  • @TryingTobemyselfRahul 我已经在上面发布了。

标签: authentication laravel laravel-4


【解决方案1】:

当您使用 Auth::attempt(); 时,您不需要对密码进行哈希处理,因此请从路由中删除 Hash::make

Route::get('test', function() {
$credentials = array('username' => 'giannis',
    'password' => 'giannis');
if (Auth::attempt($credentials)) {
    return "You are a user.";
}
return "No account for you";

});

它会像魅力一样发挥作用!

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2013-05-30
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2016-01-11
    • 2016-10-11
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多