Люмен и JWT токен не установлены - PullRequest
0 голосов
/ 11 ноября 2018

Я реализовал версию токена lwmen и 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, я получаю ошибку

токен не предоставлен.

У кого-нибудь есть идеи, как решить эту проблему? Я не уверен, почему токен не хранится.

...