Eduardo Herrera
Eduardo Herrera

Reputation: 79

JWT Authentication Laravel

I'm trying to install jwt authentication in laravel, but I'm using laravel 5.8 and JWT, following this https://tutsforweb.com/restful-api-in-laravel-56-using-jwt-authentication/ , but display me this error.

There is any problem in my vendor/tymon/jwt-auth/src/jwt.php

between line 182 200

public function parseToken()
    {
        if (! $token = $this->parser->parseToken()) {
            throw new JWTException('The token could not be parsed from the request');
        }

        return $this->setToken($token);
    }

well and I found this suggestion and I followed this Laravel - JWT Auth The token could not be parsed from the request

public function parseToken($method = 'bearer', $header = 'authorization', $query = 'token')
{
    if (! $token = $this->parseAuthHeader($header, $method)) {
        if (! $token = $this->request->query($query, false)) {
            throw new JWTException('The token could not be parsed from the request', 400);
        }
    }

    return $this->setToken($token);
}

/**
 * Parse token from the authorization header.
 *
 * @param string $header
 * @param string $method
 *
 * @return false|string
 */
protected function parseAuthHeader($header = 'authorization', $method = 'bearer')
{
    $header = $this->request->headers->get($header);

    if (! starts_with(strtolower($header), $method)) {
        return false;
    }

    return trim(str_ireplace($method, '', $header));
}

how can I change this?

ErrorException : Undefined property: Tymon\JWTAuth\JWTAuth::$request at C:\laragon\www\ecommerce\vendor\tymon\jwt-auth\src\JWT.php:203

Upvotes: 0

Views: 2178

Answers (1)

Kenneth
Kenneth

Reputation: 2993

Try this step to set up your JWT Login,

  1. Install JWT package in you Laravel App

    documentation: https://jwt-auth.readthedocs.io/en/docs/laravel-installation/

    composer require tymon/jwt-auth:dev-develop

    or

    composer require tymon/jwt-auth:1.0.0-rc.5

  2. Add JWTMiddleware in you App\Http\Middleware

    https://github.com/kennethtomagan/laravel-5-api-boilerplate/blob/master/app/Http/Middleware/JWTMiddleware.php

  3. Register this middleware in $routeMiddleware of App\Http\kernel.php file.

protected $routeMiddleware = [
      ....
      ....
      'jwt' => \App\Http\Middleware\JWTMiddleware::class,
  ];
  1. Setup your controller

sample: https://github.com/kennethtomagan/laravel-5-api-boilerplate/blob/master/app/Http/Controllers/Auth/AuthController.php

  1. Setup your API route
Route::group(['middleware' => [ 'jwt', 'jwt.auth']], function () {
    ....
    ....
    });

Hope it helps,

Working example: https://github.com/kennethtomagan/laravel-5-api-boilerplate

Upvotes: 1

Related Questions