Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

custom authentication in Laravel 8

I am trying to make Authentication on the Employee table but it always gives me false I do not what should I change to make auth apply on the Employee table.

Is auth->attempt go and check the database or not

This is Employee controller

<?php

namespace AppHttpControllers;

use AppModelsEmployee;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class EmployeeController extends Controller
{
    public function login()
    {
        return view("Employee.login");
    }

    public function check(Request $request)
    {
        $data = $request->validate([
            'email' => 'required|email|max:200',
            'password' => 'required|string',
        ]);

        $emp = $request->only('email', 'password');

        if (Auth::guard('Employee')->attempt($emp) {
            dd($request);
        } else {
            return "error";
        }
    }

I make some change to the auth.php and I add guards of Employee but it also did not work

This is auth.php

<?php

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    'employees' => [
        'driver' => 'eloquent',
        'model' => AppModelsEmployee::class,
    ],

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
        'Employee' => [
            'driver' => 'session',
            'provider' => 'employees',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsUser::class,
        ],
        'employees' => [
            'driver' => 'eloquent',
            'model' => AppModelsEmployee::class,
        ],

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

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
        'employees' => [
            'driver' => 'eloquent',
            'model' => AppModelsEmployee::class,
        ],
    ],

    'password_timeout' => 10800,
];
question from:https://stackoverflow.com/questions/65939659/custom-authentication-in-laravel-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As your controller code and configuration is looking ok,

Your Employee model should be as below:

<?php
namespace AppModels;
use IlluminateFoundationAuthUser as Model; //This import
class Employee extends Model //extend to Model, we are using User as Model.
{
    protected $guarded = ['id'];
    protected $hidden = [
     'employee_password', 'remember_token',
    ];
    public function getAuthPassword()
    {
     return $this->employee_password;
    }
}

Hope this will be helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...