csharper
csharper

Reputation: 83

Laravel 5.3 RESTFul API without authentication

I want to create an API with Laravel 5.3 but i don't need any kind of authentication. Is it possible to get rid of it? I don't want any token or any kind of authentication.

Upvotes: 4

Views: 11001

Answers (5)

sgtcadet
sgtcadet

Reputation: 319

Yes, it's possible normally in your

route/api.php

you'd have something like

Route::middleware('auth:api')->get('/user', function (Request $request) {
   return $request->user();
});

you just need to remove the part of the middleware that's referencing auth. So the above would look like:

Route::middleware('api')->get('/user', function (Request $request) {
  return $request->user();
  //middleware('api') URI prefix. which would become '/api/user'
});

or

Route::apiResource('user', 'UserController');
//same as above but includes crud methods excluding 'create and edit'

Upvotes: 7

Adrien C
Adrien C

Reputation: 424

To help anyone in my situation who arrive here : be aware that any route in api.php is prefixed by "api/". It is set in /app/Providers/RouteServiceProvider.php.

So :

Route::get('/delegates', "APIController@delegate");

Will be accessible from

http://www.yourdomain.com/api/delegates

Sorry if it's a bit off-topic, but hope it can help someone.

Upvotes: 5

Elias Soares
Elias Soares

Reputation: 10264

Of course you can get rid of it. Just setup your routes to don't use any middleware.

Create your API routes on routes/api.php file, then modify the app/Http/Kernel.php file to set your middlewares correctly:

Remove (or add) the middlewares you don't want on api middleware group.

By default, L5.3 comes with two middlewares on api group:

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

The first one provides a rate limiting to your API (60 requests/minute), the second substitutes your model bindings.

Upvotes: 2

Komal
Komal

Reputation: 2736

Allow your route to run without auth

Http\Middleware\VerifyCsrfToken
public function handle($request, Closure $next)
{
  if (!$request->is('api/*'))
  {
    return parent::handle($request, $next);
  }

  return $next($request);
}

Set route like this

'api' => 'APIController'

This is method in APIController ('/api/data')

public function getData(Request $request)
{
  return "Hello";
}

Upvotes: -1

Marek Skiba
Marek Skiba

Reputation: 2184

It's possible, just create route to your controller and return data (Without any auth middleware).

Upvotes: 1

Related Questions