Authentication on laravel API routes

1

I'm running some tests on building an API with Laravel. All the URLs of the API are currently accessing without authentication. I am trying to allow after user authentication.

In routes / api.php I'm defining:

<?php

use Illuminate\Http\Request;

Auth::routes();
Route::get('/member/{id}', 'PessoaController@edit')->middleware('auth:api');

And in my controller is defined this way:

<?php

  namespace App\Http\Controllers;

  use App\Pessoa;
  use JsValidator;
  use Illuminate\Support\Facades\Validator;
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\DB;
  use App\Http\Requests\PessoaRequest;

  date_default_timezone_set('America/Sao_Paulo');

   class PessoaController extends Controller
 {

public function __construct(Pessoa $pessoa)
{
    $this->Pessoa = $pessoa;
    $this->middleware('auth.api')->except(['index', 'show']);
    $this->middleware('auth.api:optional')->only(['index', 'show']);
}
}
    
asked by anonymous 11.06.2018 / 01:56

1 answer

0

Why define another APi within Routes? Here's how I do it.

Inside the routes.php

Route::resource('pessoas', 'PessoasController');

And inside the controller

public function __construct()
{
    $this->middleware('auth', ['only' => ['index']]); // exigir permissão para acessar
}
    
13.06.2018 / 16:19