0

I'm using Laravel 8 and I install MongoDB v5.0.6 using jenssegers/mongodb package, and the communication was good. I run the migration and I add a new user via the register page. But when I try to login using the same credentials that I use on the register page, this error appears 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 with jenssegers/mongodb integration

<?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 (generated via 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']),
    ]);
}

LoginController.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');
}
}

I think maybe the MongoDB package does a hash or something to the password but I can't find where.

PS: it works fine when I switch to MYSQL

I follow this tutorial Mongodb installation and integrate with laravel.

Malki Mohamed
  • 1,578
  • 2
  • 23
  • 40

1 Answers1

0

After a day of searching I find out the solution, it's just about the class to extend, which is the Authenticatable of Jenssegers\Mongodb package. This is what my User.php class becomes:

<?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',
    ];

}

Malki Mohamed
  • 1,578
  • 2
  • 23
  • 40