我有一个Laravel后端和Vue前端。在我的登录组件中,我对登录网络路由进行了axios调用:
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')
};
axios
.post('/login', this.$data.loginForm)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error);
});
当我记录表单数据时,我看到以下内容:
email: john@example.com
password: password
这是将其发送到LoginController
的正确格式,因为我在工作RegisterController / Register component
中以相同的方式使用它。凭据也与数据库中的记录匹配。但是,我收到此错误:
POST http://127.0.0.1:8000/login 422 (Unprocessable Entity)
当我查看为什么会给我这个错误时,它说:
{message: "The given data was invalid.",…}
errors: {email: ["These credentials do not match our records."]}
email: ["These credentials do not match our records."]
0: "These credentials do not match our records."
message: "The given data was invalid."
因此,我尝试通过执行以下操作来覆盖AuthenticatesUsers
特性的默认方法,以查看请求发送给控制器的内容:
protected function credentials(Request $request)
{
dd($request);
}
它还会返回:
email: john@example.com
password: password
与数据库中的记录相同。如果凭据与数据库中的凭据相同,为什么Laravel仍然给我这个错误? register组件确实使用默认的RegisterController创建了一个用户,并且我发送数据的登录方式与注册时相同。有人可以向我解释为什么它总是这么说吗?
更新:
这是我的用户模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
这是我完整的LoginController:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
// protected function credentials(Request $request)
// {
// dd($request);
// }
}