【问题标题】:Laravel Auth::attemp() always falseLaravel Auth::attempt() 总是假的
【发布时间】:2018-08-03 00:39:51
【问题描述】:

首先,我看过这个主题(以及许多其他主题):

Laravel Auth::attempt() always false?

Laravel 5 Auth:attempt() always returns false

但我一直无法登录我的应用程序。我正在使用 Laravel 5.6,我的表名为 oc_users

DB::table('oc_users')->insert(
[
    'email'     => 'myemail@gmail.com',
    'pwd'       => bcrypt('123456'),
    'active'    => true
]);

我转到文件 Laravel > App > User.php 并更改为:

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'oc_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['email', 'pwd'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['pwd', 'remember_token'];
}

以及执行登录的代码:

public function form_login(Request $request)
{   
    if($request->filled('email') == FALSE || $request->filled('pwd') == FALSE)
        return Redirect::back()->withErrors('Fill the credentials');

    if (Auth::attempt(['email' => $request->email, 'pwd' => $request->pwd]))
        return redirect()->intended('app');

    return Redirect::back()->withErrors('Email or password wrong');       
}

我还去了文件 App > Config > Auth.php 试图弄清楚是否有一个表我需要更改,但它似乎在 Laravel 5.6 中不存在

【问题讨论】:

  • 您能发布您的oc_users 表的架构创建吗?

标签: php laravel


【解决方案1】:

Laravel 现在使用 bcrypt:

class User extends Model {

    public function setPasswordAttribute($value) {
         $this->attributes['password'] = bcrypt($value);
    }

}

【讨论】:

    【解决方案2】:

    根据我之前的经验,auth::attempt() 函数默认检查password 列。

    为了让你的代码工作,你可以试试这个。

    在您的用户模型中添加:

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

    这个函数会覆盖Authenticatable.php中默认的getAuthPassword()函数

    【讨论】:

    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 2016-09-12
    • 2017-05-24
    • 2013-06-11
    • 2015-12-10
    • 2014-01-17
    • 2019-04-14
    相关资源
    最近更新 更多