LazyCodet

a

13:06:54 28/4/2024 - 1 views -
Programming

2 ways to Get headers Authorization in Laravel and PHP Core

1. For PHP Core & Laravel

public function VerifyToken($jwt = null)
{
    $headers = getallheaders();
    if(false == isset($headers['Authorization'])
    && false == isset($headers['authorization']))
    {
        return [
            'status' => 401,
            'message' => "Authorization's not received"
        ];
    }
    $headerAuthor = isset($headers['Authorization']) ? $headers['Authorization'] : $headers['authorization'];
    $jwt = $headerAuthor;
    $jwt = str_replace("Bearer ", "", $jwt);

​This way is not good when using Laravel. Because when I write a Feature test, getallheaders() returns an empty array, so we go to the next way

​2. For Laravel

public function VerifyToken($jwt = null)
{
    $jwt = request()->headers->get('authorization');
    if($jwt === null)
    {
        return [
            'status' => 401,
            'message' => "Authorization's not received"
        ];
    }
    $jwt = str_replace("Bearer ", "", $jwt);