Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

login

lumen

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.
like image 935
Sanju Kaniyamattam Avatar asked Feb 28 '18 09:02

Sanju Kaniyamattam


1 Answers

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
 }
}
like image 58
Samuel Dervis Avatar answered Sep 30 '22 14:09

Samuel Dervis