多重身份验证在 laravel 5.1 中不起作用

multi auth not working in laravel 5.1

我正在使用 Laravel 5.1,我的项目需要多次登录,所以我正在尝试 Kbwebs\MultiAuthSarav\Multiauth 用于多重身份验证,两者都与 user 模型 模型一起使用,但不幸的是,当我使用 TenderApplicant 模型时 Auth ::attempt return 每次都是错误的。

这是我的代码:

app.php

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
    Illuminate\Auth\AuthServiceProvider::class,
    Sarav\Multiauth\MultiauthServiceProvider::class,
    ...

auth.php

return [
'multi' => [
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table' => 'sa_users',
    ],
    'user' => [
        'driver' => 'eloquent',
        'model'  => App\Models\TenderApplicant::class,
        'table'  => 'tender_applicant',
    ],
],
'password' => [
    'email'  => 'emails.password',
    'table'  => 'sa_password_resets',
    'expire' => 60,
],

];

AuthController.php

class AuthController extends Controller
{

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

public function __construct()
{
    $this->middleware('guest', ['except' => 'getLogout']);
}

public function userAuth(Request $request)
{
    $this->validate($request, ['email' => 'required','password' => 'required']);

    $email      = $request->input('email');
    $password   = $request->input('password');
    var_dump(Auth::attempt("user", ['email' => 'awal.ashu@gmail.com', 'password' => '123456']));
}

return 用户错误,但管理员工作完美。

TenderApplicant.php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class TenderApplicant extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table      = 'tender_applicant';
    protected $primaryKey = 'TE_APP_ID';
    const CREATED_AT      = 'CREATED_AT';
    const UPDATED_AT      = 'UPDATED_AT';

    protected $fillable = [
                        'TENDER_ID', 
                        'SCHEDULE_ID', 
                        'PR_DETAIL_ID', 
                        'NID', 
                        'PR_CATEGORY', 
                        'PR_TYPE', 
                        'APPLICANT_NAME',
                        'EMAIL',  
                        'APPLICANT_PHONE', 
                        'PASSWORD', 
                        'HOLDING_NO', 
                        'IS_TE_SC_BOUGHT', 
                        'TE_SC_AMOUNT', 
                        'TE_SC_DATE', 
                        'SPOUSE_NAME', 
                        'BID_AMOUNT',
                        'BG_AMOUNT', 
                        'BG_AMOUNT_TEXT',
                        'BANK_ID',
                        'B_DRAFT_NO', 
                        'B_DRAFT_DATE', 
                        'B_DRAFT_ATTACHMENT', 
                        'IS_SELECTED', 
                        'IS_ACTIVE', 
                        'CREATED_BY', 
                        'UPDATED_BY'
                    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['TE_APP_ID'];

    /**
     * Password need to be all time encrypted.
     *
     * @param string $password
     */
    public function setPasswordAttribute($password)
    {
        $this->attributes['PASSWORD'] = bcrypt($password);
    }
}

默认情况下,Laravel 除非您将密码字段保留为 "password"。如果您更改(您保留为 PASSWORD),您必须通知 Laravel 您的密码字段更改。

请将以下代码添加到您的TenderApplicant.php文件中并检查

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

然后尝试如下访问,

Auth::attempt('admin', ['EMAIL' => 'youremail@gmail.com', 'password' => 'your_password']); 

注意:您不必在此处将 密码 更改为 PASSWORD,因为我们已经在模型中定义了我们的方法。

  1. 当您尝试 Auth::attempt() 时,laravel 将 运行 遵循查询

    SELECT * FROM "your_auth_table" WHERE email = "given_email" LIMIT 1

  2. 然后根据获得的结果,laravel将从获得的结果中提取密码字段,该字段将被哈希。
  3. 然后它将从模型中调用 getAuthPassword() 方法并获取您提供的密码字段。默认情况下它将是 "password"。现在有了 php 功能 password_verify , laravel 将使用用户给定的明文验证您的哈希密码并启动用户会话

更多信息你可以查看这个博客。

http://blog.sarav.co/laravel-password-management-mechanism/