laravel身份验证和jenssegers-mongodb无法正常工作

时间:2016-05-19 12:31:02

标签: laravel-5 jenssegers-mongodb

您好我在项目中使用了laravel,mongoDB和AdminLTE模板。对于mongoDB,我正在使用jenssegers/laravel-mongodb。但是现在我在laravel控制器中创建登录身份验证时遇到错误,这是我在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;
    }

在User.php代码中

    <?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',
    ];
}
如果我只使用像class User extends Eloquent这样的Eloquent,那么在User.php中

就会出现此错误

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

如果我使用class User extends Eloquent implements Authenticatable,那么收到此错误

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, ...)
请帮助我,我完全陷入困境。

2 个答案:

答案 0 :(得分:5)

由于您不再使用Eloquent模型,因此您的MongoDB用户模型必须实现某些接口并使用某些特征。这是为了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; 
}`

答案 1 :(得分:0)

如上所述here,您可以像这样定义您的User类:

use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $collection = 'users';
}

重点是使用 Jenssegers\Mongodb\Auth\User 代替Illuminate\Foundation\Auth\User或与此相关的任何内容。