auth:admin not working

时间:2018-04-12 06:13:32

标签: laravel laravel-5 laravel-5.5 laravel-5.6

我创建了管理员登录系统但是在auth ::尝试它总是返回false 这是我的代码

$credentials= ['email' => $request->get('email'), 'password'=>$request->get('password')]; 

   if (Auth::guard('admin')->attempt($credentials)) {
          return redirect()->intended(route('admin.dashboard'));
   }
管理模型中的

    <?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    protected $guard = 'admin';
    protected $primary_key = 'admin_id';
    protected $table = 'admins';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'admin_firstname', 'admin_lastname', 'email', 'admin_username', 'admin_phone', 'admin_picture', 'admin_gender', 'password',
    ];

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

在auth.php中

'guards' => [
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

auth ::尝试失败不知道为什么它不起作用

其日志记录但在管理中间件中发现了问题     $这 - &GT;中间件( 'AUTH:管理员');

我的auth.php

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

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

],


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

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

degault guard设置为web但在仪表板控制器中我使用的是auth:admin请检查

1 个答案:

答案 0 :(得分:1)

您的代码看起来很好。您需要确保在注册时使用bcrypt()。这样

$password=bcrypt($request['password']);

您需要更改config / auth.php 你的后卫应该是这样的

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

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

和提供者应

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


    ],

和密码

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

这是演示更改您的模型和驱动程序