This project is my attempt at the Niyo Group task
- User Authentication
- Registration
- Login
- JWT API token
- Refresh token
- Logout
- Task Management
- Create a task
- Update a task
- Delete a task
- Get all tasks
- Get a task by unique id
- Real-time updates using websockets
- Prerequisites
- PHP 8.3+
- Laravel 11
- Composer
- Node.js (for websocket setup)
- MySQL
- Clone this repository
git clone https://github.com/michaelthedev/rest-task.git cd rest-task
- Install dependencies
composer install npm install
- Set up environment file
Replace the database credentials in the .env file with your own
cp .env.example .env
- Generate application key
php artisan key:generate
- Run the migrations
php artisan migrate
- Regenerate JWT secret key (for API)
php artisan jwt:secret
- Start the development server
php artisan serve
POSTMAN>> https://documenter.getpostman.com/view/10657913/2sA3QmEaG2
Method | Endpoint | Description |
---|---|---|
POST | /auth/login |
Login a user |
POST | /auth/register |
Register a user |
POST | /auth/logout |
Logout a user (with authorization) |
POST | /auth/refresh |
Refresh a user token (with authorization) |
GET | /auth/user |
Get the authenticated user (with authorization) |
POST | /tasks |
Create a task (with authorization) |
GET | /tasks |
Get all tasks (with authorization) |
GET | /tasks/{uid} |
Get a task by unique id (uid ) (with authorization) |
PUT | /tasks/{uid} |
Update a task |
DELETE | /tasks/{uid} |
Delete a task |
By default, the application runs Laravel Reverb on localhost:8080
. These can be configured further in the .env file. The web socket can be used outside laravel as well.
More documentation about broadcasting and events in Laravel is available on Laravel's documentation. You can also check the Laravel Reverb documentation
Make sure to run this to regenerate your Reverb keys (on your laravel project)
php artisan reverb:install
Follow this process to set up web sockets (whether in Laravel or outside Laravel)
- Install Laravel Echo and Pusher
npm install --save laravel-echo pusher-js
- Set up Laravel Echo
import Echo from 'laravel-echo'; import Pusher from 'pusher-js'; window.Pusher = Pusher; window.Echo = new Echo({ broadcaster: 'reverb', key: {{REVERB_APP_KEY}}, wsHost: {{REVERB_HOST}}, wsPort: {{REVERB_PORT}}, wssPort: {{REVERB_PORT}}, forceTLS: {{https or http}}, enabledTransports: ['ws', 'wss'], auth: { headers: { Authorization: 'Bearer JWT_TOKEN_HERE' } } });
- Listen for events
const userId = 1; // replace with actual user id window.Echo.private('App.Models.User.'+userId) .listen('TaskAdded', (e) => { console.log("TaskAdded event received") console.log(e) }) .listen('TaskUpdated', (e) => { console.log("TaskUpdated event received") console.log(e) }) .listen('TaskDeleted', (e) => { console.log("TaskUpdated event received") console.log(e) })
- Start laravel, queue and reverb
php artisan serve php artisan queue:listen php artisan reverb:start
- See
resources/js/echo.js
for a sample implementation. You can also runnpm run dev
for the websocket sample
This project uses phpunit for feature tests. Run the following command to run the tests
php artisan test
Name: Michael Arawole.
Email: michael@logad.net