Laravel - auth()->refresh() is undefined in JWT AuthController?

Discussão em 'Angular' iniciado por Giuseppe The Dreamer, Janeiro 14, 2025.

  1. I'm creating a Laravel + Angular application, and I use JWT. (I follow the tutorial but I get an error in the middle).

    I already have a Laravel and Angular project, and when I created the laravel project I didn't specify any authentication (I even deleted the User migration).

    Since they are existing project, I don't want to start over. I ran the following commands to use JWT in my angular project:

    composer require tymon/jwt-auth:dev-develop --prefer-source
    php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
    php artisan jwt:secret


    After that I copied the default router to api.php:

    Route::post('login', 'ApiController@login');
    Route::post('register', 'ApiController@register');

    Route::group(['middleware' => 'auth.jwt'], function () {
    Route::get('logout', 'ApiController@logout');
    Route::get('user', 'ApiController@getAuthUser');

    });


    And extended the existing User.php:

    class User extends Authenticatable implements JWTSubject


    And I copied the required fields into config/auth.php.

    In the next step I had to copy this into AuthController.php:

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Support\Facades\Auth;
    use App\Http\Controllers\Controller;

    class AuthController extends Controller
    {
    /**
    * Create a new AuthController instance.
    *
    * @return void
    */
    public function __construct()
    {
    $this->middleware('auth:api', ['except' => ['login']]);
    }

    /**
    * Get a JWT via given credentials.
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function login()
    {
    $credentials = request(['email', 'password']);

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

    return $this->respondWithToken($token);
    }

    /**
    * Get the authenticated User.
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function me()
    {
    return response()->json(auth()->user());
    }

    /**
    * Log the user out (Invalidate the token).
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function logout()
    {
    auth()->logout();

    return response()->json(['message' => 'Successfully logged out']);
    }

    /**
    * Refresh a token.
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function refresh()
    {
    return $this->respondWithToken(auth()->refresh());
    }

    /**
    * Get the token array structure.
    *
    * @param string $token
    *
    * @return \Illuminate\Http\JsonResponse
    */
    protected function respondWithToken($token)
    {
    return response()->json([
    'access_token' => $token,
    'token_type' => 'bearer',
    'expires_in' => auth()->factory()->getTTL() * 60
    ]);
    }
    }


    In the public function refresh() method it gives me the following error: "Undefined method 'refresh'. intelephense(1013)" (The same for factory)

    Why are they undefined? Do I have to include something to the class?

    Continue reading...

Compartilhe esta Página