【问题标题】:Laravel Multiple Authentication throwing false conditionLaravel 多重身份验证抛出错误条件
【发布时间】:2019-06-20 16:01:22
【问题描述】:

我想用不同的表和不同的页面制作登录认证的管理页面和用户页面,我已经默认制作了 2 个模型,管理模型和用户模型。并在 /config/auth.php 中添加 Guard for Admin

这是我的 auth.php

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'admin' => [
      'driver' => 'session',
      'provider' => 'admin',
    ],
    'admin-api' => [
      'driver' => 'token',
      'provider' => 'admin',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
],
'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admin' => [
        'provider' => 'admin',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

这是我的管理员模型

namespace App;

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

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = "admin";
    protected $table = 'admins';

    protected $fillable = [
        'name', 'email', 'jabatan','password'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

这是我在 Controller 中的身份验证逻辑

// Login Auth
public function auth()
{
  if (auth()->check()) {
    return redirect('/pendaftar');
  }
  else {
    if (auth()->attempt([
        'email'     => request('email'),
        'password'  => request('password')
      ]))
    {
      return redirect('/pendaftar');
    }
    else
    {
      return back()->withErrors([
        'message' => Lang::get('auth.failed')
      ]);
    }
   }
 }

这是我的路线:此控制器的列表

| | POST | login-admin | |App\Http\Controllers\AdminController@auth | web  |

问题:

问题我无法使用此管理模式登录,并且总是在auth()->attempt([]) 中抛出错误条件,即使我对电子邮件和密码进行了硬编码。请给我一些建议以及我的代码有什么问题。

注意

我已经使用auth('admin')->attempt([]) 为管理员制作保护规范,但结果还是一样,我无法登录。

我已经将默认的auth config从web更改为admin,结果还是一样。

我已经将保护提供程序驱动程序更改为 database 并在 /config/auth.php 中将表指向表 admins,但没有任何反应。

我已经在 cmd 中使用 php thinker 进行测试以获取数据,并且数据可用但身份验证控制器仍然抛出错误条件。

【问题讨论】:

    标签: php laravel authentication multiple-login


    【解决方案1】:

    试试这个

    添加 -> 使用 Illuminate\Http\Request;

    公共功能授权(请求 $request)

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

    Auth::guard('admin')->attempt($credentials);

    【讨论】:

      【解决方案2】:

      您的config/auth.php 中有一个小拼写错误

      你的守卫名字是“admin”,你的提供者名字和你的表名“admins”一样

      'guards' => [
       ...
      
      'admin' => [
            'driver' => 'session',
            'provider' => 'admins',  // replace 'admin' by 'admins'
          ],
      
      ],
      
      
      'providers' => [
           ...
          'admins' => [
              'driver' => 'eloquent',
              'model' => App\Admin::class,
          ],
      ],
      
       'passwords' => [
              ...
      
              'admins' => [
                  'provider' => 'admins',
                  'table' => 'password_resets',
                  'expire' => 60,
              ],
          ],
      

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 2018-09-26
        • 2018-07-21
        • 2018-10-17
        • 1970-01-01
        • 2016-04-02
        • 2019-05-14
        • 2015-08-21
        相关资源
        最近更新 更多