我正在使用laravel 5.4,Auth::login($user)显示类型错误:
传递给Illumb\Auth\SessionGuard::login()的参数1必须实现接口Illumb\Contracts\Authenticatable,给定的Illumb\Database\Eloquent\Builder实例,在第294行的/home/vendor/laravel/framework/src/Illumb/Auth/AuthManager.php中调用
我的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();
}
}
}
我的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('role','=','user')->first();
if(Hash::check($request['password'],$user->password)){
Auth::login($user);
return 'success';
}
这:
$user = User::where('email', '=', $request['email'])-> where('password' ,'=', Hash::make($request['password']))->where('role','=','user');
不是您获取用户的方式。
而不是
$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');
缺少从查询中获取结果集的调用。添加-
它说的是:
您给了我一个查询生成器的实例
但是
我想要一个扩展authenticatable类的模型
如果打开
App\User
,您将看到它确实扩展了此类:
class User extends Authenticatable {
我是新来的laravel。我试图登录用户,但我得到以下错误每次: SessionGuard.php第439行中的ErrorException:传递给Illumb\Auth\SessionGuard::login()的参数1必须是Illumb\Contracts\Auth\Authenticatable的实例,数组给定 这是我的表格: 这是我的路线: 和用户控制器中的登录方法: 谢谢你的帮助
当用户登录时,被设置,但是返回null。 这意味着我必须在需要访问用户的属性或方法的任何地方这样做: 有什么更好的解决方案?我是否可以在BaseController中设置,然后通过
在我的Laravel 5.8/vuejs 2.6应用程序中,我使用了“tymon/jwt auth”:“^1.0.0”,我的应用程序/Http/Controllers/AuthController.php具有以下方法: 我把令牌放在客户端。它可以工作,但我想在保存数据时在服务器端添加更多检查,并在控件的方法中执行以下操作: 但是,即使我已经登录到系统,在add_new_线程方法中,$loggedU
我使用的是Laravel5.0。我只想使用用户名(无密码)登录,因为我正在使用单点登录访问我的Laravel项目,因此如果用户单点登录成功,它将检查我的数据库,以确定登录用户的用户名是否已注册。 我已经找了一整天,但我仍然找到了线索。 我有一个错误: 传递给Illumb\Auth\Guard::login()的参数1必须实现接口Illumb\Contracts\Auth\Authenticatab
我在Laravel中创建了多重身份验证。当我使用用户登录时,在post login方法中调试时,在Auth::guard('user')之后- 如何解决?我是拉威尔的初学者。 谢谢!!! /路由/auth/user。php }); /路由/网络。php }); /app/Controllers/User/Auth/LoginController-@post登录 /应用程序/控制器/用户/家庭控制器
我搜索并找到了如下各种结果:auth()- 但是,我的仍然不起作用。 在控制器中工作,但在模型中不工作。它返回。 代码是: 我尝试,它也返回。 任何想法?