• Stars
    star
    323
  • Rank 125,360 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JWT Auth Guard for Laravel and Lumen Frameworks.

JWT Auth Guard

Join PHP Chat Chat on Telegram Laravel Package Latest Version on Packagist Software License Total Downloads

JWT Auth Guard is a Laravel & Lumen Package that lets you use jwt as your driver for authentication guard in your application.

The Guard uses tymon/jwt-auth package for authentication and token handling.

Requirements

  • Laravel or Lumen Installation.
  • tymon/jwt-auth ^1.0@dev Package Setup and Config'd.

Pre-Installation

First install and setup tymon/jwt-auth package.

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

Once done, config it and then install this package.

Install

Via Composer

$ composer require irazasyed/jwt-auth-guard

Add the Service Provider

Laravel

Open config/app.php and, to your providers array at the bottom, add:

Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider::class

Lumen

Open bootstrap/app.php and register the service provider:

$app->register(Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider::class);

Usage

Open your config/auth.php config file and in place of driver under any of your guards, just add the jwt-auth as your driver and you're all set. Make sure you also set provider for the guard to communicate with your database.

Setup Guard Driver

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

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model'  => App\User::class,
    ],
],

Middleware Usage

Middleware protecting the route:

Route::get('api/content', ['middleware' => 'auth:api', 'uses' => 'ContentController@content']);

Middleware protecting the controller:

<?php

namespace App\Http\Controllers;

class ContentController extends Controller
{
    public function __construct() 
    {
        $this->middleware('auth:api');
    }
}

Note: The above example assumes you've setup a guard with the name api whose driver is jwt-auth in your config/auth.php file as explained in "Setup Guard Driver" section above.

The following usage examples assume you've setup your default auth guard to the one which uses the jwt-auth driver.

You can also explicitly define the guard before making calls to any of methods by just prefixing it with Auth::guard('api').

Example: Auth::guard('api')->user()

Attempt To Authenticate And Return Token

// This will attempt to authenticate the user using the credentials passed and returns a JWT Auth Token for subsequent requests.
$token = Auth::attempt(['email' => '[email protected]', 'password' => '123456']);

Authenticate Once By ID

if(Auth::onceUsingId(1)) {
    // Do something with the authenticated user
}

Authenticate Once By Credentials

if(Auth::once(['email' => '[email protected]', 'password' => '123456'])) {
    // Do something with the authenticated user
}

Validate Credentials

if(Auth::validate(['email' => '[email protected]', 'password' => '123456'])) {
    // Credentials are valid
}

Check User is Authenticated

if(Auth::check()) {
    // User is authenticated
}

Check User is a Guest

if(Auth::guest()) {
    // Welcome guests!
}

Logout Authenticated User

Auth::logout(); // This will invalidate the current token and unset user/token values.

Generate JWT Auth Token By ID

$token = Auth::generateTokenById(1);

echo $token;

Get Authenticated User

Once the user is authenticated via a middleware, You can access its details by doing:

$user = Auth::user();

You can also manually access user info using the token itself:

$user = Auth::setToken('YourJWTAuthToken')->user();

Get Authenticated User's ID

$userId = Auth::id();

Refresh Expired Token

Though it's recommended you refresh using the middlewares provided with the package, but if you'd like, You can also do it manually with this method.

Refresh expired token passed in request:

$token = Auth::refresh();

Refresh passed expired token:

Auth::setToken('ExpiredToken')->refresh();

Invalidate Token

Invalidate token passed in request:

$forceForever = false;
Auth::invalidate($forceForever);

Invalidate token by setting one manually:

$forceForever = false;
Auth::setToken('TokenToInvalidate')->invalidate($forceForever);

Get Token

$token = Auth::getToken(); // Returns current token passed in request.

Get Token Payload

This method will decode the token and return its raw payload.

Get Payload for the token passed in request:

$payload = Auth::getPayload();

Get Payload for the given token manually:

$payload = Auth::setToken('TokenToGetPayload')->getPayload();

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Any issues, feedback, suggestions or questions please use issue tracker here.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

telegram-bot-sdk

πŸ€– Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
PHP
2,831
star
2

awesome-cloudflare

⛅️ Curated list of awesome Cloudflare worker recipes, open-source projects, guides, blogs and other resources.
416
star
3

laravel-gamp

πŸ“Š Laravel Google Analytics Measurement Protocol Package
PHP
321
star
4

larasupport

πŸ“¦ Adds Laravel Packages Support to Lumen and Vendor Publish Artisan Command.
PHP
126
star
5

video-downloader

Video Downloader for Facebook.
PHP
69
star
6

dns-zone-files

⛅️ DNS Zone Files ready to import on CloudFlare for easy configurations of common configs. Files are in BIND Format.
46
star
7

awesome-laravel-forge-recipes

πŸ₯£ Awesome Laravel Forge Recipes
Shell
45
star
8

laravel-identicon

πŸ‘€ Laravel Identicon Package
PHP
22
star
9

zray-installer

Zend Z-Ray Easy Installer for Laravel Homestead
Shell
22
star
10

php-transmission-sdk

πŸ”„ PHP Transmission-RPC API SDK with Laravel Support.
PHP
20
star
11

phpci-installer

PHPCI Easy Installer for Laravel Homestead
Shell
19
star
12

docgen

πŸ“– Streamline your Laravel package development with automatic facade documentation using Docgen for Laravel.
PHP
18
star
13

php-witai

[WIP] PHP Library for Wit.Ai API with Laravel & Lumen Support out of the box!
PHP
11
star
14

xml-rpc-ping

PHP XML-RPC Service Pinger
PHP
11
star
15

gsuite-checker

Google Suite (GSuite) formerly known as Google Apps - Multi-Domains Checker.
PHP
9
star
16

nginx-google-ip-range-updater

Google PageSpeed IP Ranges Updater for Nginx
Shell
7
star
17

laravel-facebook

Laravel 4 Facebook SDK Package (Deprecated - Use the official Lib, latest version).
PHP
5
star
18

wp-drp-coupon

Mirror of WordPress Plugin - DRP Coupon
PHP
5
star
19

php-ip-range

Checks if IP is within a network range
PHP
5
star
20

gitlab-installer

Server setup for Gitlab Installation on Ubuntu/Linux VPS with helper bash scripts.
Shell
4
star
21

fb-kit

Facebook Graph API JS Kit - To work with Graph API using access token.
JavaScript
3
star
22

awesome-deno

Awesome Deno Land List
1
star
23

php-ixr

This is a fork of the original Incutio PHP XML-RPC library (IXR) SVN repo hosted on Google Code http://scripts.incutio.com/xmlrpc/
1
star
24

phpstorm-settings

1
star
25

ST3-Preferences

My Sublime Text 3 Preferences
1
star
26

irazasyed

1
star
27

jquery-form-helper

jQuery Form Helper - Forked from JREAM BitBucket - http://bitbucket.org/JREAM/js-util
JavaScript
1
star
28

php-chrome-driver-installer

PHP Chrome Driver Installer
PHP
1
star
29

php-stock-media

🏞️ PHP Stock Media - the ultimate library for sourcing stock images, stock videos, and other stock media resources from multiple online stock services with Laravel support out of the box!
PHP
1
star