This project is a simple API built with Laravel that uses JWT (JSON Web Token) for user authentication. Authenticated users can create, view, and delete posts. The API ensures that only the creators of a post can delete their own posts.
- User registration and login with JWT authentication.
- Protected routes for creating and deleting posts.
- Users can only delete their own posts.
- Fetch all posts.
Before you begin, ensure you have met the following requirements:
- PHP: 7.4 or higher
- Composer: Latest version
- Laravel: 8.x or higher
- MySQL: 5.7+ or PostgreSQL
-
Clone the Repository
git clone https://github.com/lordwill1/blogapi.git cd blogapi
-
Install Dependencies
Run the following command to install all the dependencies:
composer install
-
Copy
.env
FileCopy the example environment file and create a new
.env
file:cp .env.example .env
-
Generate Application Key
Run the following command to generate a new application key:
php artisan key:generate
-
Set Up Database
-
Open the
.env
file and update the database configuration section with your database credentials:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_user DB_PASSWORD=your_database_password
-
Run the migrations to create the required tables:
php artisan migrate
-
-
Install JWT Package
Install the Laravel JWT package:
composer require tymon/jwt-auth
-
Publish JWT Configuration
Publish the JWT configuration file:
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
-
Generate JWT Secret Key
Generate the secret key for JWT:
php artisan jwt:secret
To start the development server, run:
php artisan serve
The application will be accessible at http://127.0.0.1:8000
.
To see all end points
php artisan route:list
-
Register
POST /api/register
Request Body:
{ "name": "John Doe", "email": "[email protected]", "password": "password", "password_confirmation": "password" }
Response:
{ "user": { ... }, "token": "jwt-token-here" }
-
Login
POST /api/login
Request Body:
{ "email": "[email protected]", "password": "password" }
Response:
{ "token": "jwt-token-here" }
-
Logout
POST /api/logout
Request Header:
Authorization: Bearer jwt-token-here
Response:
{ "message": "Successfully logged out" }
-
Create Post
POST /api/posts
Request Header:
Authorization: Bearer jwt-token-here
Request Body:
{ "title": "My First Post", "content": "This is the content of my first post." }
Response:
{ "id": 1, "title": "My First Post", "content": "This is the content of my first post.", "user_id": 1, "created_at": "2024-08-12T10:00:00.000000Z", "updated_at": "2024-08-12T10:00:00.000000Z" }
-
View All Posts
GET /api/posts
Response:
[ { "id": 1, "title": "My First Post", "content": "This is the content of my first post.", "user_id": 1, "created_at": "2024-08-12T10:00:00.000000Z", "updated_at": "2024-08-12T10:00:00.000000Z" }, ... ]
-
Delete Post
DELETE /api/posts/{id}
Request Header:
Authorization: Bearer jwt-token-here
Response:
{ "message": "Post deleted successfully" }
- Register a New User: Use the
/api/register
endpoint to create a new user. - Login: Use the
/api/login
endpoint to authenticate and receive a JWT token. - Set Authorization Header: For subsequent requests, include the JWT token in the
Authorization
header asBearer jwt-token-here
. - Create a Post: Use the
/api/posts
endpoint to create a new post. - View Posts: Use the
/api/viewposts
endpoint to fetch all posts. - Delete a Post: Use the
/api/posts/{id}
endpoint to delete a post.
- 404 Not Found for
/api/register
: Ensure that your API routes are prefixed with/api
and that your server is running at the correct URL (http://127.0.0.1:8000
). - "Method
index
does not exist": Ensure that theindex
method is defined inPostController
. - "Add
[attribute]
to fillable property": Ensure that you have added the required attributes to the$fillable
property in your model. - "Route [login] not defined": Ensure that your authentication routes are correctly defined in
api.php
.
This README.md
file provides all the necessary information to understand, install, and use the Laravel JWT API project, as well as to troubleshoot common issues.