0

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 ?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Januar
  • 49
  • 6

1 Answers1

0

Here is the answer:

In your Customer model, make sure you're using the Jenssegers\Mongodb\Eloquent\Model class as the base class. Update your use statement like this:

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

Also, ensure that the email field 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.