【问题标题】:laravel 5.5 not support jwt auth liblaravel 5.5 不支持 jwt auth lib
【发布时间】:2019-09-10 12:59:21
【问题描述】:

我是 Laravel 的新手,在 laravel 5.5.18 中使用 JWT 身份验证,但它对我不起作用,它在 api 登录时出错 Interface 'Tymon\JWTAuth\Contracts\JWTSubject' not found"

谁能帮我解决一下。

谢谢

【问题讨论】:

  • 我已经在 google 中检查了它的建议我更新 jwt lib,但是当我使用 composer update its hang 时它也不起作用....
  • tymondesigns/jwt-auth的版本是多少?
  • "tymon/jwt-auth": "^1.0.0-beta.3@dev"
  • 确保你有 /vendor/tymon/src/... 然后运行 ​​composer dump-auto - 只是为了确保你有文件并且它们正在自动加载.
  • 您可以使用 laravel/passport 并从应用程序 composer remove vendor/package 中删除包

标签: laravel laravel-5.5 jwt-auth


【解决方案1】:

更新文章和源代码 - www.ultimateakash.com

composer remove tymon/jwt-auth
composer dump-autoload

然后安装

composer require tymon/jwt-auth:dev-develop --prefer-source
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
php artisan jwt:secret
Route::post('login', 'ApiController@login');
Route::post('register', 'ApiController@register');

Route::group(['middleware' => 'auth.jwt'], function () {
    Route::get('logout', 'ApiController@logout');
    Route::get('user', 'ApiController@getAuthUser');

});
?php
 
namespace App;
 
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
 
class User extends Authenticatable implements JWTSubject
{
    use Notifiable;
 
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
 
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
 
    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
 
    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
 
class ApiController extends Controller
{
    public function register(Request $request)
    {
        $user = new User;
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();
 
        return response()->json([
            'success' => true,
            'data' => $user
        ], 200);
    }
 
    public function login(Request $request)
    {
        $input = $request->only('email', 'password');
        $jwt_token = null;
 
        if (!$jwt_token = JWTAuth::attempt($input)) {
            return response()->json([
                'success' => false,
                'message' => 'Invalid Email or Password',
            ], 401);
        }
 
        return response()->json([
            'success' => true,
            'token' => $jwt_token,
        ]);
    }
 
    public function logout(Request $request)
    {
        $this->validate($request, [
            'token' => 'required'
        ]);
 
        try {
            JWTAuth::invalidate($request->token);
 
            return response()->json([
                'success' => true,
                'message' => 'User logged out successfully'
            ]);
        } catch (JWTException $exception) {
            return response()->json([
                'success' => false,
                'message' => 'Sorry, the user cannot be logged out'
            ], 500);
        }
    }
 
    public function getAuthUser(Request $request)
    {
        $this->validate($request, [
            'token' => 'required'
        ]);
 
        $user = JWTAuth::authenticate($request->token);
 
        return response()->json(['user' => $user]);
    }
}

【讨论】:

  • 我已经按照所有这些步骤并在供应商文件夹中获取了库,设置了密钥,但仍然给我接口 'Tymon\JWTAuth\Contracts\JWTSubject' not found" 错误,所以我需要删除旧的一个然后重新开始相同的过程我如何删除它有任何命令或只是手动执行或使用直接作曲家命令请建议我
  • 使用 composer remove tymon/jwt-auth
  • 非常感谢。这不是为了你在这里的帖子,我真的要放弃使用 Laravel。这里还有一个使用 Laravel 5.7 的好例子 - blog.pusher.com/laravel-jwt
【解决方案2】:

你可以考虑使用Laravel Passport

来自 laravel 官方文档: https://laravel.com/docs/5.8/passport

Laravel 使用 Laravel Passport 使 API 身份验证变得轻而易举,它在几分钟内为您的 Laravel 应用程序提供了完整的 OAuth2 服务器实现。 Passport 建立在由 Andy Millington 和 Simon Hamp 维护的 League OAuth2 服务器之上。

确实,由于该库是由 Laravel 的创建者维护的,因此对于 Laravel 来说,它会比其他并发库更加更新和稳定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2018-06-16
    • 1970-01-01
    • 2018-02-27
    • 2019-08-27
    • 2018-10-06
    • 2018-03-30
    相关资源
    最近更新 更多