LarabelとPassportを使って、APIサーバーを作っています。
新規ユーザー登録のAPIの作り方を教えてください。
クライアントアプリはシークレットキーとクライアントIDは持っているので、それと合わせてIDとパスワードをPostすると、アクセストークンが返って来るようにしたいです。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/17 01:39
回答2件
0
まず、routesのapi.phpに以下を追記します。
php
1Route::post('users', 'UserController@store');
その後、UserController.phpを作成し、以下のコードを書きました。
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\User; 7 8class UserController extends Controller 9{ 10 public function store(Request $request) 11 { 12 $valid = validator($request->only('email', 'name', 'password'), [ 13 'name' => 'required|string|max:255', 14 'email' => 'required|string|email|max:255|unique:users', 15 'password' => 'required|string|min:6', 16 ]); 17 18 if ($valid->fails()) { 19 $jsonError=response()->json($valid->errors()->all(), 400); 20 return \Response::json($jsonError); 21 } 22 23 $data = request()->only('email','name','password'); 24 25 $user = User::create([ 26 'name' => $data['name'], 27 'email' => $data['email'], 28 'password' => bcrypt($data['password']) 29 ]); 30 31 $request->request->add([ 32 'username' => $data['email'], 33 'password' => $data['password'] 34 ]); 35 36 // Fire off the internal request. 37 $token = Request::create( 38 'oauth/token', 39 'POST' 40 ); 41 return \Route::dispatch($token); 42 } 43} 44
クライアントには、client_idとclient_secretを持たせておきます。
APIの叩き方は、以下です。
http://localhost/api/users [POST]
json
1{ 2 "name" : "name", 3 "email": "testuser@test.com", 4 "password": "testtest", 5 "grant_type": "password", 6 "client_id" : "{your_client_id}", 7 "client_secret": "{your_client_secret}", 8 "scope": "" 9}
すると、以下の内容がかえります。
json
1{ 2"token_type": "Bearer", 3"expires_in": 31535999, 4"access_token": "token", 5"refresh_token": "token" 6}
以上です。
投稿2018/07/19 03:38
総合スコア35
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
自己解決
まず、routesのapi.phpに以下を追記します。
php
1Route::post('users', 'UserController@store');
その後、UserController.phpを作成し、以下のコードを書きました。
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\User; 7 8class UserController extends Controller 9{ 10 public function store(Request $request) 11 { 12 $valid = validator($request->only('email', 'name', 'password'), [ 13 'name' => 'required|string|max:255', 14 'email' => 'required|string|email|max:255|unique:users', 15 'password' => 'required|string|min:6', 16 ]); 17 18 if ($valid->fails()) { 19 $jsonError=response()->json($valid->errors()->all(), 400); 20 return \Response::json($jsonError); 21 } 22 23 $data = request()->only('email','name','password'); 24 25 $user = User::create([ 26 'name' => $data['name'], 27 'email' => $data['email'], 28 'password' => bcrypt($data['password']) 29 ]); 30 31 $request->request->add([ 32 'username' => $data['email'], 33 'password' => $data['password'] 34 ]); 35 36 // Fire off the internal request. 37 $token = Request::create( 38 'oauth/token', 39 'POST' 40 ); 41 return \Route::dispatch($token); 42 } 43} 44
クライアントには、client_idとclient_secretを持たせておきます。
APIの叩き方は、以下です。
http://localhost/api/users [POST]
json
1{ 2 "name" : "name", 3 "email": "testuser@test.com", 4 "password": "testtest", 5 "grant_type": "password", 6 "client_id" : "{your_client_id}", 7 "client_secret": "{your_client_secret}", 8 "scope": "" 9}
すると、以下の内容がかえります。
json
1{ 2"token_type": "Bearer", 3"expires_in": 31535999, 4"access_token": "token", 5"refresh_token": "token" 6}
以上です。
投稿2018/07/19 03:37
総合スコア35
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。