【发布时间】:2019-08-02 07:03:31
【问题描述】:
请看下面的 UserController 代码。我正在尝试通过 api 使用 VUEJS 。所以我创建了 在 api.php 注册和登录的路由。用于测试我尝试通过 postman 和 http://localhost:8000/api/register 和 Accepted application/json 和 Content-type with application/json 传递数据。我通过正文将数据作为表单数据传递。问题是,当我尝试发送数据时,邮递员连续发送数据而没有响应,并且当我取消请求时,端口 8000 不再起作用。所以我必须停止服务器并再次运行。谁能告诉我我的代码中是否有任何错误。我使用的是 laravel 5.7
<?php
namespace App\Http\Controllers\Api;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
public function register(Request $request){
$request->validate([
'name' => 'required',
'email'=> 'required',
'password'=> 'required'
]);
//add some cde that what happen if validation fils
$newUser = User::firstOrNew(['email'=> $request->email]);
$newUser->name = $request->name;
$newUser->email =$request->email;
$newUser->password = bcrypt($request->password);
$newUser->save();
$http = new Client;
$response = $http->post(url('oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => '2',
'client_secret' => '9J0sU4Ctz5p3AUz7ROiv7jELnGrU5waepprqICyH',
'username' => $request->email,
'password' => $request->password,
'scope' => '',
],
]);
return response(['data' => json_decode((string)$response->getBody(), true) ]) ;
}
public function login(Request $request){
$request->validate([
'email'=> 'required',
'password'=> 'required'
]);
$user = User::where('email', $request->email)->first();
if(!$user){
return response(['status' => 'error' , 'message'=> 'user not found']);
}
if(Hash::check($request->password, $$user->password)){
$http = new Client;
$response = $http->post(url('oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => '2',
'client_secret' => '9J0sU4Ctz5p3AUz7ROiv7jELnGrU5waepprqICyH',
'username' => $request->email,
'password' => $request->password,
'scope' => '',
],
]);
return response(['data' => json_decode((string)$response->getBody(), true)]);
}
}
}
【问题讨论】:
标签: php laravel postman laravel-passport