"message": "Argument 1 passed to Tymon\\JWTAuth\\JWTGuard::login () must be an instance of Tymon\\JWTAuth\\Contracts\\JWTSubject, instance of App\\User given, called in D:\\phpStudy\\PHPTutorial\\WWW\\financeApi\\vendor\\tymon\\jwt-auth\\src\\JWTGuard.php

"message": "Argument 1 passed to Tymon\JWTAuth\JWTGuard::login() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in D:\phpStudy\PHPTutorial\WWW\financeApi\vendor\tymon\jwt-auth\src\JWTGuard.php on line 127",
登录认证的时候为什么报这个错误。。。。

User模型的代码:
<?php
namespace App\Models;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;
use Spatie\Permission\Traits\HasRoles;

class Users extends Authenticatable implements JWTSubject{

use Notifiable;

public function getJWTIdentifier()
{

    return $this->getKey();
}

public function getJWTCustomClaims()
{

    return [];
}

}

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

@liyu001989 好了,user模型找错了。。auth.php改成这样就好了
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],

5年前 评论
讨论数量: 6
liyu001989

class User extends Authenticatable implements JWTSubject

5年前 评论
liyu001989

class User extends Authenticatable implements JWTSubject

5年前 评论

@liyu001989 谢谢你,为什么我改成了User也一样报错列。
Usermodel代码:
<?php
namespace App\Models;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements JWTSubject
{

use Notifiable;

public function getJWTIdentifier()
{

    return $this->getKey();
}

public function getJWTCustomClaims()
{

    return [];
}

}

auth.php配置:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

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

AuthorizationsController代码:

namespace App\Http\Controllers\Api;

use App\Models\User;
use Symfony\Component\HttpFoundation\Request;
use Dingo\Api\Auth\Auth;
use App\Http\Requests\Api\AuthorizationRequest;

class AuthorizationsController extends Controller
{
public function store(AuthorizationRequest $request)
{
// $username = $request->username;

// filter_var($username, FILTER_VALIDATE_EMAIL) ?
// $credentials['email'] = $username :
// $credentials['phone'] = $username;

    $credentials['password'] = $request->password;
    $credentials['name'] = $request->name;
    if (!$token = \Auth::guard('api')->attempt($credentials)) {
        return $this->response->errorUnauthorized('用户名或密码错误');
    }

    return $this->response->array([
        'access_token' => $token,
        'token_type' => 'Bearer',
        'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60
    ])->setStatusCode(201);
}

}

前面跟着教程都走通了,不知道为什么就是用户名密码登录这里就会报错。。。

5年前 评论

@liyu001989 好了,user模型找错了。。auth.php改成这样就好了
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],

5年前 评论

我这个也报这个错,他为啥用上jwt的auth了 不是用的自带的auth么
Type error: Argument 1 passed to Tymon\JWTAuth\JWTGuard::login() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of Illuminate\Auth\GenericUser given, called in D:\phpstudy\PHPTutorial\WWW\laravel55\vendor\tymon\jwt-auth\src\JWTGuard.php on line 124

4年前 评论
芝麻开门 11个月前

<?php

namespace App\Models;

use Auth; use Spatie\Permission\Traits\HasRoles; use Tymon\JWTAuth\Contracts\JWTSubject; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait; use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract;

class User extends Authenticatable implements MustVerifyEmailContract, JWTSubject { use HasRoles; use MustVerifyEmailTrait; use Traits\ActiveUserHelper; use Traits\LastActivedAtHelper;

use Notifiable {
    notify as protected laravelNotify;
}
public function notify($instance)
{
    // 如果要通知的人是当前用户,就不必通知了!
    if ($this->id == Auth::id()) {
        return;
    }

    // 只有数据库类型通知才需提醒,直接发送 Email 或者其他的都 Pass
    if (method_exists($instance, 'toDatabase')) {
        $this->increment('notification_count');
    }

    $this->laravelNotify($instance);
}

protected $fillable = [
    'name', 'phone', 'email', 'password', 'introduction', 'avatar',
    'weixin_openid', 'weixin_unionid', 'registration_id',
    'weixin_session_key', 'weapp_openid',
];

protected $hidden = [
    'password', 'remember_token', 'weixin_openid', 'weixin_unionid'
];

protected $casts = [
    'email_verified_at' => 'datetime',
];

public function topics()
{
    return $this->hasMany(Topic::class);
}

public function isAuthorOf($model)
{
    return $this->id == $model->user_id;
}

public function replies()
{
    return $this->hasMany(Reply::class);
}

public function markAsRead()
{
    $this->notification_count = 0;
    $this->save();
    $this->unreadNotifications->markAsRead();
}

public function setPasswordAttribute($value)
{
    // 如果值的长度等于 60,即认为是已经做过加密的情况
    if (strlen($value) != 60) {

        // 不等于 60,做密码加密处理
        $value = bcrypt($value);
    }

    $this->attributes['password'] = $value;
}

public function setAvatarAttribute($path)
{
    // 如果不是 `http` 子串开头,那就是从后台上传的,需要补全 URL
    if ( ! \Str::startsWith($path, 'http')) {

        // 拼接完整的 URL
        $path = config('app.url') . "/uploads/images/avatars/$path";
    }

    $this->attributes['avatar'] = $path;
}

public function getJWTIdentifier()
{
    return $this->getKey();
}

public function getJWTCustomClaims()
{
    return [];
}

}

11个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!