Skip to content
Advertisement

laravel 8 – auth()->factory() { same refresh() and attemp() } is undefind in JWT AuthController

i use JWT for API authentication in laravel 8.

when i use auth()-> in my controller , for factory() or attemp() or anything , laravel does not know it and says :

Undefined method 'attempt'.intelephense(1013)

i don’t know what i forgot to include

my AuthController :

namespace AppHttpControllersApiAdmin;

use AppModelsUser;
use IlluminateHttpRequest;
use AppHttpControllersController;
use IlluminateSupportFacadesValidator;

class AuthController extends Controller
{
    
    public function __construct() {
        $this->middleware('auth:api', ['except' => ['login', 'register']]);
    }

    
    public function login(Request $request){
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required|string|min:6',
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

        if (! $token = auth()->attempt($validator->validated())) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->createNewToken($token);
    }
.
.
.
.
.

and my config/auth.php file :

return [

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


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
            'hash' => false,
        ],
    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsUser::class,
        ],
        // Following array is newly added to config
        'otp-user' => [
            'driver' => 'otp-based-auth-provider'
        ],


    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],


    'password_timeout' => 10800,

];


Advertisement

Answer

Intelephense has some trouble dealing with some methods in Laravel. You can either ignore it, as it should work or you can call it by:

JWTAuth::attempt($validator->validated) 

It should make the error go away.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement