Create JWT Based Authentication Endpoint With Laravel Lumen

Many modern applications today implemented JWT (JSON Web Token) based authentication, although there are some pros/cons around it, JWT still a popular token format among developers.

Lumen Version

php artisan --version

Dependency

composer require firebase/php-jwt

Table Structure

CREATE TABLE `user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`password` varchar(100) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

or if you prefer migration script:

public function up()
{
Schema::create('user', function(Blueprint $table)
{
$table->increments('id');
$table->string('username', 25)->unique('username');
$table->string('password', 100);
$table->timestamps();
});
}

User Model

JWT Secret

JWT_SECRET=my-strong**secret!!!key

Auth Controller

Register Route

$router->group(['prefix'=> 'auth'], function() use ($router) {
$router->post('login', [
'uses' => 'AuthController@authenticate'
]);
});

Testing

POST to route /auth/login

--

--

enterprise architect. tech enthusiast. coffee addict. https://blog.rumbia.id

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store