无法在laravel 5.8中以管理员身份登录

时间:2020-03-28 21:05:12

标签: laravel authentication

我在laravel应用中设置了多重身份验证。我正在尝试使用默认身份验证laravel 5.8创建多个身份验证。我有两个表,一个是用户,另一个是管理员。我已经为管理员配置了警卫。用户登录正常,没有问题,但是当我尝试登录管理员时,即使我使用正确的凭据登录也无法正常工作。如果我使用少于6个字符,则密码字段验证有效。请帮助我解决此问题。enter code here 我的管理员模型是

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable {
    use Notifiable;

    protected $guard = 'admin';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'title',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}


Guard setting is


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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
    ],


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

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

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


    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],



    AdminLoginController is

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;

class AdminLoginController extends Controller {

    public function __construct() {
        $this->middleware('guest:admin')->except('logout');
    }

    public function showLoginForm() {
        return view('auth.admin-login');
    }
    protected function guard() {
        return Auth::guard('admin');
    }

    public function login(Request $request) {

        //validate the form
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:6',
        ]);
        //attemp to login
        if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
            //Authentication passed...
            return 'success';
            //return redirect()
            //->intended(route('admin.dashboardsdsdsd'));

            //if login success then redirect to page
            // if not success then redirect to back

        }

        return redirect()->back()->withInput($request->only('email', 'remember'));
    }
}

路线是

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('admin', 'AdminController@index')->name('admin.dashboard');

Route::get('admin/login', 'Auth\AdminLoginController@showLoginForm')->name('admin.login');
Route::post('admin/login', 'Auth\AdminLoginController@login')->name('admin.login.submit');

请帮助我解决此问题,以便管理员可以登录。

1 个答案:

答案 0 :(得分:2)

有两个主要原因:

第一个:您的哈希密码不正确,因此请打开此website(即MD5哈希生成器),然后输入您想要的任何数字,并接受并创建一个新的管理员直接从数据库登录帐户并粘贴密码,然后尝试

第二个:清除缓存并查看:

php artisan view:clear 
php artisan cache:clear