gpt4 book ai didi

laravel-5 - laravel 身份验证和 jenssegers-mongodb 不工作

转载 作者:行者123 更新时间:2023-12-04 03:11:08 25 4
gpt4 key购买 nike

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

在 User.php 中,如果我只使用像 class User extends Eloquent 这样的 Eloquent,那么就会出现这个错误

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, ...)

请帮助我,我完全卡住了。

最佳答案

由于您不再使用 Eloquent 模型,您的 MongoDB 用户模型必须实现一些接口(interface)并使用一些特征。这是针对 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;
}`

关于laravel-5 - laravel 身份验证和 jenssegers-mongodb 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37323300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com