I'm using my own collection named customer instead of users in this case.
Model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
// use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Auth\User as Authenticatable;
class Customer extends Authenticatable
{
use HasFactory;
protected $collection='customer';
protected $guarded = ['id'];
public $timestamps = false;
}
Controller
namespace App\Http\Controllers;
use App\Models\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\Authenticatable;
class LoginController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('login', ["title" => "LOGIN"]);
}
public function validation(Request $request)
{
$credentials = $request->validate([
'email' => ['required', 'email'],
'password' => ['required'],
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('dashboard');
}
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
])->onlyInput('email');
}
}
config/auth.php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
config/database.php
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_URI', 'mongodb://localhost:27017'),
'database' => 'herbal66',
],
.env
DB_CONNECTION=mongodb
DB_URI=mongodb://localhost:27017
It always return 'The provided credentials do not match our records.'.
What should i do ?
Here is the answer:
In your
Customermodel, make sure you're using theJenssegers\Mongodb\Eloquent\Modelclass as the base class. Update yourusestatement like this:Also, ensure that the
emailfield in the MongoDB collection is being stored as plain text (not hashed) since Laravel's default authentication expects the password to be hashed, but you're using MongoDB and might have different conventions.If the issue persists, try debugging by checking if the email and password values are being fetched and passed correctly to the
Auth::attempt()function.