Jwt-auth: Token generation failing: Type error: Argument 2 passed to Lcobucci\\JWT\\Signer\\Hmac::createHash() must be an instance of Lcobucci\\JWT\\Signer\\Key, null given

Created on 6 Feb 2018  路  7Comments  路  Source: tymondesigns/jwt-auth

Token generation failing

Login was previously working but seems to have been been broken.

Environment

| Bug? | yes
| Framework | Laravel
| Framework version | 5.5.*
| Package version | dev-develop 60103d1
| PHP version | 7.2

Steps to reproduce

Set up jwt auth set up as explained here.

Expected behaviour

Token should be generated.

Actual behaviour

{ "message": "Type error: Argument 2 passed to Lcobucci\\JWT\\Signer\\Hmac::createHash() must be an instance of Lcobucci\\JWT\\Signer\\Key, null given, called in .../app/vendor/lcobucci/jwt/src/Signer/BaseSigner.php on line 34", "status_code": 500 }

Controller

<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Requests\LoginRequest;
use App\Exceptions\InvalidCredentials;
use Tymon\JWTAuth\JWTAuth;
use Illuminate\Support\Facades\Auth;

/**
 * Class LoginController
 * @property JWTAuth JWTAuth
 * @package App\Http\Controllers\Auth
 */
class LoginController extends Controller
{
  /**
   * LoginController constructor.
   * @param JWTAuth $JWTAuth
   */
  public function __construct(JWTAuth $JWTAuth)
  {
    /** @var JWTAuth $JWTAuth */
    $this->JWTAuth = $JWTAuth;
  }

  /**
   * Log the user in
   *
   * @param LoginRequest $request
   * @return \Illuminate\Http\JsonResponse
   */
  public function login(LoginRequest $request)
  {
    $credentials = $request->only(['email', 'password']);
    if (!Auth::attempt($credentials)) {
      throw new InvalidCredentials('Invalid email or password provided.');
    }
    $token = $this->JWTAuth->fromUser(Auth::user());

    return response()
        ->json([
            'status' => 'ok',
            'token' => $token,
            'expires_in' => $this->JWTAuth->factory()->getTTL() * 60
        ]);
  }
}

NB:
Dumping Auth::user() returns a perfectly fine User instance.

Most helpful comment

Try running php artisan jwt:secret

All 7 comments

Try running php artisan jwt:secret

Worked like a charm. Thanks @redolivedev I recently switched computers so I didn't have the key.

I am still having the same problem on laravel v5.6.* . running artisan jwt:secret did not help, I have followed the same instructions as in the page specified above

Same here @i1990jain any joy sorting?

Just sorted - run

php artisan cache:clear
php artisan config:clear

@redolivedev 's answer worked!

鎴戜篃閬囧埌杩欐牱鐨勯棶棰橈紝閲嶆柊鐢熸垚绉橀挜涔熸病鏈夌敤

Same here @i1990jain any joy sorting?

Just sorted - run

php artisan cache:clear
php artisan config:clear

Work perfectly!! :D Thanks so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shah-newaz picture shah-newaz  路  3Comments

hfalucas picture hfalucas  路  3Comments

therealmjk picture therealmjk  路  3Comments

agneshoving picture agneshoving  路  3Comments

aofdev picture aofdev  路  3Comments