使用laravel中的尝试来验证来自自定义防护的电子邮件和密码

时间:2019-05-19 23:15:33

标签: php laravel authentication laravel-5 laravel-5.8

那是我拥有api函数的控制器:

public function loginsub(Request $request)
{ 
   $credentials = $request->only('email', 'password');

    if(Auth::guard('sub_users')->attempt($credentials))
    { 
        $user = Auth::guard('sub_users'); 
    }
}

来自auth.php配置的卫兵和提供者:我已经编辑了帖子并添加了提供者部分

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

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

         'sub_users' => [
            'driver' => 'passport',
            'provider' => 'sub_users'
        ],
    ],

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


    ],

我收到此错误:

  

“消息”:“方法尝试不存在。”,       “状态码”:500

我正在尝试使用子用户模型登录:

<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class sub_users extends Authenticatable
{
    use HasApiTokens, Notifiable;

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

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

我也尝试了check()而不是尝试,但没有用 预先感谢

1 个答案:

答案 0 :(得分:1)

公共功能loginsub(Request $ request) {    $ credentials = $ request-> only('email','password');

if(Auth::guard('users')->attempt($credentials))
{ 
    $user = Auth::guard('users'); 
}

}