Search code examples
phplaravelauthenticationcontroller

Laravel Auth Attempt Failed


i been trying to do login Auth with 2 table and i been struggling to find the problem for entire day and this is the only error show up

enter image description here

First this is my config/auth.php code

<?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,

];

and this is for my inspirator models

<?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 blade view code

<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>

and finally this is my Controller code

<?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']);
    }

thank you very much in advance


Solution

  • Your model class for Inspirator should extends Authenticatable like this:

    class Inspirator extends Authenticatable
    

    And you have to pay attention to the hashing issue, so if the above line didn't solve your issue, try to change the hash setting inside your guard like this:

    'inspirator' => [
                'driver' => 'session',
                'provider' => 'admins',
                'hash' => false,
            ],