This project has been archived. If you're curious, check out one of my latest courses:

If you work with JavaScript, check out the fetch use cases and JavaScript projects.

Thank you! — Jad Joubran

JWT Auth Installation

Installation instructions for JWT Authentication

Install tymon/jwt-auth

Require the package using composer:

composer require tymon/jwt-auth:^1.0@dev

Add the service provider in config/app.php

Tymon\JWTAuth\Providers\LaravelServiceProvider::class,

Publish the vendor config file:

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

Generate the JWT

php artisan jwt:secret

 

Install irazasyed/jwt-auth-guard

Require the package using composer:

composer require irazasyed/jwt-auth-guard

Add the service provider in config/app.php

Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider::class,

Setup the Guard driver in your config/auth.php

'guards' => [
    'api' => [
        'driver' => 'jwt-auth',
        'provider' => 'users'
    ],
    // ...
],

Update unauthorized format

We still need to update the format of the authorized response to match our response macros.

Replace the unauthenticated method in app/Exceptions/Handler.php with the below:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->error('Unauthenticated.', 401);
    }

    return redirect()->guest(route('login'));
}

You're all set!