laravel middleware check custom header for REST API

Today, We want to share with you laravel middleware check custom header for REST API.In this post we will show you laravel set request header, hear for how to get header value in laravel middleware we will give you demo and example for implement.In this post, we will learn about laravel check if header is present with an example.

laravel middleware check custom header for REST API

There are the Following The simple About Set header in request (middleware) Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel middleware check header, so the laravel set header middleware is used for this example is following below.

How to create and check custom header with middleware for REST API in Laravel?

Laravel is a web application framework with expressive, elegant syntax.The PHP Framework for Web Artisans,freeing you to create without sweating the small things. CRUD Operation With Server Side.

Your Jquery Ajax or AngularJS/Vuejs Http Request

    $.ajax({  
         type: 'POST',  
         dataType: 'json',  
         url: 'https://www.pakainfo.com/api/login',  
         headers: { 'X-infinityknow': '982560624'},  
         data: {'email':'[email protected]','password':'982560624'}  
    }).done(function(data){  
        alert('Sign In Successfully, Good Luck');  
    }).fail(function(jqXHR, ajaxOptions, thrownError){  
         alert(jqXHR.responseText);  
    });  

fire this command and create middleware.

php artisan make:middleware isCheckActiveUser

app/Http/Middleware/isCheckActiveUser.php

namespace App\Http\Middleware;  
  
use Closure;  
use Illuminate\Contracts\Auth\Guard;  
use Response;  
  
class isCheckActiveUser  
{  
    public function handle($request, Closure $next)  
    {  
        if(!isset($_SERVER['HTTP_X_INFINITY'])){  
            return Response::json(array('error'=-->'Infor for Please set custom users header'));  
        }  
  
        if($_SERVER['HTTP_X_INFINITY'] != '982560624'){  
            return Response::json(array('error'=>'Sorry dear wrong custom users header'));  
        }  
  
        return $next($request);  
    }  
}

add in app/Http/Kernel.php

namespace App\Http;  
  
use Illuminate\Foundation\Http\Kernel as HttpKernel;  
  
class Kernel extends HttpKernel  
{  
    /** 
     * The application's global HTTP middleware stack. 
     * 
     * @var array 
     */  
    protected $middleware = [  
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,  
        \App\Http\Middleware\EncryptCookies::class,  
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,  
        \Illuminate\Session\Middleware\StartSession::class,  
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,  
        \App\Http\Middleware\VerifyCsrfToken::class,  
    ];  
  
    /** 
     * The application's route middleware. 
     * 
     * @var array 
     */  
    protected $routeMiddleware = [  
        'auth' =--> \App\Http\Middleware\Authenticate::class,  
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,  
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,  
        'isCheckActiveUser' => \App\Http\Middleware\isCheckActiveUser::class,  
    ];  
}  

use in your route

Route::post('api/login', array('uses' => 'APIAuthController@login','middleware' => ['isCheckActiveUser']));
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel auth:api middleware.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment