All Projects → irazasyed → Jwt Auth Guard

irazasyed / Jwt Auth Guard

Licence: mit
JWT Auth Guard for Laravel and Lumen Frameworks.

Projects that are alternatives of or similar to Jwt Auth Guard

video-downloader
Video Downloader for Facebook.
Stars: ✭ 63 (-80.25%)
Mutual labels:  composer, lumen, laravel-package, laravel-5-package, composer-packages
Laravel Server Monitor
Server Monitoring Command for Laravel Applications
Stars: ✭ 424 (+32.92%)
Mutual labels:  laravel, laravel-package, laravel-5-package, composer, composer-packages
Laravel Jwt
Dead simple, plug and play JWT API Authentication for Laravel (5.4+)
Stars: ✭ 225 (-29.47%)
Mutual labels:  laravel, laravel-5-package, jwt, jwt-authentication, jwt-auth
Telegram Bot Sdk
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
Stars: ✭ 2,212 (+593.42%)
Mutual labels:  laravel, laravel-package, laravel-5-package, composer, composer-packages
Laravel Remember Uploads
Laravel Middleware and helper for remembering file uploads during validation redirects
Stars: ✭ 67 (-79%)
Mutual labels:  middleware, laravel, laravel-package, laravel-5-package
Administrator
a fork from Frozennode/Administrator
Stars: ✭ 296 (-7.21%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+766.14%)
Mutual labels:  laravel, laravel-5-package, auth
Htmlcache
Laravel middleware to cache the rendered html
Stars: ✭ 35 (-89.03%)
Mutual labels:  middleware, laravel, composer
L5 Very Basic Auth
Stateless HTTP basic auth for Laravel without the need for a database.
Stars: ✭ 127 (-60.19%)
Mutual labels:  middleware, laravel, laravel-5-package
Laravel Http2 Server Push
A middleware package for Laravel to enable server push for your script, style, and image assets.
Stars: ✭ 174 (-45.45%)
Mutual labels:  middleware, laravel, lumen
Laravel Shield
A HTTP basic auth middleware for Laravel
Stars: ✭ 193 (-39.5%)
Mutual labels:  middleware, laravel, composer
Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (-25.39%)
Mutual labels:  laravel, laravel-5-package, composer
Auth Tests
Always-current tests for Laravel's authentication system. Curated by the community.
Stars: ✭ 230 (-27.9%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Gitscrum
GitScrum is a Project Management Tool, developed to help entrepreneurs, freelancers, managers, and teams Skyrocket their Productivity with the Agile methodology and Gamification.
Stars: ✭ 2,686 (+742.01%)
Mutual labels:  laravel, laravel-package, laravel-5-package
jwt-auth
JSON Web Token Authentication for Laravel and Lumen
Stars: ✭ 46 (-85.58%)
Mutual labels:  middleware, composer, lumen
Laravel Gamp
📊 Laravel Google Analytics Measurement Protocol Package
Stars: ✭ 271 (-15.05%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Sneaker
An easy way to send emails whenever an exception occurs on server.
Stars: ✭ 223 (-30.09%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Laravel Achievements
Achievements for Laravel 5.3+
Stars: ✭ 279 (-12.54%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Voyager Frontend
The Missing Front-end for The Missing Laravel Admin 🔥
Stars: ✭ 200 (-37.3%)
Mutual labels:  laravel, laravel-package, laravel-5-package
Hooks
Hooks is a extension system for your Laravel application.
Stars: ✭ 202 (-36.68%)
Mutual labels:  laravel, laravel-package, laravel-5-package

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

Pre-Installation

First install and setup tymon/jwt-auth package.

$ composer require tymon/jwt-auth:^[email protected]

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' => '[email protected]']);

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].