Laravel Passport Auth App
Laravel already makes it easy to perform authentication via traditional login forms, but what about APIs? APIs typically use tokens to authenticate users and do not maintain session state between requests. Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes. Passport is built on top of the League OAuth2 server that is maintained by Andy Millington and Simon Hamp.
- After cloning the application, you need to install it's dependencies,
- cd laravel-passport-auth
- composer install
- Then rename .env.example as .env and provide correct db details.
- After Generate the application key using following command,
- php artisan key:generate
- Migrate the application using following command,
- php artisan migrate
- Create the encryption keys needed to generate secure access tokens,
- php artisan passport:install
- Finally run the application using following command,
- php artisan serve
- Create laravel project,
- composer create-project --prefer-dist laravel/laravel laravel-passport-auth "5.8.*"
- Then install laravel passport package,
- composer require laravel/passport
- Create db connection and run migrations,
- php artisan migrate
- Generate keys,
- php artisan passport:install
- Add the Laravel\Passport\HasApiTokens trait to App\User model (Use my code).
- Next, call the Passport::routes method within the boot method of the Providers/AuthServiceProvider (Use my code).
- After, in config/auth.php configuration file, set the driver option of the api authentication guard to passport (Use my code).
- Create UserController (Use my code),
- php artisan make:controller /API/UserController
- Create api routes (Use my code).
- Finally run the following command and check the application using Postman tool.
- php artisan serve
Bellow i mentioned my postman requests...
- http://127.0.0.1:8000/api/register -- Method:- POST -- Payload:-
{ "name":"Name", "email":"email@gmail.com", "password":"12345678" }
--
-
http://127.0.0.1:8000/api/authenticate -- Method:- POST -- Payload:- -- { "email":"email@gmail.com", "password":"12345678" } -- --
-
http://127.0.0.1:8000/api/user -- Method:- GET -- Payload:- Key: Authorization Value: Bearer [insert your token] -- --
You can change above api as you wish and according to the requirements.