Php 向laravel注册新用户作为API
我正在构建一个Angular应用程序,它依赖于一个单独的LaravelAPI,并且正在尝试注册一个新用户 现成的Laravel 5.1提供了许多方便的方法来注册用户,但它们适合在整个应用程序中使用Laravel,这意味着Php 向laravel注册新用户作为API,php,angularjs,laravel,authentication,redirect,Php,Angularjs,Laravel,Authentication,Redirect,我正在构建一个Angular应用程序,它依赖于一个单独的LaravelAPI,并且正在尝试注册一个新用户 现成的Laravel 5.1提供了许多方便的方法来注册用户,但它们适合在整个应用程序中使用Laravel,这意味着blade templatefrontend 我在routes.php文件中添加了以下内容: $router->controllers([ 'auth' => 'Auth\AuthController', 'password' => 'Auth\
blade template
frontend
我在routes.php文件中添加了以下内容:
$router->controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);
路由/auth/register
是为我提供的,因此我尝试创建一个新用户:
来自DHC:
我们很抱歉,但ChromeAPI不允许获取对的响应正文
重定向
从我的应用程序:
无法加载XMLHttpRequest。这个
请求已重定向到“”,这是
不允许用于需要飞行前的跨来源请求
我发现这是因为方法/auth/register
调用了registerusers.php中的postRegister()
:
App\Http\Controllers\Auth\AuthController@postRegister
这看起来像:
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
这有一个重定向,我想通常会重定向到blade欢迎模板或其他东西
如果我注释掉重定向,只返回一个200响应
return Response::make('user created', 200);
我得到一个CORS错误:
。请求的资源上不存在“Access Control Allow Origin”标头
这很奇怪,因为我通过BarryCORS中间件路由所有路由:
Route::group(['middleware' => 'cors'], function(\Illuminate\Routing\Router $router) {
上面说允许使用*
来源
所以问题是:如何为仅使用Laravel的API的前端应用程序设置注册和身份验证
对于来自我的前端应用程序的每个调用,似乎都有两个对register
的调用:
First:第一个请求似乎有访问控制头,它是一个选项http请求:
Request URL:http://dde.localhost/auth/register
Request Method:OPTIONS
Status Code:200 OK
//Response Headers:
access-control-allow-headers:ACCEPT, CONTENT-TYPE
access-control-allow-methods:GET, POST, PUT, DELETE
access-control-allow-origin:http://localhost:1337
Allow:GET,HEAD,POST,PUT,PATCH,DELETE
第二次:在第二次调用中,最终调用POST,并使用有效负载,但访问控制头被剥离
Request URL:http://dde.localhost/auth/register
Request Method:POST
Status Code:500 Internal Server Error
//Response Headers: there are no access-control-allow methods attached
//Payload
{username: "testname", password: "some random password"}
这是为什么?在CORS调用之前,OPTIONS请求是标准的,它正是为了检查服务器的响应头
如果您得到的是HTTP 500,那是因为服务器有问题。如果仅仅是CORS,你会得到40倍的误差。
检查您的laravel应用程序日志,服务器端出现问题。您是否检查了响应是否确实包含这些标题?最好使用基于令牌的身份验证。@fos.alex请参见上面的编辑plz