Laravel 身份验证尝试失败

问题描述 投票:0回答:1

我一直在尝试使用 2 个表进行登录身份验证,我一整天都在努力寻找问题,这是唯一出现的错误

enter image description here

首先这是我的 config/auth.php 代码

<?php

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

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

        'inspirator' => [
            'driver' => 'session',
            'provider' => 'inpirators',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "database", "eloquent"
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        'inpirators' => [
            'driver' => 'eloquent',
            'model' => App\Models\Inspirator::class,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_reset_tokens',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,

];

这是我的吸气模型

<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;

class Inspirator extends Model
{
    protected $table = 'inspirator';
    protected $primaryKey = 'idInspirator';

    protected $fillable = [
        'NamaInspirator',
        'AvatarInspirator',
        'EmailInspirator',
        'AlamatInspirator',
        'KelaminInspirator',
        'NoWaInspirator',
        'TipeInspirator',
        'KotaInspirator',
        'KodeAffiliate',
        'TotalAffiliate',
        'Password',
    ];

    protected $hidden = [
        'Password',
        'remember_token',  
    ];

    public function getAuthIdentifierName()
    {
        return 'EmailInspirator';
    }

    public function getAuthIdentifier()
    {
        return $this->EmailInspirator;
    }

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

    public function transaksis()
    {
    return $this->hasMany(Transaksi::class, 'idInspirator');
    }
}

形成刀片视图代码

<form action="/login/auth" method="POST" class="login-email">
                @csrf
                {{-- <p class="login-text" style="font-size: 2rem; font-weight: 600;">Login</p> --}}
                <div class="input-group">
                    <input type="email" placeholder="Email" name="EmailInspirator" value="{{ old('EmailInspirator') }}" required>
                </div>
                <div class="input-group">
                    <input type="password" placeholder="Password" name="Password" value="" required>
                </div>
                <div style="color:red">{{ $errors->first('Salah') }} </div>
                <div class="input-group">
                    <button name="submit" class="btn">Login</button>
                </div>
                <p class="login-register-text">Tidak Punya Akun? <a href="/register">Buat Akun</a>.</p>
            </form>

最后这是我的控制器代码

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\Inspirator;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use RealRashid\SweetAlert\Facades\Alert;

class AuthController extends Controller
{

    public function showLoginForm(){
        $title = "Login";
        return view('login',compact('title'));
    }

    public function login(Request $request){
        $this->validate($request, array(
            'EmailInspirator' => 'required',
            'Password' => 'required',
        ));

        if (Auth::guard('inspirator')->attempt(['EmailInspirator' => $request->input('EmailInspirator'),        'Password' => $request->input('Password')], $request->remember)) {
            return redirect('/');
        }
        return redirect()->back()->onlyInput('EmailInspirator')->withErrors(['Salah' => 'Password atau Email salah']);
    }

提前非常感谢您

php laravel authentication controller
1个回答
0
投票

您的 Inspirator 模型类应该像这样扩展

Authenticatable

class Inspirator extends Authenticatable

并且你必须注意哈希问题,所以如果上面的行没有解决你的问题,请尝试更改你的防护内的哈希设置,如下所示:

'inspirator' => [
            'driver' => 'session',
            'provider' => 'admins',
            'hash' => false,
        ],
© www.soinside.com 2019 - 2024. All rights reserved.