dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

How to configure HTTP Basic auth? #1222

Open Sogl opened 8 years ago

Sogl commented 8 years ago

Hello!

In wiki: https://github.com/dingo/api/wiki/Authentication

I found that I need to put this code somewhere:

app('Dingo\Api\Auth\Auth')->extend('basic', function ($app) {
   return new Dingo\Api\Auth\Provider\Basic($app['auth'], 'email');
});

Also I found an issue with same problem: https://github.com/dingo/api/issues/694

My test route:

// example of protected route
$api->get('protected', ['middleware' => ['api.auth'], function () {
    return \App\Employee::all();
}]);

I tried to put in App\Providers\AppServiceProvider.php:

public function boot()
{
   $this->app('Dingo\Api\Auth\Auth')->extend('basic', function ($app) {
      return new Dingo\Api\Auth\Provider\Basic($app['auth'], 'email');
   });
}

Route call shows error:

FatalErrorException in AppServiceProvider.php line 17:
Call to undefined method App\Providers\AppServiceProvider::app()

Also tried to put in config/api.php this code:

'auth' => [
         'basic' => function ($app) {
          return new Dingo\Api\Auth\Provider\Basic($app['auth']);
        }
    ],

Error:

"message": "Call to undefined method Closure::authenticate()",
"code": 1,
"status_code": 500,

How to configure it?

Env: Laravel 5.3 Latest Dingo

bendparker commented 7 years ago

I second this question.

BartReymenants commented 7 years ago

I had the same issue.. problem was different databases.. the Auth User table was being queried from the wrong setup

karandatwani92 commented 7 years ago

put this in config/api.php

'auth' => [ 'basic' => 'Dingo\Api\Auth\Provider\Basic' ],

onim4ru commented 7 years ago

@karandatwani92 @Sogl @bendparker Hi guys, i've got the exact same problem. I've tried too this in my config/api.php but this isnt working. I'm on Laravel 5.4 with latest Dingo. The middleware used for routes is like you api.auth.

When i try to access to my route with postman, i select Basic Auth and i enter login and password and this works, but, when i try to access my route from direct url in the browser like http://site.com/api/users this gives me a (even if i logged on my site with another tab) :

Failed to authenticate because of bad credentials or an invalid authorization header

And if i try an internal request of this route, i've got the same error.

Do you find any solutions about this ? How did you proceed for your project ?

Best regards

eleftrik commented 7 years ago

Hi, same problem as @onim4ru. Someone please can help? Thanks