1
votes

I have been trying to implement the JWT-Aut in Laravel's Dingo API package, but I am stuck at users Login part, I have checked the official docs for auth but cant figure out, here is what I have done till now.

Protected the routes

Route::api(['version' => 'v1', 'protected' => true], function () {

    Route::resource('users', 'UserController');
});

Added JWT auth provider in dingo/config

'jwt' => function ($app) {
    return new Dingo\Api\Auth\JWTProvider($app['tymon.jwt.auth']);
 }

Installed the JWT-Auth from Github docs

Tried Login using below sample code from JWT-Auth docs using Postman but getting {token : false}

 Route::post('auth/login', function () {
        $credentials = Input::only('email', 'password');

        if ( ! $token = JWTAuth::attempt($credentials) )
        {
            // return 401 error response
        }

        return Response::json(compact('token'));
});

If someone can guide how I can login logout, & signup user and make a request with Authorization: Bearer <token> will be very helpful.

If someone could share your auth controller for same will be lifesaver :)

1

1 Answers

1
votes

It looks like you are not returning a response when the credentials are incorrect - so the token will be equal to false in that case.

Here is an example:

Route::post('auth/login', function () {
    $credentials = Input::only('email', 'password');

    if ( ! $token = JWTAuth::attempt($credentials) )
    {
        // return the 401 response
        return Response::json(['error' => 'invalid_credentials'], 401);
    }

    return Response::json(compact('token'));
});