Kboyz
Kboyz

Reputation: 189

Lumen 5.8 Auth attempt does not exist

I'm new to Lumen and building a RESTful API and I got stuck for how many hours now because Auth::attempt is not working. I've been searching for any answers but all the results are only Laravel not Lumen.

So I created a AuthController and login method to authenticate user but I got an error.

public function login(Request $request)
{
        $this->validate($request, [
            'email' => 'required|string|email',
            'password' => 'required|string',
        ]);

        $credentials = $request->only('email', 'password');
        if( !Auth::attempt($credentials) ) {
            return response()->json([
                'message' => 'Unauthorized'
            ], 401);
        }
}

This is the error: "Method Illuminate\Auth\RequestGuard::attempt does not exist."

Anyone can help me? Thank you!

Upvotes: 2

Views: 2141

Answers (2)

Entrepreneur AJ
Entrepreneur AJ

Reputation: 63

Lumen Passport Setup:

composer require dusterio/lumen-passport

Enable the app and auth service providers in bootstrap/app.php

add the following service providers in the bootstrap file

$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);

replace the contents inside the boot function of the auth service provider in /app/Providers/AuthServiceProvider with:

LumenPassport::routes($this->app->router);

Run php artisan migrate

run php artisan passport:install

Then follow this https://laravel.com/docs/5.8/passport#issuing-access-tokens

remeber the lumen version does not have views or routes for authorising access you have to build them seperatly on your client.

Upvotes: 0

utdev
utdev

Reputation: 4102

Can you try following:

   if (!Auth::guard('web')->attempt($credentials) {
        return response()->json([
            'message' => 'Unauthorized'
        ], 401);
    }

The method is only available for routes which are using the web middleware, could you check that out? You may need to edit your config/auth file.

Upvotes: 0

Related Questions