<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: meherulsust</title>
    <description>The latest articles on DEV Community by meherulsust (@meherulsust).</description>
    <link>https://dev.to/meherulsust</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F323069%2F357f0255-fb75-4774-9fb0-3674569fc509.png</url>
      <title>DEV Community: meherulsust</title>
      <link>https://dev.to/meherulsust</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meherulsust"/>
    <language>en</language>
    <item>
      <title>How to build a JWT Authenticated API with Lumen (8.3.1)</title>
      <dc:creator>meherulsust</dc:creator>
      <pubDate>Fri, 03 Dec 2021 18:59:31 +0000</pubDate>
      <link>https://dev.to/meherulsust/how-to-build-a-jwt-authenticated-api-with-lumen-831-171o</link>
      <guid>https://dev.to/meherulsust/how-to-build-a-jwt-authenticated-api-with-lumen-831-171o</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mtrerq74u4hpqvzdhme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mtrerq74u4hpqvzdhme.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Lumen is a the stunningly fast micro-framework by Laravel built to deliver microservices and blazing fast APIs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this tutorial, i would like to show you how to build a JWT Authenticated API with Lumen 8.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server Requirements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PHP &amp;gt;= 7.3&lt;/li&gt;
&lt;li&gt;OpenSSL PHP Extension&lt;/li&gt;
&lt;li&gt;PDO PHP Extension&lt;/li&gt;
&lt;li&gt;Mbstring PHP Extension&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install Lumen via composer&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer create-project --prefer-dist laravel/lumen blog-api


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add &lt;strong&gt;config&lt;/strong&gt; floder to the root directory and add auth.php file to the config folder, &lt;strong&gt;config/auth.php&lt;/strong&gt; and add the code below into the auth.php file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

return [
    'defaults' =&amp;gt; [
        'guard' =&amp;gt; 'api',
        'passwords' =&amp;gt; 'users',
    ],

    'guards' =&amp;gt; [
        'api' =&amp;gt; [
            'driver' =&amp;gt; 'jwt',
            'provider' =&amp;gt; 'users',
        ],
    ],

    'providers' =&amp;gt; [
        'users' =&amp;gt; [
            'driver' =&amp;gt; 'eloquent',
            'model' =&amp;gt; \App\Models\User::class
        ]
    ]
];


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Install jwt-auth via composer&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer require tymon/jwt-auth:*


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Bootstrap file changes.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add the following snippet to the bootstrap/app.php file under the root directory as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// Uncomment this line

$app-&amp;gt;withFacades();

$app-&amp;gt;withEloquent();

Then uncomment the auth middleware 

$app-&amp;gt;routeMiddleware([
    'auth' =&amp;gt; App\Http\Middleware\Authenticate::class,
]);

$app-&amp;gt;register(App\Providers\AuthServiceProvider::class);

// Add this line in the same file:

$app-&amp;gt;register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Generate secret key&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To generate a key for you:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan jwt:secret


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will update your .env file with something like &lt;/p&gt;

&lt;p&gt;JWT_SECRET=secret_jwt_string_key&lt;/p&gt;

&lt;p&gt;It is the key that will be used to sign your tokens&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database Connection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=root
DB_PASSWORD=


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Create Migration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run this for user table migration below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:migration create_users_table


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And replace it with the below code at &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;database\migrations*_create_users_table.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('name');
            $table-&amp;gt;string('email')-&amp;gt;unique();
            $table-&amp;gt;timestamp('email_verified_at')-&amp;gt;nullable();
            $table-&amp;gt;string('password');
            $table-&amp;gt;rememberToken();
            $table-&amp;gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}






&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Migrate your database&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ php artisan migrate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create database seeder for a user by runing:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:seeder UsersTableSeeder


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And replace it with the below code at &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;database\seeders\UserTableSeeder.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;


class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $users = User::create([
            'name' =&amp;gt; 'Md.Meherul Islam', 
            'email' =&amp;gt; 'meherul@gmail.com',
            'password' =&amp;gt; Hash::make('12345678')
        ]);
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then run the fllowing command to insert data into your database&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan db:seed --class=UserTableSeeder


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now create a files AuthController.php into &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;app\Http\Controllers\AuthController.php&lt;br&gt;
 with the below one&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use  App\Models\User;

class AuthController extends Controller
{


    public function __construct()
    {
        $this-&amp;gt;middleware('auth:api', ['except' =&amp;gt; ['login', 'refresh', 'logout']]);
    }
    /**
     * Get a JWT via given credentials.
     *
     * @param  Request  $request
     * @return Response
     */
    public function login(Request $request)
    {

        $this-&amp;gt;validate($request, [
            'email' =&amp;gt; 'required|string',
            'password' =&amp;gt; 'required|string',
        ]);

        $credentials = $request-&amp;gt;only(['email', 'password']);

        if (! $token = Auth::attempt($credentials)) {
            return response()-&amp;gt;json(['message' =&amp;gt; 'Unauthorized'], 401);
        }

        return $this-&amp;gt;respondWithToken($token);
    }

     /**
     * Get the authenticated User.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function me()
    {
        return response()-&amp;gt;json(auth()-&amp;gt;user());
    }

    /**
     * Log the user out (Invalidate the token).
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        auth()-&amp;gt;logout();

        return response()-&amp;gt;json(['message' =&amp;gt; 'Successfully logged out']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function refresh()
    {
        return $this-&amp;gt;respondWithToken(auth()-&amp;gt;refresh());
    }

    /**
     * Get the token array structure.
     *
     * @param  string $token
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondWithToken($token)
    {
        return response()-&amp;gt;json([
            'access_token' =&amp;gt; $token,
            'token_type' =&amp;gt; 'bearer',
            'user' =&amp;gt; auth()-&amp;gt;user(),
            'expires_in' =&amp;gt; auth()-&amp;gt;factory()-&amp;gt;getTTL() * 60 * 24
        ]);
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now replace User Model code into app\Models\User.php with below code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

//this is new
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject 
{
    use Authenticatable, Authorizable;

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this-&amp;gt;getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Change route files into the routes\web.php with the code below :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

/** @var \Laravel\Lumen\Routing\Router $router */

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/




$router-&amp;gt;get('/', function () use ($router) {
    echo "&amp;lt;center&amp;gt; Welcome &amp;lt;/center&amp;gt;";
});

$router-&amp;gt;get('/version', function () use ($router) {
    return $router-&amp;gt;app-&amp;gt;version();
});

Route::group([

    'prefix' =&amp;gt; 'api'

], function ($router) {
    Route::post('login', 'AuthController@login');
    Route::post('logout', 'AuthController@logout');
    Route::post('refresh', 'AuthController@refresh');
    Route::post('user-profile', 'AuthController@me');

});






&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test Lumen JWT Authentication API with Postman&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now for built-in PHP development server:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php -S localhost:8000 -t public



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We have created a secure REST API using JWT Authentication. To make the testing process easy and subtle, we will rely on Postman.&lt;/p&gt;

&lt;p&gt;Authentication APIs for Login, User Profile, Token Refresh and Logout.&lt;/p&gt;

&lt;p&gt;Method  Endpoint at post man&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;POST  localhost:8000/api/login&lt;/li&gt;
&lt;li&gt;POST  localhost:8000/api/user-profile&lt;/li&gt;
&lt;li&gt;POST  localhost:8000/api/refresh&lt;/li&gt;
&lt;li&gt;POST  localhost:8000/api//logout&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>System Design in Software Development</title>
      <dc:creator>meherulsust</dc:creator>
      <pubDate>Fri, 29 May 2020 07:47:57 +0000</pubDate>
      <link>https://dev.to/meherulsust/system-design-in-software-development-6o4</link>
      <guid>https://dev.to/meherulsust/system-design-in-software-development-6o4</guid>
      <description>&lt;p&gt;What is System Design: Software system design is a process to convert user whole requirements into some appropriate form, which helps the software developer in coding and implementation. It is the process of designing the elements of a system such as the architecture, modules, and components, the different interfaces of those components, and the data that goes through that system.&lt;/p&gt;

&lt;p&gt;Some of the major tasks should be performed during the system design process :&lt;/p&gt;

&lt;p&gt;1.Requirement analysis&lt;/p&gt;

&lt;p&gt;2.Initialize design definition&lt;/p&gt;

&lt;p&gt;3.Document the design definition strategy&lt;/p&gt;

&lt;p&gt;4.Plan and Identify the technologies&lt;/p&gt;

&lt;p&gt;5.Database Design&lt;/p&gt;

&lt;p&gt;6.Load Balancing&lt;/p&gt;

&lt;p&gt;7.Scaling&lt;/p&gt;

&lt;p&gt;8.Sharding&lt;/p&gt;

&lt;p&gt;9.Caching&lt;/p&gt;

&lt;p&gt;9.Indexing&lt;/p&gt;

&lt;p&gt;etc&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
