A package to implement easy authentication in lumen
22
6
2
Package Data
Maintainer Username: abdurrahmanriyad
Maintainer Contact: me.riyad@outlook.com (Abdur Rahman)
Package Create Date: 2019-01-11
Package Last Update: 2019-01-18
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:13:43
Package Statistics
Total Downloads: 22
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 6
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

lumenAuth

LumenAuth is a simple authentication package for lumen. This enables user to get rid of primary hassle to setup token based authentication in lumen.

Installation

  • Install LumenAuth with composer:
composer require abdurrahmanriyad/lumenauth
  • Add the following line to bootstrap/app.php file
$app->register(\Abdurrahmanriyad\LumenAuth\LumenAuthServiceProvider::class);
  • Add a secret key(anything you want) to LUMEN_AUTH_SECRET in .env which used to build token
LUMEN_AUTH_SECRET=YOUR_SECRET_KEY

eg. LUMEN_AUTH_SECRET=7852ef15dcdd3eaeb40sdfasdf459171556
  • If your User model doesn't belong to App\User then add the following line on your .env file
USER_MODEL=YOUR_USER_MODEL_NAMESPACE

eg. USER_MODEL=App\Models\User

Usage

  • Add lumenAuth middleware to routes you want authentication

For example,

$router->group(['middleware' => 'lumenAuth'],
    function () use ($router) {
        //your routes
    });
  • To generate token for a user you use LumenAuthFacade::getToken($user) which return a token for given user instance.

For example,

use Abdurrahmanriyad\LumenAuth\Facades\LumenAuthFacade;
.......

class AuthController extends Controller {

   public function login(Request $request) {
        //validate user and check user
        ........

        // Verify the password and generate the token
        if (Hash::check($userPassword, $user->password)) {
            return response()->json([
                'token' => LumenAuthFacade::getToken($user)
            ], 200);
        }

        ......... 
    }

}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT