18

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.
Sanju Kaniyamattam
  • 501
  • 1
  • 4
  • 14

3 Answers3

20

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
 }
}
Samuel Dervis
  • 366
  • 1
  • 9
  • the problem is package functions like spatie/laravel-permission work on api so i think we should use mix of api and web for RESTFUL applications – ghazyy Dec 07 '18 at 05:48
  • Note: When using the @Samuel Dervis solution do not forget to remove your login route from: `'middleware' => 'auth: api'`, if it's inside. The guard method also accepts the api parameter, as `Auth::guard('api')` but I currently encounter difficults when using it. – Fellipe Sanches Jun 15 '20 at 21:07
  • In my case, my controller class was named `Auth`. So I had to create an alias for the `Illuminate\Support\Facades\Auth`. – Wesley Gonçalves Aug 03 '20 at 14:47
  • life saver thanks, I just created a new method with this to avoid damaging the api auth method I'm currently using, and worked like a charm – ccrez Nov 01 '21 at 12:53
3

Changing default guard to "web" fixed my problem.

config/auth.php:

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
Faridul Khan
  • 1,741
  • 1
  • 16
  • 27
1

using jwt it's worked for me

1-Auth::attempt($credentials)

just replace line 1 to line 2

2-$token = Auth::guard('api')->attempt($credentials)