Method Illuminate\Auth\RequestGuard::attempt does not exist

故事扮演 提交于 2019-12-22 02:01:45

问题


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.

回答1:


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


来源:https://stackoverflow.com/questions/49026557/method-illuminate-auth-requestguardattempt-does-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!