我使用的是Laravel7, Passport package和Default Auth Controllers。在Web上,登录和注册工作正常,但在api上产生错误。
Api.php
Route::group([
'prefix' => 'auth'
], function () {
Route::post('login', 'AuthLoginController@login');
Route::post('register', 'AuthRegisterController@create');
Route::group(['middleware' => 'auth:api'], function() {
Route::get('logout', 'AuthController@logout');
Route::get('user', 'AuthController@user');
});
});
RegisterController.php(在默认Auth文件夹内)
protected function create(array $data)
{
return User::create([
'first_name' => $data['name'],
'last_name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'user_type'=> "abc",
]);
}
Api URL为http://127.0.0.1:8000/api/auth/register
try this -
protected function create(array $data)
{
return json_encode(User::create([
'first_name' => $data['name'],
'last_name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'user_type'=> "abc",
])->toArray());
}