本文介绍了方法Illuminate \ Auth \ RequestGuard :: attempt不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Laravel和Lumen都是陌生的.我在lumen 5.6中使用oauth2.0创建了一个登录api,我已经安装了通行证并生成了令牌.以下是我的登录控制器功能,可以正常工作.它返回令牌.
I am new to both laravel and lumen.I was creating a login api with oauth2.0 in lumen 5.6, i have installed passport and generated token.Below is my login controller function and it is working fine. It returns token.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
//use Illuminate\Support\Facades\DB;
use App\User;
use Auth;
public function login(Request $request)
{
global $app;
$proxy = Request::create(
'/oauth/token',
'post',
[
'grant_type' => env('API_GRAND_TYPE'),
'client_id' => env('API_CLIENT_ID'),
'client_secret' => env('API_CLIENT_SECRET'),
'username' => $request->username,
'password' => $request->password,
]
);
return $app->dispatch($proxy);
}
由于除了用户名和密码外,我还必须检查用户状态,因此我需要首先检查用户凭据.所以我确实喜欢这个.
Since i have to check user status apart from username and password, i need to check the user credential first. so i do like this.
public function login(Request $request)
{
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials)) {
return ['result' => 'ok'];
}
return ['result' => 'not ok'];
}
Here i am getting this error.
Method Illuminate\Auth\RequestGuard::attempt does not exist.
So i tried Auth::check instead of Auth::attempt.
Now there is no error but it always return false even though the credentials are valid.
I searched a lot for a solution but i didn't get.
推荐答案
功能防护仅适用于具有Web中间件的路由
The function guard is only available for routes with web middleware
public function login() {
if(Auth::guard('web')->attempt(['email' => $email, 'password' => $password], false, false)) {requests
// good
} else {
// invalid credentials, act accordingly
}
}
这篇关于方法Illuminate \ Auth \ RequestGuard :: attempt不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!