0

我正在使用 Laravel 8 并使用jenssegers/mongodb包安装 MongoDB v5.0.6,并且通信良好。我运行迁移并通过注册页面添加新用户。但是,当我尝试使用在注册页面上使用的相同凭据登录时,会出现此错误These credentials do not match our records

配置/数据库.php

'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE'),
        'username' => env('DB_USERNAME'),
        'password' => env('DB_PASSWORD'),
        'options' => [
            'database' => env('DB_DATABASE') // set the authentication database
        ]
    ]

Models/User.php 与jenssegers/mongodb集成

<?php

 namespace App\Models;

 use Illuminate\Contracts\Auth\MustVerifyEmail;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 //use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Notifications\Notifiable;
 use Laravel\Sanctum\HasApiTokens;

 use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
 use Illuminate\Auth\Authenticatable as AuthenticableTrait;
 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

 class User extends Eloquent implements AuthenticatableContract
{
use AuthenticableTrait;
use HasFactory,HasApiTokens,Notifiable;

protected $connection = 'mongodb';

/**
 * The attributes that are mass assignable.
 *
 * @var array<int, string>
 */
protected $fillable = [
    'name',
    'email',
    'password',
];

/**
 * The attributes that should be hidden for serialization.
 *
 * @var array<int, string>
 */
protected $hidden = [
    'password',
    'remember_token',
];

/**
 * The attributes that should be cast.
 *
 * @var array<string, string>
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

/**
 * Get the name of the unique identifier for the user.
 *
 * @return string
 */
public function getAuthIdentifierName()
{
    // TODO: Implement getAuthIdentifierName() method.
}

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    // TODO: Implement getAuthIdentifier() method.
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    // TODO: Implement getAuthPassword() method.
}

/**
 * Get the token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken()
{
    // TODO: Implement getRememberToken() method.
}

/**
 * Set the token value for the "remember me" session.
 *
 * @param  string $value
 * @return void
 */
public function setRememberToken($value)
{
    // TODO: Implement setRememberToken() method.
}

/**
 * Get the column name for the "remember me" token.
 *
 * @return string
 */
public function getRememberTokenName()
{
    // TODO: Implement getRememberTokenName() method.
}
}

RegisterController.php(通过 Laravel/ui 生成)

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

登录控制器.php

<?php

 namespace App\Http\Controllers\Auth;

 use App\Http\Controllers\Controller;
 use App\Providers\RouteServiceProvider;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;

 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 = RouteServiceProvider::HOME;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest')->except('logout');
}
}

我想也许 MongoDB 包对密码进行了哈希或其他操作,但我找不到在哪里。

PS:当我切换到 MYSQL 时它工作正常

我按照本教程Mongodb 安装并与 laravel 集成

4

1 回答 1

0

经过一天的搜索,我找到了解决方案,它只是要扩展的类,即Jenssegers\Mongodb包的 Authenticatable。这就是我的 User.php 类变成的样子:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{

    use HasApiTokens,HasFactory, Notifiable;


    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'api_token',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

}

于 2022-02-16T16:55:48.037 回答