【问题标题】:These credentials do not match our records, Laravel mongodb这些凭据与我们的记录不匹配,Laravel mongodb
【发布时间】:2022-02-17 00:55:48
【问题描述】:

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

config/database.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 installation and integrate with laravel

【问题讨论】:

  • 请出示您的登录密码
  • 嗨@aynber,生成的是laravel/ui的默认LoginController,我现在添加,但我并没有改变它。

标签: php laravel mongodb laravel-ui


【解决方案1】:

经过一天的搜索,我找到了解决方案,它只是要扩展的类,即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',
    ];

}

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 2017-05-14
    • 2015-06-09
    • 2020-06-16
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2016-06-27
    • 2022-10-04
    相关资源
    最近更新 更多