dingo / api

A RESTful API package for the Laravel and Lumen frameworks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JWT doesn't work

ztj1993 opened this issue · comments

commented
Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 7.x.y
Package version 3.x.y
PHP version 7.x.y

Actual Behaviour

Describe the behaviour you're experiencing. Do not just copy and paste a random error message and expect help.

JWT doesn't work

ServiceProvider boot:

        $this->app['api.auth']->extend('jwt', function ($app) {
            return new \Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
        });

or

        app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
            return new \Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
        });

config auth.php:

            'guards' => [
                'jwt' => [
                    'driver' => 'jwt',
                    'provider' => 'member',
                ],
            ],
            'providers' => [
                'member' => [
                    'driver' => 'eloquent',
                    'model' =>App\UserModel::class,
                ],
            ],

controller:

    public function __construct()
    {
        parent::__construct();
        $this->middleware('api.auth', ['except' => ['login']]);
    }

    public function login()
    {
        $credentials = request(['name', 'password']);
        if (!$token = auth('jwt')->attempt($credentials)) {
            return $this->response->errorUnauthorized();
        }
        return $this->respondWithToken($token);
    }

    protected function respondWithToken($token)
    {
        return [
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('jwt')->factory()->getTTL() * 60
        ];
    }

    public function refresh()
    {
        return $this->respondWithToken(auth('jwt')->refresh());
    }

call /api/refresh, output:

{
    "message": "include(/app/User.php): failed to open stream: No such file or directory",
    "status_code": 500
}

note: I deleted the app/user.php

Expected Behaviour

Describe the behaviour you're expecting.

JWT works normally and uses my model.

Steps to Reproduce

List all the steps needed to reproduce the issue you're having. Make sure to include code (affected models, configurations),
any screenshots and/or other resources that may help us understand what's going on.

Possible Solutions

If you have any ideas on how to solve the issue, add them here, otherwise you can omit this part.

I tested a lot of ways, but I really couldn't handle it, so I came to ask for help.

commented

Hi

There is a setting where the User model is, and whatever is throwing that error, it doesn't have the correct path to the User model configured. I think there's a couple of places it can be, but I would advise you to search the whole config directory for "User" and also enable full debugging so you see where this is thrown.