This repository provides a Ruby on Rails API template that includes user authentication using Devise and JWT (JSON Web Token). It is designed for quickly creating new Rails projects with a prebuilt authentication system and standard API response handling.
-
User Authentication:
Devise
integrated with custom controllers for handling login, registration, and logout.- Token-based authentication using JWT.
-
API Response Handling:
- Consistent JSON responses using a custom response handler.
- Standardized error and success messages.
-
Exception Handling:
- Catch and handle various API errors, including JWT issues, validation errors, and missing parameters.
-
Ready for Development:
- Preconfigured structure for rapid API development.
- Clean, modular code organization.
- Ruby: Ensure Ruby is installed.
- Rails: Install Rails version
>= 7.0
. - Database: PostgreSQL or your preferred DB.
-
Clone this repository:
git clone https://github.com/okueca/rails_api_template.git cd rails_api_template
-
Install dependencies:
bundle install
-
Generate a Rails secret key:
bundle exec rails secret
-
Add the secret to your encrypted credentials file:
- If you use VSCode as your editor:
EDITOR='code --wait' rails credentials:edit
- Add the following to your encrypted credentials file:
# Other secrets... # Used as the base secret for Devise-JWT devise_jwt_secret_key: <paste your generated secret here>
- If you use VSCode as your editor:
-
Set up your database:
rails db:create db:migrate
-
Start the server:
rails server
- Endpoint:
POST /signup
- Request:
{ "user": { "name": "Ariclene Chimbili", "email": "user@example.com", "password": "password", "password_confirmation": "password" } }
- Response:
{ "message": "Signed up successfully.", "data": { "id": 11, "email": "ariclinis10@gmail.com", "name": "Ariclene" }, "meta": [], "errors": [] }
- Endpoint:
POST /login
- Request:
{ "user":{ "email": "ariclinis1@gmail.com", "password": "123456" } }
- Response:
{ "status": 200, "message": "Logged in successfully.", "data": { "user": { "id": 1, "email": "ariclinis1@gmail.com", "name": "Ariclene" } } }
- Endpoint:
DELETE /logout
- Request: Pass Authorization token in the headers.
- Response:
{ "status": 200, "message": "Logged out successfully." }
- Provides consistent JSON response structures for success, error, and pagination.
- Handles exceptions globally and ensures appropriate error messages.
- A helper class (
JsonResponse
) for building structured JSON responses.
rails_api_template/
├── app/
│ ├── controllers/
│ │ ├── concerns/
│ │ │ ├── api_response_handler.rb
│ │ │ ├── api_exceptions_handler.rb
│ │ ├── users/
│ │ │ ├── sessions_controller.rb
│ │ │ ├── registrations_controller.rb
│ ├── serializers/ # Custom UserSerializer (not included here)
│ │ │ ├── plan_serializer.rb
│ │ │ ├── user_serializer.rb
├── lib/
│ ├── okueca_tools/
│ │ ├── json_response.rb
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch
- Make your changes and commit:
git commit -m "Add new feature"
- Push and submit a pull request:
git push origin feature-branch
This project is open-sourced under the MIT License.
Created by Ariclene Chimbili (https://github.com/ariclinis). Feel free to reach out for questions or collaborations!