欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to configure HTTP Basic auth

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:
#694

My test route:

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

I tried to put inApp\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 inconfig/api.phpthis 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

I second this question.

 
@BartReymenants

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

 
@karandatwani92

put this in config/api.php

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

 
@onim4ru

onim4rucommentedon 27 Maredited

@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 youapi.auth.

When i try to access to my route with postman, i selectBasic Authand i enter login and password and this works, but, when i try to access my route from direct url in the browser likehttp://site.com/api/usersthis 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

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

来自 https://github.com/dingo/api/issues/1222

如何配置HTTP基本认证

你好!

在维基:
http:/ / / / / github.com野狗维基/ API认证

我发现我需要把这段代码的某个地方:

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

我也发现了一个问题,同样的问题:
# 694

我的测试路线:

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

我试图把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');
   });
}

路由呼叫显示错误:

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

也试图把config/api.php此代码:

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

误差:

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

如何配置?

Env:
laravel 5.3
最新的澳洲野狗

 
@bendparker

我的第二个问题。

 
@BartReymenants

我也有同样的问题。问题是,不同的数据库。
认证用户表被质疑从错误的设置

 
@karandatwani92

把这个配置/ api.php

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

 
@onim4ru

onim4ru评论on 27 Mar编辑

karandatwani92 @@ Sogl“bendparker
嗨,我有同样的问题。我的配置api.php也试过但这不是工作。
我在Laravel 5.4最新的澳洲野狗。用于路由的中间件是喜欢你api.auth

当我试图进入我的路线与邮递员,我选择Basic Auth我输入登录名和密码,这样的作品,但是,当我想访问我的路线从直接的URL在浏览器中一样http:/ / / / site.com API用户这给了我一个(即使我登录我的网站与另一个表):

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

如果我尝试这条路线的内在要求,我有同样的错误。

你觉得这个有什么解决办法吗?
你是如何进行你的计划?

诚挚的问候

 
@eleftrik

嗨,同样的问题“onim4ru。有人能帮忙吗?
感谢

来自 https://github.com/dingo/api/issues/1222
普通分类: