我正在构建api
以通过我的移动应用程序对我的用户进行身份验证
登录控制器返回正确的token
。
<?php
namespace App\Api\V1\Controllers;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class LoginController extends Controller
{
public function login(LoginRequest $request, JWTAuth $JWTAuth)
{
$credentials = $request->only(['username', 'password']);
try {
$token = $JWTAuth->attempt($credentials);
if(!$token) {
throw new AccessDeniedHttpException();
}
} catch (JWTException $e) {
throw new HttpException(500);
}
return response()
->json([
'status' => 'ok',
'token' => $token
]);
}
}
邮差结果
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHBzOlwvXC9iZXRhZmlsZS5vcmdcL2dpcHNcL3B1YmxpY1wvYXBpXC9hdXRoXC9sb2dpbiIsImlhdCI6MTQ5Mjc4MDI2NiwiZXhwIjoxNDkyNzgzODY2LCJuYmYiOjE0OTI3ODAyNjYsImp0aSI6InZHWkxaNHNqRUlqYW05WTMifQ.g8_-qHsVVvCEj9_BoqDCKJ9QHvm-yqWALsXmxeMK_3c"
}
现在,当我尝试通过令牌获取当前用户时,我收到了签名错误 用户控制器
<?php
namespace App\Api\V1\Controllers;
use JWTAuth;
use App\Record;
use App\Http\Requests;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class RecordController extends Controller
{
use Helpers;
public function store(Request $request) {
//$record = new Record;
//return $this->currentUser();
$currentUser = JWTAuth::parseToken()->authenticate();
return $currentUser;
}
private function currentUser() {
return JWTAuth::parseToken()->authenticate();
}
}
邮差结果
{
"error": {
"message": "Token Signature could not be verified.",
"status_code": 500
}
}
我已尝试通过url domain.com/api/auth?token=token_key
和标头授权承载者令牌_key传递令牌
我在config/jwt.php 'secret' => env('jwt_secret')
内和.env JWT_SECRET=jwt_secret
任何有助于解决此问题的提示?
由于
答案 0 :(得分:0)
尝试生成密钥:
getFiles()
这将用php artisan jwt:secret
之类的内容更新您的.env
文件