All Projects → codecasts → Laravel Jwt

codecasts / Laravel Jwt

Licence: mit
Dead simple, plug and play JWT API Authentication for Laravel (5.4+)

Projects that are alternatives of or similar to Laravel Jwt

Jwt Auth Guard
JWT Auth Guard for Laravel and Lumen Frameworks.
Stars: ✭ 319 (+41.78%)
Mutual labels:  laravel, laravel-5-package, jwt, jwt-authentication, jwt-auth
Php Jwt
Ultra lightweight, dependency free and standalone JSON web token (JWT) library for PHP5.6 to PHP8.0. This library makes JWT a cheese.
Stars: ✭ 214 (-4.89%)
Mutual labels:  jwt, jwt-authentication, jwt-auth, json-web-token
Nest Angular
NestJS, Angular 6, Server Side Rendering (Angular Universal), GraphQL, JWT (JSON Web Tokens) and Facebook/Twitter/Google Authentication, Mongoose, MongoDB, Webpack, TypeScript
Stars: ✭ 307 (+36.44%)
Mutual labels:  jwt, jwt-authentication, jwt-auth
Jwt
Go JWT signing, verifying and validating
Stars: ✭ 394 (+75.11%)
Mutual labels:  jwt, jwt-authentication, jwt-auth
Laravel Vue Starter
Well Documented Laravel Starter App From Development to Production. For Full Blown RESTFUL API and SPA with Beautiful UI Using Buefy / ElementUi For Reusable Vue Components
Stars: ✭ 76 (-66.22%)
Mutual labels:  laravel, jwt, jwt-authentication
Jwt
Kotlin JWT 🔑 implementation (Json Web Token) as required by APNs 🔔 (Apple Push Notifications) or Sign in with Apple 🍏
Stars: ✭ 31 (-86.22%)
Mutual labels:  jwt, jwt-authentication, json-web-token
Laravel Jwt
Laravel with JWT Authentication for API development
Stars: ✭ 31 (-86.22%)
Mutual labels:  laravel, jwt-authentication, jwt-auth
Netcoreblockly
.NET Core API to Blockly - generate from WebAPI, Swagger, OData, GraphQL =>
Stars: ✭ 121 (-46.22%)
Mutual labels:  jwt, jwt-authentication, jwt-auth
Reallysimplejwt
A really simple library to generate JSON Web Tokens in PHP.
Stars: ✭ 218 (-3.11%)
Mutual labels:  jwt, jwt-auth, json-web-token
Laravel Api Boilerplate
A Boilerplate Project For Laravel API's (NOT MAINTAINED)
Stars: ✭ 113 (-49.78%)
Mutual labels:  laravel, jwt-authentication, jwt-auth
React Login
A client side implementation of authentication using react.js for my blog on medium. This is the second part of my previous blog on how to implement scalable node.js server.
Stars: ✭ 105 (-53.33%)
Mutual labels:  jwt, jwt-authentication, jwt-auth
Jose2go
Golang (GO) implementation of Javascript Object Signing and Encryption specification
Stars: ✭ 150 (-33.33%)
Mutual labels:  jwt, jwt-authentication, jwt-auth
Jwt Spring Security Jpa
Backend MVP showcasing JWT (Json Web Token) authentication with multiple login, timeout / refresh / logout (with in memory invalidation) using Spring Security & MySQL JPA.
Stars: ✭ 202 (-10.22%)
Mutual labels:  jwt, jwt-authentication, jwt-auth
Laravel Vue Spa
SPA Boilerplate made with Laravel 5.6, Vue 2, Bootstrap 4, Vue-router 3, Vuex 3, Axios
Stars: ✭ 172 (-23.56%)
Mutual labels:  laravel, jwt
Lapse
Laravel Self Hosted Tiny Error Tracking System With Notifications
Stars: ✭ 172 (-23.56%)
Mutual labels:  laravel, laravel-5-package
Doorkeeper Jwt
JWT Token support for Doorkeeper
Stars: ✭ 174 (-22.67%)
Mutual labels:  jwt, jwt-auth
Jwtpermission
基于token验证的Java Web权限控制框架,使用jjwt,支持redis和db多种存储方式,可用于前后端分离项目,功能完善、使用简单、易于扩展。
Stars: ✭ 186 (-17.33%)
Mutual labels:  jwt, jwt-auth
React Jwt Authentication Example
React - JWT Authentication Tutorial & Example
Stars: ✭ 170 (-24.44%)
Mutual labels:  jwt, jwt-authentication
Laravel Cart Manager
Managing the cart in your Laravel/E-commerce application is a breeze
Stars: ✭ 175 (-22.22%)
Mutual labels:  laravel, laravel-5-package
Aspnetcore2jwtauthentication
Jwt Authentication without ASP.NET Core Identity
Stars: ✭ 218 (-3.11%)
Mutual labels:  jwt, json-web-token

Readme Art

Laravel JWT

Latest Stable Version Total Downloads License

This package provides out-of-the-box API authentication using JWT for Laravel.

Installation.

You can install this package by running:

composer require codecasts/laravel-jwt

Setup.

In order to setup this package into your application, minimal configuration is actually needed.

1) Service Provider.

Register this package's Service Provider by adding it to the providers section of your config/app.php file:

You may skip this step on Laravel 5.5 due to the auto-discovery package feature.

   'providers' => [

       // ... other providers omitted

       Codecasts\Auth\JWT\ServiceProvider::class,

   ],

2) Configuration file.

Publish the configuration file (config/jwt.php) by running the following command after registering the Service Provider.

php artisan vendor:publish --provider="Codecasts\Auth\JWT\ServiceProvider"

3) Generate a Secret.

In order for this package to works, you will need a separate secret (do not use the application key).

This package provides a command that can be used for generating a strong key.

Get a new key by running:

php artisan jwt:generate

Then, copy the generated key contents into your .env file.

NOTICE: The key generation process will not automatically set it inside your .env file, do it manually.

4) Setup Guard

In order to automatically authenticate your routes using JWT tokens, you need to change the guard driver to jwt

Inside config/auth.php set the corresponding guard group you want to protect:

If you have the default guard group named api, your auth.php should be like this:

  'guards' => [
        // ... other guards omitted.

        'api' => [
            'driver'   => 'jwt', // this is the line you need to change.
            'provider' => 'users',
        ],
    ],

That's it, we are all ready to use it.

Usage.

This package aims to be dead simple to use.

The following templates can be used to setup your existing authentication controllers and resources.

NOTICE: Full working examples of use for this package will be added on this package when it reaches it's 1.0 version.

Protecting Routes.

This package is fully integrated with Laravel Authentication.

The default configuration (config/jwt.php) brings a sensitive value that is very useful when your application is not completely an API: middleware_match

By not completely an API, I mean, the JWT guard is not the default one.

In those cases, in order to use the auth middleware, the config key middleware_match MUST be set to true.

This configuration key allows non protected routes to work properly.

Notice that this option will match middleware group names with guard names.

In this case, the 'api' middleware group will always use the api guard.

Also, the 'web' middleware group will always use the web guard

If you do not use this value, you will need to use suffixes when referencing the auth middleware, like auth:api.

Issuing and Renewing Tokens.

For issuing tokens, no special class is actually needed, you can just expect create a Guard current implementation from the IoC and work from there.

Check out the examples.

On the following examples, all Guard instances are injected from Illuminate\Contracts\Auth\Guard

On the following examples, all Request instances are injected from Illuminate\Http\Request

Token from User Instance.

This method should be used when you just registered a user and any other special cases.

public function tokenFromUser(Guard $auth)
{
    // generating a token from a given user.
    $user = SomeUserModel::find(12);

    // logs in the user
    $auth->login($user);

    // get and return a new token
    $token = $auth->issue();

    return $token;
}

Token from User Credentials.

This method should be used when you just registered a user and any other special cases.

public function tokenFromCredentials(Guard $auth, Request $request)
{
    // get some credentials
    $credentials = $request->only(['email', 'password']);

    if ($auth->attempt($credentials)) {
       return $token = $auth->issue();
    }

    return ['Invalid Credentials'];
}

Refreshing Tokens.

Tokens can be refreshed in 2 different ways: Auto detect or manual.

If you do not pass any argument into the refresh method, the Guard will look for either a Authorization header or a token field on the request's body.

public function refreshToken(Guard $auth)
{
    // auto detecting token from request.
    $token = $auth->refresh();

    // manually passing the token to be refreshed.
    $token = $auth->refresh($oldToken);

    return $token;
}

Custom Claims.

Of course, there are support for custom claims.

You can set them in two ways.

By explicitly passing them.

$customClaims = [
    'custom1' => 'value1',
    'custom2' => 'value2',
];

// when issuing
$auth->issue($customClaims);

// when refreshing
// custom claims are the second parameter as the first one is the
// old token
$auth->refresh(null, $customClaims);

By Authenticatable method.

If all your users will have the same custom claims, you can setup a default custom claims method on your User's model (or any other Authenticatable you're using):

If the method customJWTClaims() is present on the model being issue the token against, this claims will be automatically included.

class User extends Model implements Authenticatable
{
    public function customJWTClaims()
    {
        return [
            'email' => $this->email,
            'name'  => $this->name,
        ];
    }
}

Contributing

Please see CONTRIBUTING for details.

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].