Protect routes with passport laravel and Dingo Api

0

I have searched how to protect laravel routes through my Api with Dingo I found an example but I do not think it is the right one here link
If anyone knows another way to do from Dingo Api as it is done here using the library from Jwt where the library is integrated into the config / api.php file

JSON Web Tokens (JWT)

This package makes use of a 3rd party package to integrate JWT authentication. Please refer to the tymon/jwt-auth GitHub page for details on installing and configuring the package.

Once you have the package you can configure the provider in your config/api.php file or in a service provider or bootstrap file.

'auth' => [
    'jwt' => 'Dingo\Api\Auth\Provider\JWT',
],
app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
   return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});
    
asked by vdjkelly 24.10.2016 в 13:21
source

1 answer

0

At the end of so much struggle I could make Passport work with Dingo Api for my Apis-Rest I leave the code if someone needs it and can help and can improve it and make it more efficient and correct

GuardServiceProvider.php file

<?php
/**
 * Created by PhpStorm.
 * User: vdjke
 * Date: 10/28/2016
 * Time: 6:31 p.m.
 */

namespace App\Providers;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use Illuminate\Auth\AuthManager;
use Dingo\Api\Auth\Provider\Authorization;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

/**
 * Class GuardServiceProvider
 * @package App\Providers
 */
class GuardServiceProvider extends Authorization
{

    /**
     * @var \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
     */
    protected $auth;


    /**
     * @var string
     */
    protected $guard = 'api';


    /**
     * GuardServiceProvider constructor.
     * @param AuthManager $auth
     */
    public function __construct(AuthManager $auth)
    {
        $this->auth = $auth->guard($this->guard);
    }


    /**
     * @param Request $request
     * @param Route $route
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function authenticate(Request $request, Route $route)
    {
        if (! $user = $this->auth->user()) {
            throw new UnauthorizedHttpException(
                get_class($this),
                'Invalid API key and token.'
            );
        }

        return $user;
    }


    /**
     * @return string
     */
    public function getAuthorizationMethod()
    {
        return 'Bearer';
    }
}

On your Kernel.php

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

Use of middleware in your route files

$api->group(['middleware' => ['api']], function($api){
            $api->get('stream', 'ApiStreamController@index');
        });

I hope it serves you something and it will be useful to you

    
answered by 30.10.2016 / 15:49
source