lundi 4 novembre 2019

Unauthorized error for api call laravel route JWT

I am doing login via mobile and generate token like:

{
    "token_type": "Bearer",
    "expires_in": 2255555,
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQwLCJpc3MiOiJodHRwOi8vbXljbGllbnR3b3JrLndlYnNpdGUvdHJhbnhpdF9zY2hlZHVsZS9wdWJsaWMvYXBpL3VzZXIvbG9naW4iLCJpYXQiOjE1NzI2OTM4MzUsImV4cCI6MTU3MzA1MzgzNSwibmJmIjoxNTcyNjkzODM1LCJqdGkiOiIwdHdrZHFwNHNzcXVRM0FZIn0.Jez-RqV1AXgaApGYQNRJorsW9XiYuX63hWJddLsppVk",
    "refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQwLCJpc3MiOiJodHRwOi8vbXljbGllbnR3b3JrLndlYnNpdGUvdHJhbnhpdF9zY2hlZHVsZS9wdWJsaWMvYXBpL3VzZXIvbG9naW4iLCJpYXQiOjE1NzI2OTM4MzYsImV4cCI6MTU3MzA1MzgzNiwibmJmIjoxNTcyNjkzODM2LCJqdGkiOiJJbDk1V2g3WHRhWHM0SDMxIn0.uDoMAIj33ldKT93jnRbr8Wc3kYAdfG0AV24ka9R5k68"
}

but it does not being authorized for further processing of application and give me

Unauthorized error. for other api requests.

I am using route middleware `auth:api'

and inside kernal.php, I have

protected $middlewareGroups = [
        'web' => [
            \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,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'account' => \App\Http\Middleware\RedirectIfNotAccount::class,
        'account.guest' => \App\Http\Middleware\RedirectIfAccount::class,
        'fleet' => \App\Http\Middleware\RedirectIfNotFleet::class,
        'fleet.guest' => \App\Http\Middleware\RedirectIfFleet::class,
        'dispatcher' => \App\Http\Middleware\RedirectIfNotDispatcher::class,
        'dispatcher.guest' => \App\Http\Middleware\RedirectIfDispatcher::class,
        'provider' => \App\Http\Middleware\RedirectIfNotProvider::class,
        'provider.guest' => \App\Http\Middleware\RedirectIfProvider::class,
        'provider.api' => \App\Http\Middleware\ProviderApiMiddleware::class,
        'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
        'admin.guest' => \App\Http\Middleware\RedirectIfAdmin::class,
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken',
        'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken',
        'demo' => \App\Http\Middleware\DemoModeMiddleware::class,
    ];

controller:

public function login(Request $request){

        if (isset($request->email))
        {

        $credentials = $request->only('email', 'password');

         try {
            if (! $token = JWTAuth::attempt($credentials)) {

                return response()->json(['error' => 'The email address or password you entered is incorrect.'], 401);
            }
        } catch (JWTException $e) {
            return response()->json(['error' => 'Something went wrong, Please try again later!'], 500);
        }

$refresh_token=$this->refresh_token($request);

        }
else{

    $token=$request->refresh_token;

$refresh_token = JWTAuth::refresh($request->refresh_token);



}

return response()->json(["token_type" => "Bearer",'expires_in' => 2255555,'access_token' => $token, 'refresh_token'=>$refresh_token]);


   }


public function refresh_token(Request $request)
    {

  $credentials = $request->only('email', 'password');

         try {
            if (! $token = JWTAuth::attempt($credentials)) {

                return 'The email address or password you entered is incorrect.';
            }
        } catch (JWTException $e) {
            return 'Something went wrong, Please try again later!';
        }
        return $token;
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire