簡體   English   中英

Laravel 7 多個身份驗證角色

[英]Laravel 7 multiple auth roles

我正在嘗試為站點創建自定義身份驗證角色,我關注了另一個堆棧溢出帖子,似乎可以讓它工作,但我的總是失敗,我知道信息存在,因為一旦 Auth::guard 失敗,我就會轉儲並死掉信息到屏幕上. 問題是即使信息存在 auth guard 失敗

if (Auth::guard('Admin')->attempt(['Username' => $Request->input('UName'), 'Password' => $Request->input('Password')]))

信息經過驗證和批准並設法通過

 if (Admins::where('Username', $Request->input('UName'))->exists()) {
            $AdminDetails = Admins::Where('Username', $Request->input('UName'))->first();
            if(Hash::check($Request->input('Password'), $AdminDetails->Password)){
                   //user details are correct
            }
  }

我已經使用戶和管理員都可以驗證上面的代碼嘗試授權管理員,我還更改了 config/auth.php 文件,如下所示

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
        'Admin' => [
            'driver' => 'session',
            'provider' => 'Admin',
        ],
        'User' => [
            'driver' => 'session', 
            'provider' => 'User',
        ],
    ],
'providers' => [
        'User' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        'Admin' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admins::class,
        ]

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

模型位於正確的命名空間中,如下所示,管理員可驗證

<?php
namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admins extends Authenticatable
{
    use Notifiable;

    protected $guard = 'Admin';

    protected $fillable = [
        'FName', 'LName', 'Email', 'Password', 'Username'
    ];

    protected $hidden = [
        'password'
    ];
}

並且用戶可驗證

<?php

namespace App\Models;


use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Users extends Authenticatable
{
    use Notifiable;

    protected $guard = 'User';

    protected $fillable = [
        'FullName', 'Email', 'CompanyName','ReferedBy','PhoneNumber','MemberStatus','CustomMessage',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

Laravel 7 不顯示任何錯誤或任何內容,但嘗試登錄用戶的 if 語句失敗

此外,管理員 Model 的遷移是

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAdminsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->id();
            $table->string('Username');
            $table->string('FName');
            $table->string('LName');
            $table->string('Email');
            $table->string('Password');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admins');
    }
}

我知道這是一個問題的大量信息,但我知道它都與問題有關。 感謝您提供的任何幫助。

將憑據數組傳遞給Auth::attempt保存密碼的元素必須命名為password ,它與表上的任何字段名稱都沒有關系。 這就是用戶提供者知道哪個字段應該是“密碼”的方式,因為它需要進行 hash 檢查; 憑據中除password外的所有其他字段都是查詢的 WHERE 條件。

['Username' => ..., 'password' => ...]

如果您的模型/表使用除密碼之外的其他字段作為password ,您將必須通過定義getAuthPassword方法來告訴 model:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM