user3058067
user3058067

Reputation: 327

php-open-source-saver/jwt-auth login error

i get the following error when i try to login and i don't find where the problem is. I use lumen 9 with php 8.1.

<!-- Lcobucci\JWT\Token\Builder::relatedTo(): Argument #1 ($subject) must be of type string, null given, called in /home/ss20nt22/public_html/wms/vendor/php-open-source-saver/jwt-auth/src/Providers/JWT/Lcobucci.php on line 212 (500 Internal Server Error) -->

here is my function:

public function login(Request $request)
    {
        
        $this->validate($request, [
            'nume' => 'required|string',
            'password' => 'required|string',
        ]);
        $credentials = $request->only(['nume', 'password']);
        if (!$token = Auth::attempt($credentials)) {
            // Login has failed
            return response()->json(['message' => 'Unauthorized'], 401);
        }
        return $this->respondWithToken($token);
    }

Upvotes: 2

Views: 10013

Answers (4)

Ditotisi
Ditotisi

Reputation: 21

If anyone have same problems in laravel 9 and above, but have sure JWT_SECRET is exist in .env file your problem can be in primary key id is not same between migrations and models.

when you have migration with custom id name like this :

 $table->id('user_id');

then register it in User Model like this:

protected $primaryKey = "user_id";

Upvotes: 2

adjustment layer
adjustment layer

Reputation: 360

I fixed it by running php artisan config:cache. This command caches your configuration. This helped me, as I noticed that environment variables sometimes do not have time to be loaded into the dynamic config.

And just in case, make sure that you generated secret key by running php artisan jwt:secret

Upvotes: 1

Pooja JaJal
Pooja JaJal

Reputation: 293

In config\jwt.php file Change

'jwt' => Tymon\JWTAuth\Providers\JWT\Lcobucci::class, to 'jwt' => Tymon\JWTAuth\Providers\JWT\Namshi::class

Upvotes: 1

Joandreiy Cordeiro
Joandreiy Cordeiro

Reputation: 156

I solved it as follows, I uninstalled the package and installed it again from the documentation here.

https://jwt-auth.readthedocs.io/en/develop/laravel-installation/

In the case of lumen, do the same procedure with the documentation below.

https://jwt-auth.readthedocs.io/en/develop/lumen-installation/

Steps:

uninstall the package

Remove JWT_SECRET from .env or elsewhere

If possible remove the file

config/jwt.php

Clear all lumen cache

Stop the web service

Follow the installation instructions in the link

https://jwt-auth.readthedocs.io/en/develop/lumen-installation/

Recalling that I use Laravel 8 in php 8

Upvotes: 0

Related Questions