流明和JWT令牌未设置

时间:2018-11-11 18:45:02

标签: laravel jwt lumen

我已经实现了lumen和tymon jwt令牌版本tymon / jwt-auth“:” ^1.0@dev

这是我的身份验证控制器

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Tymon\JWTAuth\JWTAuth;

class AuthController extends Controller
{
    /**
     * @var \Tymon\JWTAuth\JWTAuth
     */
    protected $jwt;

    public function __construct(JWTAuth $jwt)
    {
        $this->jwt = $jwt;
    }

    public function postLogin(Request $request) 
    {
        $this->validate($request, [
            'email'    => 'required|email|max:255',
            'password' => 'required',
        ]);

        try {
          if(! $token = $this->jwt->attempt($request->only('email', 'password'))) {
            $response = $this->jsonResponse(null, 'user_not_found', 404, 'fail');
          }
        } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
            $response = $this->jsonResponse(null, 'token_expired', 500, 'fail');

        } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
            $response = $this->jsonResponse(null, 'token_invalid', 500, 'fail');

        } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
            $response = $this->jsonResponse(null, 'token_absent', 500, 'fail');
        }

        $this->jwt->setToken($token);
        $response = $this->jsonResponse(compact('token'), 'logged_in', 200, 'success');


        return $response;
    }


    public function getLogout() 
    {
      try {

        $this->jwt->invalidate();

        $response = $this->jsonResponse(null, 'successfully_logged_out', 200, 'success');
      } catch(\Exception $e) {
          var_dump($e->getMessage());
          $response = $this->jsonResponse(null, 'error_logging_out', 400, 'fail');
      }
      return $response;
    }


    public function getRefresh()
    {
        try {
           $data = $this->jwt->refresh();
           $response = $this->jsonResponse($data, 'refreshed_token', 200, 'success');

        } catch(\Exception $e) {
            var_dump($e->getMessage());
            $response = response()->json(['message' => 'unable_to_refresh_token', 'status' => 400], 400);
        }
        return $response;
    }

当我使用postLogin方法时,一切都很好,并且得到令牌作为响应,但是当我尝试使无效或刷新或getToken时,我得到了错误

  

未提供令牌。

有人知道如何解决此问题吗?我不确定为什么不存储令牌。

0 个答案:

没有答案