sandip kakade
sandip kakade

Reputation: 1356

laravel authentication and jenssegers-mongodb not working

Hello i am using laravel, mongoDB and AdminLTE template in my project. for mongoDB i am using jenssegers/laravel-mongodb. But now i am facing error when i creating login authentication in laravel controller this my code in AuthController.php

   public function postLogin(Request $request)
   {
    $authUser = User::where('email', '=', $request->email)->first();
    if (isset($authUser)) {
        if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
            echo "success";exit;
        } else {
            echo "fail";exit;
        }
    } else {
        echo "fail";exit;
    }

In User.php code

    <?php

namespace App;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use DB;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticable as AuthenticableTrait;



class User extends Eloquent implements Authenticatable 
{
    protected $connection = 'mongodb';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

in User.php if i using only Eloquent like class User extends Eloquent so getting this error

Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given, called in /var/www/html/cams_alphaV1/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 378 and defined

if i using class User extends Eloquent implements Authenticatable so getting this error

Class App\User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifierName, Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifier, Illuminate\Contracts\Auth\Authenticatable::getAuthPassword, ...)

please help me i am totally stuck.

Upvotes: 2

Views: 7889

Answers (2)

Parisa
Parisa

Reputation: 801

as mentioned here, you can define your User class like this:

use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $collection = 'users';
}

the point is using Jenssegers\Mongodb\Auth\User instead of Illuminate\Foundation\Auth\User or any thing related to this one.

Upvotes: 3

thefallen
thefallen

Reputation: 9749

Since you're no longer using the Eloquent Model, your MongoDB user model must implement some interfaces and use some traits. This is for Laravel 5.2:

`use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
 use Illuminate\Auth\Authenticatable;
 use Illuminate\Auth\Passwords\CanResetPassword;
 use Illuminate\Foundation\Auth\Access\Authorizable;
 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
 use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Eloquent implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword; 
}`

Upvotes: 7

Related Questions