php - Laravel登录 - 失败条件不起作用
问题描述
我正在 Laravel 中创建一个登录 api。
我有登录方法
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return response()->json(auth()->user());
}
return Response::json(['message' => 'Failed to authenticate'], 401);
}
如果登录失败,则不会出现 401 错误。有什么理由吗?
解决方案
尝试
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return response()->json(auth()->user());
}
else {
return Response::json(['message' => 'Failed to authenticate'], 401);
}
}
我也希望dd()
你auth()
确保事情按预期进行。你在传递你的 CSRF 令牌吗?
你也有你Response
使用使用../../Response加载的吗?在控制器的顶部?
推荐阅读
- c++ - boost odeint 何时真正调用观察者?
- php - 从数据库php检查密码
- mapbox - 在 MapBox 中获取标记特征实例
- php - 使用 PHP 进行表单和 SQL 验证
- javascript - 从 Three.js 中 Vector 的 z 坐标派生的顶点颜色
- python - flow_from_directory 可以从 Keras 的同一目录中获取训练和验证数据吗?
- r - 如何使用主 data.frame 将 anti_join 应用于 data.frames 列表
- java - Spring:表已经存在错误!@DirtiesContext 和 EmbeddedDatabaseBuilder 不能一起工作吗?
- java - 转换浮点数组
- rxjs - Angular 6 多个 HTTP 请求 RxJs