方法Illuminate \ Auth \ RequestGuard :: attempt不存在

时间:2018-02-28 09:53:19

标签: login lumen

我是laravel和流明的新手。 我在流明5.6中用oauth2.0创建了一个登录api,我已经安装了护照并生成了令牌。 下面是我的登录控制器功能,它工作正常。它返回令牌。

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);
        }  

由于我必须检查除用户名和密码之外的用​​户状态,我需要先检查用户凭据。所以我喜欢这个。

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.

2 个答案:

答案 0 :(得分:13)

功能防护仅适用于具有Web中间件的路由

public function login() {

  if(Auth::guard('web')->attempt(['email' => $email, 'password' => $password], false, false)) {requests
     // good
  } else {
    // invalid credentials, act accordingly
 }
}

答案 1 :(得分:0)

将默认防护更改为“网络”解决了我的问题。

config / auth.php:

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],