[英]Laravel: Trying to get property 'id' of non-object for Auth::User()->id;
I have the following login controller:我有以下登录controller:
public function userLogin(Request $request) {
$rules = [
'email' => 'required|exists:users',
'password' => 'required'
];
$request->validate($rules);
$data = [
'email' => $request->get('email'),
'password' => $request->get('password'),
];
if(Auth::attempt($data))
{
$user = Auth::User();
$userid = Auth::User()->id;
return response()->json([
'user' => $user,
'token' => $user->createToken('yourAppName')->accessToken ,
$userid
]);
}
else
{
return response()->json('Unauthorized', 401);
}
}
My web.php
( Token is generated with Laravel Passport )我的
web.php
(令牌是用 Laravel 护照生成的)
Route::get('/customize/{id}', function ($id) {
if (
User::where('id', Auth::User()->id)->whereHas('profile', function ($query) use ($id) {
return $query->where('id', $id);
})
->exists()
) {
return view( 'welcome' );
}
return "no";
});
For some reason, when testing my logincontroller
with POSTMAN , I actually receive a value for Auth::User()->id
.出于某种原因,在使用
logincontroller
测试我的登录控制器时,我实际上收到了Auth::User()->id
的值。 But in web.php
I receive Trying to get property 'id' of non-object
.但是在
web.php
我收到Trying to get property 'id' of non-object
。 Any idea?任何想法?
here is a solution这是一个解决方案
you can find that user in db with email and password you have and get its id您可以使用 email 和您拥有的密码在 db 中找到该用户并获取其 ID
and then use LoginUsingId($id) to login your user然后使用 LoginUsingId($id) 登录您的用户
Find out why you are not considered as an authenticated user.找出为什么您不被视为经过身份验证的用户。 I can guess you send Authentication token in your request headers when you use Postman.
我猜你在使用 Postman 时会在请求标头中发送身份验证令牌。 that this is not possible when you hit the address in the browser directly.
当您直接在浏览器中点击地址时,这是不可能的。
can u replace Auth::User()->id
to Auth::user()->id
maybe uppercase may be causing a problem您可以将
Auth::User()->id
替换为Auth::user()->id
也许大写可能会导致问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.