2017-07-18 109 views
2

我設置了Laravel Passport,目前我正在嘗試向Post Route註冊用戶。我確實在Controllers/Api/Auth內部創建了一個RegisterControllerLaravel Passport註冊用戶憑據不正確

因此,我創建了一個客戶表,它看起來像用戶表激動。 如果我調用路由,客戶端會被創建,但我沒有獲得訪問令牌或刷新令牌。

到我的控制器的路線是這樣的(路由/ API):

Route::post('register', ['as' => 'register', 'uses' => 'Api\Auth\[email protected]']); 

我的控制器看起來是這樣的:

<?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); 
    } 
} 

這是我的客戶端模式:

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']; 

當我試圖與郵遞員打電話時,我收到以下錯誤消息: enter image description here

回答

1

好吧我修好了,如果我使用User模型而不是我的Client模型,後路由工作,所以我猜想必須有不同的東西。

經過一番研究,我發現需要添加模型,在我的情況下,客戶端模型爲config/auth.php內的providers數組。

所以第一個需要改變的API後衛是這樣的:

'guards' => [ 
    'web' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 

    'api' => [ 
     'driver' => 'passport', 
     'provider' => 'clients', 
    ], 
], 

這種方式API路線註冊登錄的唯一採取行動,與我的客戶。

現在你需要一個新的提供者在這種情況下這樣的客戶提供者。

'providers' => [ 
    'users' => [ 
     'driver' => 'eloquent', 
     'model' => App\User::class, 
    ], 
    'clients' => [ 
     'driver' => 'eloquent', 
     'model' => App\Client::class 
    ], 
], 

並且瞧,如果我呼叫路由,我會得到一個訪問令牌+刷新令牌。

4

我在這裏可能會失去基礎,但看起來好像您是通過撥打bcrypt('password')來撥打密碼「password」來創建您的客戶端。

是不是bcrypt(request('password'))

這可以解釋爲什麼您的憑據在您的請求中是錯誤的,因爲它們是; )

+0

完全誤讀了這個問題,然後誤判了你的答案,所以實際上+1 – ash

+0

首先感謝你的答案,我改變了這一行,更新了我的問題中的控制器,但我仍然得到相同的錯誤信息 – utdev

+0

好吧,這很奇怪,如果我改變客戶端到用戶它的作品,但我不明白爲什麼想法? – utdev