This is a simple API backend for a social media app. Its features include:
- User registration and deletion.
- Post creation, deletion, (un)liking, and flagging.
- A simple content filter that checks and prevents publishing of posts if they contain certain prohibited words.
- Admin users with permission to delete posts, delete users, and review flagged posts.
This API uses Node/Express.js for the backend and MongoDB for the database.
This API uses the following precautions to ensure user safety:
- Passwords are hashed using the
bcryptlibrary and stored in the database in their encrypted form. The hash function is one-way, so a bad actor with access to the database wouldn't be able to reverse-engineer passwords. - Session cookies are encrypted using the JSON Web Token protocol. This involves signing with a server-defined secret to allow detection of modified session cookies.
- Sessions only last for a server-defined amount of time. This reduces the probability of a session replay attack by invalidating session cookies while not in use.
- To create an admin account, a user must provide the server-defined admin secret alongisde their account registration information.
This API exposes the following routes and methods:
POST /api/users: Register a new userGET /api/users/[id]: Retrieve user information by IDDELETE /api/users/[id]: Delete a user by IDGET /api/users/[id]/posts: Retrieve a user's posts
POST /api/posts: Create a new postGET /api/posts/[id]: Retrieve a post by IDDELETE /api/posts/[id]: Delete a post by IDPOST /api/posts/[id]/like: Like a postDELETE /api/posts/[id]/like: Unlike a postGET /api/posts/[id]/flag: Obtain a post's flags (admins only)POST /api/posts/[id]/flag: Flag a post for review
POST /api/sessions: Log in and create a new sessionDELETE /api/sessions: Log out and delete the current session
Check out the full documentation.
To set up the project, follow these steps:
-
Clone the repository:
git clone https://github.com/NajmKHoda/social-api.git cd social-api -
Install dependencies using
npm:npm install
-
Set up the database:
Head to the MongoDB website and create an account. Then, follow the instructions of this article to create a database using MongoDB Atlas. Obtain the URI provided by Atlas for your cluster by selecting Connect on the project overview page and following the instructions.
-
Set up environment variables:
Create a
.envfile in the root directory. The.envfile MUST declare the following variables:MONGODB_URI= # URI provided by MongoDB Atlas ADMIN_SECRET= # Secret for admin account creation SESSION_SECRET= # Signing secret for session cookies SESSION_DURATION= # Length of a session, in seconds SESSION_ISSUER= # Name of the app providing the session
You can also declare a
PORTvariable if you want to bind to a port other than8000. You may refer to.env.examplefor a template. -
(optional) Configure prohibited words:
The
/config/disallowedWords.txtfile contains the list of all words (each on their own line) used by the content filter. If a post contains any word in this list, it is not published. Feel free to change this list to suit your needs. -
Start the server:
npm start
Your API server should now be running.