Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
laravel中的Auth::login($user)无法登录该用户_Laravel_Authentication_Laravel 5.2_Laravel 5.3_Laravel 5.4 - Fatal编程技术网

laravel中的Auth::login($user)无法登录该用户

laravel中的Auth::login($user)无法登录该用户,laravel,authentication,laravel-5.2,laravel-5.3,laravel-5.4,Laravel,Authentication,Laravel 5.2,Laravel 5.3,Laravel 5.4,我使用的是laravel 5.4,Auth::login($user)显示类型错误: 传递给Illumb\Auth\SessionGuard::login()的参数1必须 实现接口Illumb\Contracts\Auth\Authenticatable, 给定的Illumb\Database\Elount\Builder实例,在中调用 /主页/vendor/laravel/framework/src/illumb/Auth/AuthManager.php on 第294行 我的User.php

我使用的是laravel 5.4Auth::login($user)显示类型错误:

传递给Illumb\Auth\SessionGuard::login()的参数1必须 实现接口Illumb\Contracts\Auth\Authenticatable, 给定的Illumb\Database\Elount\Builder实例,在中调用 /主页/vendor/laravel/framework/src/illumb/Auth/AuthManager.php on 第294行

我的User.php文件是:
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'role', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}
我的AuthController.php是:
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Auth;
use Illuminate\Support\Facades\Input;
use \Hash;

class AuthController extends Controller
{
//
public function register(Request $request){
    $this->validate($request, [
        'name' => 'required|max:30',
        'email' => 'required|email|unique:users',
        'regpassword' => 'required|alpha_num|confirmed'
        ]);

    /*$user = new User();
    $user['name'] = $request['name'];
    $user['email'] = $request['email'];
    $user['password'] = bcrypt($request['password']);
    $user['role'] = 'user';
    $user->save();
    */
    $user = User::create(array(
        'email' => Input::get('email'),
        'name' => Input::get('name'),
        'password' => Hash::make(Input::get('password')),
        'role' => 'user'));
    return 'success';


 }

public function userLogin(Request $request){
    $this->validate($request,[
        'email' => 'required|email',
        'password' => 'required' 
        ]);

    $user = User::where('email', '=', $request['email'])-> where('password' ,'=', Hash::make($request['password']))->where('role','=','user');                                                    

    if($user){
        Auth::login($user); 
        return $next($request);
    }
    else{
        return redirect()->back();
    }


}
}
My web.php

Route::get('/', function () {
return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');
Route::post('/register', 'AuthController@register');
Route::post('/UserLogin','AuthController@userLogin');
这:

不是您获取用户的方式

而不是

$user = User::where('email', '=', $request['email'])-> where('password' ,'=', Hash::make($request['password']))->where('role','=','user');                                                    

if($user){
    Auth::login($user); 
    return $next($request);
}
else{
    return redirect()->back();
}
您可以使用:

   $logged = auth()->attempt(
          ['email' => $request['email'], 
           'password' => $request['password'], 
           'role' => 'user']);

    if($logged) {
        return $next($request); // probably it won't work. This is fine in middleware but not in controller
    }
    else{
        return redirect()->back();
    }
你的问题是:

$user = User::where('email', '=', $request['email'])-> where('password' ,'=', Hash::make($request['password']))->where('role','=','user');
缺少从查询返回结果集的调用。添加
->first()

它说的是:

您给了我一个查询生成器的实例

但是

我想要一个扩展authenticatable类的模型

如果打开
App\User
,您将看到它确实扩展了此类:

class User extends Authenticatable {

解决我的问题如下:

$user = User::where('email', '=' $request['email'])->where('role','=','user')->first();
if(Hash::check($request['password'],$user->password)){
    Auth::login($user);
    return  'success';
}   

尝试使用Auth::尝试(['email'=>$email,'password'=>$password,'active'=>1]);如果(Auth::trunt(…))没有通过。如果(Auth::trunt(['email'=>$email,'password'=>$password]){//Authentication passed…return redirect()->designed('dashboard');}否则{//do the stuff}身份验证没有通过您是使用加密密码还是仅使用密码字符串进行尝试?我添加了->first()最后。之后,它甚至不会通过ifcondition@LakshyaGarg然后查询有问题。我想我的“密码”有问题。如果我仅通过电子邮件找到用户,它将成功返回,但如果通过电子邮件和密码找到用户,它将返回false。我已找到解决方案,并对其进行了更新。感谢您指出要添加->first()以获取用户模型对象。如果给定的凭据有效,则应该添加->first()。但正如我所评论的
return$next($request)在控制器中不起作用,您应该在其他地方执行(例如重定向到仪表板),我添加了返回“成功”;在if条件下,但在尝试登录后,它返回主页。@LakshyaGarg请确保您发送了有效数据,并且为用户设置了有效的角色。我已从数据库中对此进行了检查。我使用的是加密密码,这可能是原因吗?@LakshyaGarg在数据库中它应该在$request中加密。显然,您应该没有加密密码
$user = User::where('email', '=' $request['email'])->where('role','=','user')->first();
if(Hash::check($request['password'],$user->password)){
    Auth::login($user);
    return  'success';
}