ryank30's avatar

Laravel mongodb authentication failed

I have an authentication failed issue with mongodb 3.2 and laravel 5.2

I have tested it with just php and mongodb php library like below.

require 'vendor/autoload.php'; $uripotions = array(); $uripotions['username'] = 'myuser'; $uripotions['password'] = 'mypassword'; $client = new MongoDB\Client("mongodb://localhost:27017", $uripotions); $collection = $client->client_db->accounts;

$result = $collection->find(); foreach($result as $item){ echo $item['first_name']; }

And it worked.

However, it doesn't work on laravel 5.2 after following the steps that jenssegers/laravel-mongodb mentions.

I have added the service provider class to config/app.php and added my mongodb config in config/database.php like below.

'mongodb' => [
    'driver'   => 'mongodb',
    'host'     => env('DB_HOST', 'localhost'),
    'port'     => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE', 'client_db'),
    'username' => env('DB_USERNAME', 'myuser'),
    'password' => env('DB_PASSWORD', 'mypassword'),
    'options' => [
        'db' => 'admin'
    ]
],   

In my controller, I put a script like below:

class IntroController extends Controller { public function index(Account $account) { $user = $account->where('first_name', 'ryan')->first(); return $user; } }

In the Account model:

namespace App; use Jenssegers\Mongodb\Eloquent\Model as Eloquent; class Account extends Eloquent { protected $connection = 'mongodb'; protected $collection = 'accounts'; protected $fillable = ['first_name', 'last_name', 'email']; }

I get an error message like below:

AuthenticationException in Find.php line 179: Authentication failed.

It would be appreciated if you could help me with this issue.

Thanks,

0 likes
3 replies
asadsajjad's avatar

Hello ryank,

use this in your Model instead ... :)

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

class User extends Eloquent implements Authenticatable { use AuthenticableTrait;

// your code

}

stardesign041@gmail.com's avatar

I have just changed the below database -

//'default' => 'mysql', 'default' => 'mongodb', ===Function Code==

protected function addCookieToResponse($request, $response) { $config = config('session');

    $response->headers->setCookie(
        new Cookie(
            'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']),
            $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null
        )
    );

    return $response;
}

Then after this it shows the below error

(1/1) ErrorException Trying to get property 'headers' of non-object

in VerifyCsrfToken.php line 158 at HandleExceptions->handleError(8, 'Trying to get property 'headers' of non-object', 'E:\xampp\htdocs\automobile_mongo\application\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php', 158, array('request' => object(Request), 'response' => null, 'config' => array('driver' => 'database', 'lifetime' => 120, 'expire_on_close' => false, 'encrypt' => true, 'files' => 'E:\xampp\htdocs\automobile_mongo\application\storage\framework/sessions', 'connection' => null, 'table' => 'sessions', 'store' => null, 'lottery' => array(2, 100), 'cookie' => 'laravel_session', 'path' => '/', 'domain' => null, 'secure' => false, 'http_only' => true)))in VerifyCsrfToken.php line 158 at VerifyCsrfToken->addCookieToResponse(object(Request), null)in VerifyCsrfToken.php line 67 at VerifyCsrfToken->handle(object(Request), object(Closure))in Pipeline.php line 149 at Pipeline->Illuminate\Pipeline{closure}(object(Request))in Pipeline.php line 53 at Pipeline->Illuminate\Routing{closure}(object(Request))in ShareErrorsFromSession.php line 49 at ShareErrorsFromSession->handle(object(Request), object(Closure))in Pipeline.php line 149 at Pipeline->Illuminate\Pipeline{closure}(object(Request))in Pipeline.php line 53 at Pipeline->Illuminate\Routing{closure}(object(Request))in StartSession.php line 65 at StartSession->handle(object(Request), object(Closure))in Pipeline.php line 149 at Pipeline->Illuminate\Pipeline{closure}(object(Request))in Pipeline.php line 53 at Pipeline->Illuminate\Routing{closure}(object(Request))in AddQueuedCookiesToResponse.php line 37 at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))in Pipeline.php line 149 at Pipeline->Illuminate\Pipeline{closure}(object(Request))in Pipeline.php line 53 at Pipeline->Illuminate\Routing{closure}(object(Request))in EncryptCookies.php line 59 at EncryptCookies->handle(object(Request), object(Closure))in Pipeline.php line 149 at Pipeline->Illuminate\Pipeline{closure}(object(Request))in Pipeline.php line 53 at Pipeline->Illuminate\Routing{closure}(object(Request))in Pipeline.php line 102 at Pipeline->then(object(Closure))in Router.php line 660 at Router->runRouteWithinStack(object(Route), object(Request))in Router.php line 635 at Router->runRoute(object(Request), object(Route))in Router.php line 601 at Router->dispatchToRoute(object(Request))in Router.php line 590 at Router->dispatch(object(Request))in Kernel.php line 176 at Kernel->Illuminate\Foundation\Http{closure}(object(Request))in Pipeline.php line 30 at Pipeline->Illuminate\Routing{closure}(object(Request))in CheckForMaintenanceMode.php line 46 at CheckForMaintenanceMode->handle(object(Request), object(Closure))in Pipeline.php line 149 at Pipeline->Illuminate\Pipeline{closure}(object(Request))in Pipeline.php line 53 at Pipeline->Illuminate\Routing{closure}(object(Request))in Pipeline.php line 102 at Pipeline->then(object(Closure))in Kernel.php line 151 at Kernel->sendRequestThroughRouter(object(Request))in Kernel.php line 116 at Kernel->handle(object(Request))in index.php line 59

Please or to participate in this conversation.