2017-05-25 12 views
5

私はパスワード付与を設定しました(これはアプリのバックエンドです)。今、私はoauth/tokenへのポストリクエストを送ることができ、ポストマンで動作します。しかし、私もapiからユーザーを登録したいのですが?ユーザーをLaravel Passportに登録する

私は現在の/registerルートを使用することができますが、ユーザーをログインページにリダイレクトする必要があり、彼の資格情報で再度ログインする必要がありますか?

またはRegisterControllerのregistered()機能では、oauth/tokenルートにリダイレクトする必要がありますか? (これについては、「x-www-form-urlencoded」の5つのデータをすべて送信していることに注意してください。それはうまくいくようですが、ヘッダーでいくつかのデータを分離する必要がありますか?私はチャンスがあるときに尋ねる)。

this guyのようにoauth/tokenメソッドに何かを追加する必要がありますか?実際には、投稿された$requestのデータをライブラリ内の[email protected]メソッドにキャッチしようとしましたが、parsedBody配列を操作する方法がわかりませんでした。実際のライブラリから登録機能を起動すると、それが登録かログインかわかりますか?

多分私はいくつかの情報を見逃していますが、私はこのトピックに基づいて何も見つけることができませんでした。 Passportにユーザーを登録する適切な方法は何ですか?あなたのAPIで

答えて

4

Route::post('register','Api\[email protected]'); 

としてルートを作成し、がUserController方法create()

function create(Request $request) 
{ 
    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $request 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    $valid = validator($request->only('email', 'name', 'password','mobile'), [ 
     'name' => 'required|string|max:255', 
     'email' => 'required|string|email|max:255|unique:users', 
     'password' => 'required|string|min:6', 
     'mobile' => 'required', 
    ]); 

    if ($valid->fails()) { 
     $jsonError=response()->json($valid->errors()->all(), 400); 
     return \Response::json($jsonError); 
    } 

    $data = request()->only('email','name','password','mobile'); 

    $user = User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
     'mobile' => $data['mobile'] 
    ]); 

    // And created user until here. 

    $client = Client::where('password_client', 1)->first(); 

    // Is this $request the same request? I mean Request $request? Then wouldn't it mess the other $request stuff? Also how did you pass it on the $request in $proxy? Wouldn't Request::create() just create a new thing? 

    $request->request->add([ 
     'grant_type' => 'password', 
     'client_id'  => $client->id, 
     'client_secret' => $client->secret, 
     'username'  => $data['email'], 
     'password'  => $data['password'], 
     'scope'   => null, 
    ]); 

    // Fire off the internal request. 
    $token = Request::create(
     'oauth/token', 
     'POST' 
    ); 
    return \Route::dispatch($token); 
} 

作成し、新しいユーザーを作成した後、アクセストークンを返します。

関連する問題