1

我有一个 Laravel 后端和 Vue 前端。在我的登录组件中,我对我的登录 Web 路由进行了 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 仍然给我这个错误?注册组件确实使用默认的 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);
//    }
}
4

0 回答 0