Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Sample Laravel application to demonstrate REST APIs with JWT authentication

License

Notifications You must be signed in to change notification settings

roncemer/laravel-jwt-rest-api

Repository files navigation

Sample Laravel application to demonstrate REST APIs with JWT authentication

License

MIT

Start up the environment

Start Docker (or Docker Desktop). If using Docker Desktop, open up the window and go into the Containers view.

Bring up the Docker containers for MariaDB and phpMyAdmin:

docker-compose up -d

Install composer modules:

composer install

Start the development web server in a separate window:

php artisan serve

Point a browser to (http://localhost:10000) and enter the following, then click "Log in":

Server: mariadbtest
Username: root
Password: 123TeSt321

Click on the SQL tab, enter the following command, and click "Go" (or, you can just run ./mysql.sh and enter/run this command inside the mysql console):

create database laravel_jwt_rest_api_test;

You should now see the laravel_jwt_rest_api_test database in the left panel (you may need to refesh the page).

Run the database migration in the project folder:

php artisan migrate

Back in phpMyAdmin, you should be able to expand the laravel_jwt_rest_api_test database in the left panel and see a number of tables which were created.

Test the User APIs

Test new user registration:

curl -XPOST -F "name=John Doe" -F "[email protected]" -F "password=admin123" -F "password_confirmation=admin123" "http://localhost:8000/api/auth/register"

Should return JSON similar to the following:

{"message":"User successfully registered","user":{"name":"John Doe","email":"[email protected]","updated_at":"2022-07-18T23:59:35.000000Z","created_at":"2022-07-18T23:59:35.000000Z","id":1}}

Test user login:

curl -XPOST -F "[email protected]" -F "password=admin123" "http://localhost:8000/api/auth/login"

Should return JSON similar to the following (token is redacted here):

{"access_token":"<redacted>","token_type":"bearer","expires_in":3600,"user":{"id":1,"name":"John Doe","email":"[email protected]","email_verified_at":null,"created_at":"2022-07-18T23:59:35.000000Z","updated_at":"2022-07-18T23:59:35.000000Z"}}

Test user profile (replace with the actual token from the response to the login request):

curl -XGET -H "Authorization: Bearer <redacted>" "http://localhost:8000/api/auth/user-profile"

Should return JSON similar to the following:

{"id":1,"name":"John Doe","email":"[email protected]","email_verified_at":null,"created_at":"2022-07-18T23:59:35.000000Z","updated_at":"2022-07-18T23:59:35.000000Z"}

Test JWT token refresh (replace with the actual token from the response to the login request):

curl -XPOST -H "Authorization: Bearer <redacted>" "http://localhost:8000/api/auth/refresh"

Should return JSON similar to the following (new token is redacted here):

{"access_token":"<redacted>","token_type":"bearer","expires_in":3600,"user":{"id":1,"name":"John Doe","email":"[email protected]","email_verified_at":null,"created_at":"2022-07-18T23:59:35.000000Z","updated_at":"2022-07-18T23:59:35.000000Z"}}

Test logout (replace with the actual token from the response to the last login or refresh request):

curl -XPOST -H "Authorization: Bearer <redacted>" "http://localhost:8000/api/auth/logout"

Should return JSON similar to the following:

{"message":"User successfully signed out"}

Test the Post APIs

Log in:

curl -XPOST -F "[email protected]" -F "password=admin123" "http://localhost:8000/api/auth/login"

Save a copy of the token to make it easier to copy and paste it into subsequent curl commands.

Create a post (replace with the actual token from the response to the last login or refresh request):

curl -XPOST -H "Authorization: Bearer <redacted>" -F "title=Laravel 9 REST API" -F "description=Lorem ipsum blah blah blah." "http://localhost:8000/api/posts"

Should return JSON similar to the following:

{"data":{"title":"Laravel 9 REST API","description":"Lorem ipsum blah blah blah.","updated_at":"2022-07-19T01:12:06.000000Z","created_at":"2022-07-19T01:12:06.000000Z","id":1}}

Get all posts (replace with the actual token from the response to the last login or refresh request):

curl -XGET -H "Authorization: Bearer <redacted>" "http://localhost:8000/api/posts"

Should return all posts, in JSON format. If you've created multiple posts, they should all be there.

Get a single post (replace with the actual token from the response to the last login or refresh request):

curl -XGET -H "Authorization: Bearer <redacted>" "http://localhost:8000/api/posts/1"

Update a post (replace with the actual token from the response to the last login or refresh request):

curl -XPOST -H "Authorization: Bearer <redacted>" -F "_method=PUT" -F "title=Laravel 9 New Features" -F "description=Test." "http://localhost:8000/api/posts/1"

Delete a post (replace with the actual token from the response to the last login or refresh request):

curl -XDELETE -H "Authorization: Bearer <redacted>" "http://localhost:8000/api/posts/1"

Stopping everything

Stop the development web server (php artisan serve) using Ctrl+C.

Stop the docker containers:

docker-compose down

About

Sample Laravel application to demonstrate REST APIs with JWT authentication

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published