DEV Community

Build a JWT Authenticated API with Lumen(v5.8)

Ndifreke Friday on August 24, 2019

In this tutorial, we will be using lumen; a super-fast micro-framework by laravel to build a simple and secure REST API. At the end of this tutoria...
Collapse
 
dhavaldignizant profile image
dhaval-dignizant •

getting this error after install jwt

php artisan jwt:secret

There are no commands defined in the "jwt" namespace.

Collapse
 
b6t3m6n profile image
B6T3M6N •

Had the same issue.

following the docs here jwt-auth.readthedocs.io/en/develop...
especially the chapter Bootstrap file changes fixed the issue

Add the following snippet to the bootstrap/app.php file under the providers section as follows:

// Uncomment this line
$app->register(App\Providers\AuthServiceProvider::class);

// Add this line
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ndiecodes profile image
Ndifreke Friday •

Please check to make sure tymon/jwt-auth installed successfully

Also type php artisan to see available commands.

Keep me posted on your progress.

Collapse
 
ndiecodes profile image
Ndifreke Friday •

Side note: the command jwt:secret updates your .env file with something like JWT_SECRET=secretkeystring.

"It is the key that will be used to sign your tokens. How that happens exactly will depend on the algorithm that you choose to use."

Thread Thread
 
dhavaldignizant profile image
dhaval-dignizant •

now it's working. Thank you.

Thread Thread
 
ndiecodes profile image
Ndifreke Friday •

You are welcome

Collapse
 
cleathley profile image
Chris Leathley • • Edited

you might want to also add

use Illuminate\Support\Facades\Auth;

when you add respondWithToken to app/Http/Controllers/Controller.php (Lumin 6+)

Collapse
 
kbzone profile image
Cabello, Hector Guillermo •

This is key.

I was followed step by step your tutorial, writing the code by myself (i mean, without clone your github project) and without this line (thanks @cleathley ) an error appears.

I think you should edit your post and add that line after you explain this:
«Add a global respondWithToken method to Controller class»

Anyway, great tutorial. It help me a lot!!

Collapse
 
ndiecodes profile image
Ndifreke Friday • • Edited

I'm glad it helped 👍

I will update the post, so new readers don't run into this same error.

Collapse
 
ndiecodes profile image
Ndifreke Friday •

Thanks For this, I will update the tutorial to help people not encounter this error in the future.

Collapse
 
sayajin101 profile image
Syns •

This saved my life ty

Collapse
 
wmazed profile image
Walid Yacine MAZED • • Edited

First of all great tutorial, thanks a lot for all your efforts dude, I've a question, how can I set a remember me system with JWT token ? And also, how to increase expiration duration from the config file (config/auth.php) ?

Collapse
 
ndiecodes profile image
Ndifreke Friday • • Edited

Would there be a need for remember me? As long as the client has the token; they have access to the system.

You could extend token's time to live for those type of users, that could work...I think.

Override the token ttl
$token = auth()->setTTL(7200)->attempt($credentials);

Further Reading
stackoverflow.com/questions/236038...

Collapse
 
wmazed profile image
Walid Yacine MAZED • • Edited

Thanks dude, I've another question please, if the token is stocked localy, then yo access protected area I've check the user token for that, if Auth::user() did the job, why we stock the token localy ?

Collapse
 
wmazed profile image
Walid Yacine MAZED •

For the second part of the question, the answer should be:
github.com/tymondesigns/jwt-auth/b...

Collapse
 
debabratakarfa profile image
Debabrata Karfa • • Edited

Getting this error,

In Connection.php line 665:

SQLSTATE[HY000] [2002] Connection refused (SQL: create table migrations (id int unsigned not null auto_increment primary key, migration varchar(255) not null, batch int not
null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

In Connector.php line 70:

SQLSTATE[HY000] [2002] Connection refused

Using Homestead development environment.

Collapse
 
ndiecodes profile image
Ndifreke Friday • • Edited

This error is due to PHP not being able to connect to MySQL

"Create a .env file, copy all contents in .env.example into the .env file and add your database configurations."

Collapse
 
racedaemon profile image
racedaemon •

This was of great help. The only problem I encountered was when running php artisan jwt:secret.
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class); needs to be added to app.php before the command is executed.

Collapse
 
bosz profile image
Fongoh Martin T. •

Nice tutorial, very clear and straight to the point. My worry is why the dev-develope branch? The main branch, does it have an issue?

Collapse
 
ndiecodes profile image
Ndifreke Friday •

For some reason the dev-develop branch has always worked for new versions of lumen, the next best thing would be version @2.0-dev

Collapse
 
bosz profile image
Fongoh Martin T. •

Ok thanks. I tried using the default and it keeps failing and when i use dev-develop, it worked well.

Thanks again for the tutorial, nice, easy and straight to the point.

I come again with one question. If i log into different devices, eg 4 different clients and have 4 different tokens, will all 4 be valid simultaneously? If not, what will you suggest to be done to ensure multiple tokens are valid across different devices.

:)

Thread Thread
 
ndiecodes profile image
Ndifreke Friday •

Every Token Generated that is yet to expired is valid; So yes, a user can have multiple tokens across multiple devices.

Collapse
 
executions12 profile image
executions12 •

everything except php artisan jwt::secret is work...
so, i write jwt key in .env by myself..

thank you, nice tutorial...

Collapse
 
jaiminhothi profile image
jaimin-hothi • • Edited

Hi,
Thanks for your great tutorial.
But I want a custom field (email and password) both so could you make it an example for that.

My field name = USER_NAME, PASSWORD (Both are capital)

I try but token always false.

If you do then its great help for me.

Thank you

Collapse
 
yunchurlee profile image
yunchurlee •

I've just reviewed and coded through. It worked wonderfully~~~

Collapse
 
isrortega profile image
isrortega •

Thank you, very good tutorial

Collapse
 
guibattoni profile image
Guilherme Battoni •

Thanks for this article! Very straightforward and easy to understand : )

Collapse
 
ndiecodes profile image
Ndifreke Friday •

I'm glad it helped :)

Collapse
 
ndiecodes profile image
Ndifreke Friday •

I am yet to come across this issue. Is there an open issue on github?

Collapse
 
nwaweru profile image
Ndirangu Waweru •

Nice one!

Collapse
 
ndiecodes profile image
Ndifreke Friday •

Thanks!

Collapse
 
techautobahn profile image
TechAutobahn •

Wonderful Bro.. It really helped me to understand the concept.. Thank you..

Collapse
 
devsakib profile image
Nazmus Sakib •

Thanks for the tutorial, helps me a lot

Collapse
 
amanuell2 profile image
amanuell2 •

can you go further this post by making role-based auth in lumen?

Collapse
 
bluerabby profile image
bluerabby •

getting this error when doing login request

"Method Illuminate\Auth\RequestGuard::attempt does not exist."

Any help ?

Collapse
 
bluerabby profile image
bluerabby •

Sorry, my mistake who put config/auth.php that not in root directory

Collapse
 
guimilleo profile image
Guilherme Milléo •

Please, can you explain how to reset the password of user?

Collapse
 
ndiecodes profile image
Ndifreke Friday •
Collapse
 
schallernicolas profile image
schallernicolas •

What a great tutorial. Thank you very much!

Collapse
 
ndiecodes profile image
Ndifreke Friday •

You are welcome

Collapse
 
be_anjos profile image
Brenddon Anjos •

Hy bro, thanks for the tutorial. I want to know how I implements logout

Collapse
 
mamedioguilherme1 profile image
Guilherme Mamedio •

public function logout () {
Auth::logout();
return response()->json(['message' => 'Successfully logged out']);
}

Collapse
 
ainurrahms profile image
Ainur Rahman •

Does the lumen version affect? I use lumen version 6

Collapse
 
mayank091193 profile image
Mayank Patel •

This was really helpful, man! Thanks very much.

Collapse
 
jenueldev profile image
Jenuel Oras Ganawed •

how to do this when using mongodb?

Collapse
 
tadeubdev profile image
Tadeu Barbosa •

Great post! You had save me!

Collapse
 
alih70442 profile image
Ali H •

thanks for sharing this great tutorial.
I had issue with ('model' => \App\User::class).
so I changed it to ('model' => \App\Models\User::class),
and it works now.

Collapse
 
wandieinnocents profile image
WENAREEBA INNOCENT •

This has been an amazing tutorial , few bugs but were resolved :)-