Awesome Market is a marketplace API with features for user registration, login, token management, role-based authorization, and more.
- User registration
- User login
- Request new access token using refresh token
- Role-based access control
- Three user roles: Admin, Seller, Buyer
- Admin: Full access to all endpoints
- Seller: Add products, manage inventories
- Buyer: Create orders
- Product Reviews
- Manage orders including payment with card
- User Registration
- User Login
- Token Management
- Product Management (Seller)
- Inventory Management (Seller)
- Order Management (Buyer)
- Product Reviews Management
- Node.js (Managed using
nvm
) - Docker (for running the database and the API)
- Open Terminal.
- Run the install script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
- Load
nvm
:export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
- Download and install
nvm-windows
from nvm-windows GitHub repository. - Run the installer and follow the prompts.
git clone https://github.com/descholar-ceo/awesome-market.git
cd awesome-market
./setup.sh
- Copy all environment variables from .env.example:
cp .env.example .env
- Edit the .env file and paste all provide real values for the environment variables.
./start.sh dev
In case the server fails to start, and throws this error: ERROR [ExceptionHandler] relation "roles" does not exist
, follow the following steps to resolve it:
- Click
CTRL + C
to stop the server - Run again
./start.sh dev
to start the server - You should see the terminal all green
After a successful installation, you can start developing features
To ensure environment safety and consistency across development environments, the application includes logic to validate environment variables before starting the server using Joi. Some environment variables are marked as required, while others are optional. If any value is added that is not validated, the server will not start. This ensures that anyone working on this project has the same environment setup, promoting consistency and preventing configuration errors.
In order to make sure that all features work correctly, including live email notifications, you need a SendGrid API key and a whitelisted email from SendGrid. Follow these steps to obtain the required values:
- Sign up for SendGrid: If you don't already have a SendGrid account, sign up at SendGrid.
- Create an API Key:
- After logging into your SendGrid account, navigate to the "API Keys" section under "Settings".
- Click on "Create API Key" and give it a name.
- Assign the necessary permissions and create the key.
- Copy the generated API key.
- Whitelist an Email Address:
- Navigate to the "Sender Authentication" section under "Settings".
- Follow the steps to verify your email address. SendGrid will send a verification email to the address you provide.
- Once verified, this email address will be whitelisted and can be used to send emails.
- Update .env File: Add the following variables to your
.env
file with the obtained values:
SENDGRID_API_KEY=your_sendgrid_api_key
APP_MAILING_ADDRESS=your_whitelisted_email
To enable payment processing with Stripe, follow these steps:
- Sign up for Stripe: If you don't already have a Stripe account, sign up at Stripe.
- Obtain API Keys:
- After logging into your Stripe account, navigate to the "Developers" section.
- Go to the "API keys" tab. Here you will find your "Publishable key" and "Secret key".
- Copy the "Secret key".
- Set Up Webhooks:
- Navigate to the "Webhooks" section under "Developers".
- Click on "Add endpoint"
- Here you will need a URL that have to look like a live link with
https
scheme, you can use ngrok in order to get it. Run the following command if the dev server is running:
./ngrok http [THE PORT AT WHICH LOCALHOST IS LISTENING]
- Copy the URL ngrok is showing, it's something that looks like this:
https://f5cf-41-186-29-129.ngrok-free.app
- Provide the URL where Stripe will send webhook events (e.g., https://f5cf-41-186-29-129.ngrok-free.app/orders/checkout/webhook).
- Select the API version we are using in this project which is:
2024-04-10
- Select these two events
checkout.session.completed
,checkout.session.async_payment_failed
. - Click on "Add endpoint" and copy the "Signing secret".
- Update .env File: Add the following variables to your
.env
file with the obtained values:
STRIPE_SECRET_KEY=your_stripe_secret_key
STRIPE_WEBHOOK_SECRET=your_stripe_webhook_secret
- Enable Stripe Connect
Enabling this stripe configuration will make it possible for sellers to receive their payouts
- Log in to your Stripe Dashboard.
- Navigate to Settings > Connect.
- Click on Get started with Connect and follow the instructions to enable Stripe Connect for your account.
- Platform branding: Customize the branding for your connected accounts to match your platform's branding.
- Before creating a migration, you need to be using the same node-version specified by the project. To do that, open a terminal, navigate to
awesome-market
directory and run the following command:
nvm use
- And then create a migration by running:
yarn migration:create name-of-your-migration-file
- Before running migrations, you need to be connected to the API container first. To do that, open a terminal, navigate to
awesome-market
directory and run the following command:
./connect-to-api.sh
- And then run the migrations by running:
yarn migration:run
For protected endpoints, you need to include the Authorization header in your request. The value should be in the format Bearer YOUR_ACCESS_TOKEN
. Here is an example of how to include it in your request headers:
GET /protected-endpoint HTTP/1.1
Host: localhost:[HOST_PORT]
Authorization: Bearer YOUR_ACCESS_TOKEN
The /auth/get-new-access-token
endpoint allows you to obtain a new access token using a refresh token without requiring the user to re-login. This is useful when the access token expires before the refresh token.
- Set the following environment variables to configure token expiration times:
REFRESH_JWT_EXPIRES
: The expiration time for the refresh token (e.g., 7d for 7 days).ACCESS_JWT_EXPIRES
: The expiration time for the access token (e.g., 15m for 15 minutes).
- Using the
/get-new-access-token
Endpoint: To get a new access token, send a GET request to the/get-new-access-token
endpoint with the refresh token included in the headers:
GET /auth/get-new-access-token HTTP/1.1
Host: localhost:[HOST_PORT]
Authorization: Bearer YOUR_REFRESH_TOKEN
You can access the API at the endpoint: http://localhost:[HOST_PORT] where HOST_PORT is the value you passed in the .env file.
You can find the db schema pdf file inside the the awesome-market/db/awesome-market.pdf
directory or find it on this link: Awesome Market Schema
Go to http://localhost:[HOST_PORT]/api-docs
All the APIs have the same format of responses:
- For success response we have:
{
status: 200,
message: string,
data: any
}
- For a failure response we have:
{
statusCode: number,
message: [string],
error: string
}
Feel free to fork this repository and make contributions. Pull requests are welcome.