【发布时间】:2021-03-08 17:45:26
【问题描述】:
我正在使用 apache2 和 php artisan serve 命令来测试我的项目。
拥有下一个控制器:
<?php
// file: Http\Controllers\AuthCtrl.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User; // <--
use Illuminate\Support\Facades\Auth;
class AuthCtrl extends Controller
{
public function login(Request $request)
{
$rules = [ 'email' => 'required|exists:users',
'password' => 'required' ];
$request->validate($rules);
$data = [ 'email' => $request->get('email'),
'password' => $request->get('password') ];
if( Auth::attempt($data) )
{
$user = Auth::user();
// the $user->createToken('appName')->accessToken generates the JWT token that we can use
return response()->json([
'user' => $user, // <- we're sending the user info for frontend usage
'token' => $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
'success' => true
]);
}
else
{
return response()->json([
'success' => false,
'msg' => 'Credenciales erroneas.'
]);
}
}
}
还有一个简单的模拟 index.php,它使用以下 js 进行 ajax 调用:
function ExecLogin()
{
$.ajax({
method: "POST",
url: "http://127.0.0.1:8000/api/login",
data : {email : "fsd@gmail.com", password: "mypass"}
}).done(
function(data, status)
{
if(data.success == true)
{
localStorage.setItem('appname_token', data.token);
// the following part makes sure that all the requests made later with jqXHR will automatically have this header.
$( document ).ajaxSend(function( event, jqxhr, settings ) {
jqxhr.setRequestHeader('Authorization', "Bearer " + data.token);
});
}
else
{
alert(data.msg);
}
}).fail(function(error){
// handle the error
});
}
如您所见,当代码进入这部分时,(当用户认证OK时),它返回一个JSON响应就可以了:
$user = Auth::user();
// the $user->createToken('appName')->accessToken generates the JWT token that we can use
return response()->json([
'user' => $user, // <- we're sending the user info for frontend usage
'token' => $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
'success' => true
]);
但是当没有认证OK的时候,在这部分进入,但是响应的是index.php text/html,而不是我定义的json响应:
return response()->json([
'success' => false,
'msg' => 'Credenciales erroneas.'
]);
我在 ajax 调用的数据对象上收到以下字符串:
"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 13px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="top-right links">
<a href="http://127.0.0.1:8000/login">Login</a>
<a href="http://127.0.0.1:8000/register">Register</a>
</div>
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="links">
<a href="https://laravel.com/docs">Docs</a>
<a href="https://laracasts.com">Laracasts</a>
<a href="https://laravel-news.com">News</a>
<a href="https://blog.laravel.com">Blog</a>
<a href="https://nova.laravel.com">Nova</a>
<a href="https://forge.laravel.com">Forge</a>
<a href="https://vapor.laravel.com">Vapor</a>
<a href="https://github.com/laravel/laravel">GitHub</a>
</div>
</div>
</div>
</body>
</html>
"
我在这里遗漏了什么重要的东西吗?
身份验证中间件如下所示:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson() ) {
return route('login');
}
}
}
【问题讨论】:
-
您是如何检查响应 id text/html 的?您是否查看了对
/api/login的响应或ajax 调用?检查当带有无效凭据的发布请求发送到/api/login时返回的响应是什么 - 在 devtools 的网络选项卡中并通过控制台登录 index.php 的 js sn-p -
我就是这么做的,它返回 index.php 的 html 页面。让我把它放在这个问题上。
-
就是这样,我以前在生产服务器上从来没有遇到过这个问题,所以我想这一定是本地开发的 php artisan 服务。
-
查看
App\Http\Middleware\AuthenticateredirectTo()的内容是什么 -
受保护的函数 redirectTo($request) { if (!$request->expectsJson() ) { return route('login'); } }