Jwt-auth: access_token value = true, Laravel 5.5 and jwt-auth 1.0.0-rc.1

Created on 1 Nov 2017  路  11Comments  路  Source: tymondesigns/jwt-auth

On success login, the access_token value is "true".

The login method:

`public function login(Request $request)
{
$credentials = $request->only('email', 'password');

    if ($token = $this->guard()->attempt($credentials)) {
        return $this->respondWithToken($token);
    }

    return response()->json(['error' => 'Unauthorized'], 401);
}`

The response (success):

{"access_token":"true","token_type":"bearer","expires_in":3600}

Most helpful comment

edit the config/app.php aliases
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,

`public function login(Request $request)
{
$credentials = $request->only('email', 'password');

    if ($token = JWTAuth::attempt($credentials)) {
        return $this->respondWithToken($token);
    }

    return response()->json(['error' => 'Unauthorized'], 401);
}

`
enjoy it begin

All 11 comments

edit the config/app.php aliases
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,

`public function login(Request $request)
{
$credentials = $request->only('email', 'password');

    if ($token = JWTAuth::attempt($credentials)) {
        return $this->respondWithToken($token);
    }

    return response()->json(['error' => 'Unauthorized'], 401);
}

`
enjoy it begin

Ok, it works. Thanks

As in the official docs for 1.0.0

Change this in auth.php

guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

then copy the controller from controller

just change this part in the controller

    public function guard()
    {
        return Auth::guard();
    }

    //Change above to this

    public function guard()
    {
        return Auth::guard('api');
    }

This works for me and should definitely work for you too :)

The answer by @tushark12345 is correct. If you don't change the guard types in auth.php it uses the regular auth flow, which returns a simple true in attempt to indicate the authenticated content can be run on the next line (view return, redirect, etc).

I think it makes sense to update the documentation to leave the default guard as web... it makes more sense to specify it on the routes, and define the Auth::guard('api'); in the JWT controller.

In config/auth.php change

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

to

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

its work ;)

Hi, I'm using Laravel 5.8. I am getting an error "Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in ProjectPath\vendor\tymon\jwt-auth\src\JWTAuth.php on line 54"

On success login, the access_token value is "true".

The login method:

`public function login(Request $request)
{
$credentials = $request->only('email', 'password');

    if ($token = $this->guard()->attempt($credentials)) {
        return $this->respondWithToken($token);
    }

    return response()->json(['error' => 'Unauthorized'], 401);
}`

The response (success):

{"access_token":"true","token_type":"bearer","expires_in":3600}

Worked for me. Thanks

Try this:

if ($token = auth('api')->attempt($credentials)) {
    return $this->respondWithToken($token);
}

return response()->json(['error' => 'Unauthorized'], 401);

Hi

php artisan config:clear
php artisan config:cache

It worked for me

As in the official docs for 1.0.0

Change this in auth.php

guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

then copy the controller from controller

just change this part in the controller

    public function guard()
    {
        return Auth::guard();
    }

    //Change above to this

    public function guard()
    {
        return Auth::guard('api');
    }

This works for me and should definitely work for you too :)

Was this page helpful?
0 / 5 - 0 ratings