Laravel Passport 注册用户凭据不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45171367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Passport Register the user credentials were incorrect
提问by utdev
I set up Laravel Passport and currently I am trying to register user with a Post Route. I did create a RegisterController
inside Controllers/Api/Auth
.
我设置了 Laravel Passport,目前我正在尝试使用 Post Route 注册用户。我确实创建了一个RegisterController
内部Controllers/Api/Auth
.
Thus I created a clients table which looks excatly like a users table. The client gets created if I call the route, but I do not get an access token nor a refresh token.
因此,我创建了一个看起来非常像用户表的客户表。如果我调用路由,客户端将被创建,但我没有获得访问令牌或刷新令牌。
The route to my controller looks like this (routes/api):
到我的控制器的路由看起来像这样(路由/api):
Route::post('register', ['as' => 'register', 'uses' => 'Api\Auth\RegisterController@register']);
My Controller looks like this:
我的控制器看起来像这样:
<?php
namespace App\Http\Controllers\Api\Auth;
use App\Client;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use Laravel\Passport\Client as PClient;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
private $client;
public function __construct() {
$this->client = PClient::find(1);
}
public function register(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|confirmed'
]);
$client_user = Client::create([
'name' => request('name'),
'email' => request('email'),
'password' => bcrypt(request('password'))
]);
$params = [
'grant_type' => 'password',
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'username' => request('email'),
'password' => request('password'),
'scope' => '*'
];
$request->request->add($params);
$proxy = Request::create('oauth/token', 'POST');
return Route::dispatch($proxy);
}
}
This is my Client Model:
这是我的客户端模型:
class Client extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, HasApiTokens, Notifiable;
protected $table = 'clients';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
When I am trying to call it with Postman I get this error message:
采纳答案by utdev
Ok I fixed it, the post route worked if I used the User
Model instead of my Client
model, so I guessed that there has to be something different.
好的,我修复了它,如果我使用User
模型而不是我的Client
模型,post 路由就可以工作,所以我猜肯定有一些不同的东西。
After some research I have found out that one needs to add the model, in my case the client model to the providers array inside config/auth.php
.
经过一番研究,我发现需要添加模型,在我的例子中是将客户端模型添加到config/auth.php
.
So first one needs to change the api guard like this:
所以第一个需要像这样改变 api 守卫:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'clients',
],
],
This way to api routes login and register only take action with my clients.
这种 api 路由登录和注册的方式只对我的客户采取行动。
Now you need to a a new provider in this case a clients provider like this.
现在你需要一个新的提供者,在这种情况下,一个像这样的客户端提供者。
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Client::class
],
],
And voila I get an access token + refresh token if I call the route.
瞧,如果我调用路由,我会得到一个访问令牌 + 刷新令牌。
回答by Ryan
I may be way off basis here but it looks as if you are creating your client with a password of "password" due to your bcrypt('password')
call.
我在这里可能有点偏离基础,但由于您的bcrypt('password')
呼叫,您似乎正在使用“密码”密码创建您的客户端。
Should it not be bcrypt(request('password'))
?
不应该bcrypt(request('password'))
吗?
This would explain why your credentials are wrong in your request, because they are ; )
这将解释为什么您的请求中的凭据是错误的,因为它们是;)