I want to make a custom middleware in laravel but i have not idea how to supply token input

Viewed 17

"I want to make a custom middleware in laravel but I have not idea how to supply token input. Here I want to match token input with my secret key.

The code is:"

This is snapshot of my code

1 Answers

If you use this middleware for API, you can pass secret key in header at every request and function handle in EnsureTokenIsValid should look like this

public function handle(Request $request, Closure $next){
        if ($request->header('secret_key') !== 'your-secret-key'){
            // logic here
        }
        return $next($request);
}

If you use it for normal http requests pass it in body or query params

public function handle(Request $request, Closure $next){
        if ($request->get('secret_key') !== 'your-secret-key'){
            // logic here
        }
        return $next($request);
}

Hope help u and happy coding !

Related