gpt4 book ai didi

laravel 5.5 不支持 jwt 授权库

转载 作者:行者123 更新时间:2023-12-05 09:13:39 26 4
gpt4 key购买 nike

我是 Laravel 的新手,在 Laravel 5.5.18 中使用 JWT 身份验证,但它对我不起作用它在 api 登录中给出错误未找到接口(interface)“Tymon\JWTAuth\Contracts\JWTSubject”

谁能帮我解决一下。

谢谢

最佳答案

更新文章和源代码 - 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]);
}
}

关于laravel 5.5 不支持 jwt 授权库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55775677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com