JWT web token does not give back user infos

75 views Asked by At

I have a problem with my API. After my login, I have a token, but unfortunately I can not read the information of my user with this token.

my code to have the token (that works):

/**
 * @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;  
 }

then I try to use this code to read the information of my token:

/**
 * @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;
}

but nothing does .. I do not understand why .. :/

If someone would have a track to offer me please

0

There are 0 answers