Skip to content

mdalaminsarker/ethikana

Repository files navigation

Lumen with JWT Authentication

Basically this is a starter kit for you to integrate Lumen with JWT Authentication.

What's Added

Quick Start

  • Clone this repo or download it's release archive and extract it somewhere
  • You may delete .git folder if you get this code via git clone
  • Run composer install
  • Run php artisan jwt:generate
  • Configure your .env file for authenticating via database
  • Run php artisan migrate --seed

A Live PoC

  • Run a PHP built in server from your root project:
php -S localhost:8000 -t public/

Or via artisan command:

php artisan serve

To authenticate a user, make a POST request to /auth/login with parameter as mentioned below:

email: johndoe@example.com
password: johndoe

Request:

curl -X POST -F "email=johndoe@example.com" -F "password=johndoe" "http://localhost:8000/auth/login"

Response:

{
  "success": {
    "message": "token_generated",
    "token": "a_long_token_appears_here"
  }
}
  • With token provided by above request, you can check authenticated user by sending a GET request to: /auth/user.

Request:

curl -X GET -H "Authorization: Bearer a_long_token_appears_here" "http://localhost:8000/auth/user"

Response:

{
  "success": {
    "user": {
      "id": 1,
      "name": "John Doe",
      "email": "johndoe@example.com",
      "created_at": null,
      "updated_at": null
    }
  }
}
  • To refresh your token, simply send a PATCH request to /auth/refresh.
  • Last but not least, you can also invalidate token by sending a DELETE request to /auth/invalidate.
  • To list all registered routes inside your application, you may execute php artisan route:list
⇒  php artisan route:list
+--------+------------------+---------------------+------------------------------------------+------------------+------------+
| Verb   | Path             | NamedRoute          | Controller                               | Action           | Middleware |
+--------+------------------+---------------------+------------------------------------------+------------------+------------+
| POST   | /auth/login      | api.auth.login      | App\Http\Controllers\Auth\AuthController | postLogin        |            |
| GET    | /                | api.index           | App\Http\Controllers\APIController       | getIndex         | jwt.auth   |
| GET    | /auth/user       | api.auth.user       | App\Http\Controllers\Auth\AuthController | getUser          | jwt.auth   |
| PATCH  | /auth/refresh    | api.auth.refresh    | App\Http\Controllers\Auth\AuthController | patchRefresh     | jwt.auth   |
| DELETE | /auth/invalidate | api.auth.invalidate | App\Http\Controllers\Auth\AuthController | deleteInvalidate | jwt.auth   |
+--------+------------------+---------------------+------------------------------------------+------------------+------------+

Future

I will make a standalone package that would works on your current project.

License

Laravel and Lumen is a trademark of Taylor Otwell
Sean Tymon officially holds "Laravel JWT" license

ethikana

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •