1

我的 API 有问题。登录后,我有一个令牌,但不幸的是,我无法使用此令牌读取用户的信息。

我的代码拥有令牌(有效):

/**
 * @Rest\View()
 * @Rest\Post("/api/createToken")
 */
 public function createTokenAction(Request $request)
 {
     // reception payload
     $username = $request->request->get('email');
     $password = $request->request->get('password');

     $user = $this->getDoctrine()
         ->getRepository('ApplicationSonataUserBundle:User')
         ->findOneBy(['email' => $username]);

     // check user
     if (!$user) {
        $response = new JsonResponse('User not found');
        $response->setStatusCode(Response::HTTP_NOT_FOUND);
        return $response;
     }

     //check password
    $encoder_service = $this->get('security.encoder_factory');
    $encoder = $encoder_service->getEncoder($user);
    $valid = $encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt());
    if (!$valid) {
        $response = new JsonResponse('User Password is invalid');
        $response->setStatusCode(Response::HTTP_NOT_FOUND);
        return $response;
    }


     // store data inside my token

     $token = $this->get('lexik_jwt_authentication.encoder')
         ->encode([
             'id' => $user->getId(),
             'email' => $user->getEmail(),
             'exp' => time() + 3600 // 1 hour expiration
     ]);

     $view = View::create($token);
     $view->setFormat('json');

     return $view;  
 }

然后我尝试使用此代码来读取我的令牌信息:

/**
 * @Rest\View()
 * @Rest\Get("/api/user")
 */
public function getUsersAction(Request $request)
{
    $response = $this->get('lexik_jwt_authentication.jwt_manager')->create($this->getUser());
    $view = View::create($response);
    $view->setFormat('json');

    return $view;
}

但什么都没有..我不明白为什么..:/

如果有人愿意提供我的曲目,请

4

0 回答 0