Code Monkey home page Code Monkey logo

blogapi's Introduction

Laravel JWT API for User Authentication and Post Management

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.

Features

  • User registration and login with JWT authentication.
  • Protected routes for creating and deleting posts.
  • Users can only delete their own posts.
  • Fetch all posts.

Prerequisites

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

Installation

  1. Clone the Repository

    git clone https://github.com/lordwill1/blogapi.git
    cd blogapi
  2. Install Dependencies

    Run the following command to install all the dependencies:

    composer install
  3. Copy .env File

    Copy the example environment file and create a new .env file:

    cp .env.example .env
  4. Generate Application Key

    Run the following command to generate a new application key:

    php artisan key:generate
  5. 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
  6. Install JWT Package

    Install the Laravel JWT package:

    composer require tymon/jwt-auth
  7. Publish JWT Configuration

    Publish the JWT configuration file:

    php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
  8. Generate JWT Secret Key

    Generate the secret key for JWT:

    php artisan jwt:secret

Usage

Starting the Server

To start the development server, run:

php artisan serve

The application will be accessible at http://127.0.0.1:8000.

API Endpoints

To see all end points

php artisan route:list

Authentication

  • 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"
    }

Post Management

  • 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"
    }

Testing with Postman

  1. Register a New User: Use the /api/register endpoint to create a new user.
  2. Login: Use the /api/login endpoint to authenticate and receive a JWT token.
  3. Set Authorization Header: For subsequent requests, include the JWT token in the Authorization header as Bearer jwt-token-here.
  4. Create a Post: Use the /api/posts endpoint to create a new post.
  5. View Posts: Use the /api/viewposts endpoint to fetch all posts.
  6. Delete a Post: Use the /api/posts/{id} endpoint to delete a post.

Troubleshooting

Common Errors

  • 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 the index method is defined in PostController.
  • "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.

License

This project is licensed

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.

blogapi's People

Contributors

lordwill1 avatar

Watchers

 avatar

Forkers

byteblaze98

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.