0

In my App i don t need a registration feature so i added a user in the database manually ,Actually I tried with the LexiJWTAuthenticationBundle i followed the documentation but unfortunately when I use the cURL to send a request i get the following error

<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.52 (Win64) OpenSSL/1.1.1m PHP/8.1.4 Server at localhost Port 443</address>
</body></html>```

**the cURL request is 

curl -X POST -H "Content-Type: application/json" https://localhost/api/check_login --data '{"email":"johndoe@gmail.fr","password":"test"}' -k


Moreover frankly i m confused about the logic of the bundle so im worried if i need a controller or not .
However in the frontENd i m using reactJS i just sent a post request using axios 
but when i hit submit in the network when i inspect i get 
{
  "code": 401,
  "message": "JWT Token not found"
}
Noor Ha
  • 1
  • 2

1 Answers1

2

I think documentation is only showing an example. Not sure if that works out of the box because after installing it, I haven't seen a new Controller to handle the login_check route. In fact, ask to add the route manually, without referring a resource.

To generate a Token from a user, in a controller you will need to instantiate Jwt.

// src/Controller/AuthController.PHP

use App\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;

class AuthController
{


    private JWTTokenManagerInterface $jwt;

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

    #[Route(path: '/token', name: 'api_auth_token')]
    public function login(Request $request): Response
    {
        // Code to retrieve your user 
        // [...]
        // User must extends UserInterface for Lexik to work properly
        $token = $this->jwt->create($user);
        // return the token 
    }

This code will show you how to create the token, but not to login. You will have to check your authentication flow.

Side note: Don“t confuse hashing the password with generating a token. You will need to do both things separately

I recommend you to look for the service you need by exploring autowirng:

php bin/console debug:autowiring jwt
Martin
  • 343
  • 2
  • 11