This repo contains the source code for a file management system. This app allows user to retrieve and read the file content.
- Express.js
- Prisma
- MySQL
- Google Cloud Storage
- AWS Cloud Storage
- User Authentication using JWT tokens
- create file with content inside it
- retrieve a file based on id
- retrieve all files
- delete a file based on id
- read file based on id
Make sure you have following installed
- Node.js
- npm
- mySQL
- prisma
- express
- all the other dependencies in package.json
-
Clone the repo:
git clone git@github.com:Avi-17/Task-nest.git
-
Install Dependencies
cd Task-nest npm i
-
Setting up database
inside a .env file, set the following variables
JWT_SECRET FIREBASE_ADMIN_SDK (base64 encoded string Google Cloud Secrets) DATABASE_URL (to connect with db) DB_HOST DB_PORT DB_USER DB_PASSWORD DB_NAME
After this, if you want you can also set up google cloud storage
- generate private key, a json file will be downloaded, keep that file in your config folder
The entry point for this application is index.js.
The server can be started with the help of following command
node index.jscan also use nodemon
For the file metadata, aws cloud storage is used. File creation, storage and deletion is done in google cloud storage. Both are in sync i.e. if a file is created or deleted, it is reflected both in aws as well as in firebase cloud storage.
-
METHOD: POST
- /auth/signup : requires email and password in request body. Signs up a new user
- /auth/login : requires email and password in request body, generates a JWT token which is later used as a Authorization header to provide access to various operations to the file system
- /api/files: creates a new file. requires fileName and content in request body.
-
METHOD: GET
- /api/files/:id : pass id in url, returns file details by id.
- /api/allFiles : returns all Files metadata.
- /api/files/:id/read: pass id in url, returns file content by id.
-
METHOD: DELETE
- /api/files/:id : pass id in url, deletes the file from id.
-
METHOD: PUT
- /api/files/:id : pass id in url and content in req body in json format. Updates (overwrites) the file Content.
This middleware is used to secure access to various operations in the file system project by verifying JSON Web Tokens (JWT) for user authentication. Here's a breakdown of how it works:
- Token Extraction: The middleware expects an authorization token in the
Authorizationheader of the request, formatted asBearer <token>. It retrieves and isolates the token. - Access Denial: If no token is provided, the middleware immediately returns a
401status with anAccess Deniedmessage. - Token Verification: Using
jsonwebtokenand the secret key (JWT_SECRET), it verifies the token’s validity. If valid, theuserIdfrom the token payload is assigned toreq.userIdfor further use in downstream route handlers. - Error Handling: If the token is invalid or expired, the middleware responds with a
400status and anInvalid tokenmessage.
Add this middleware to any route requiring authentication to ensure only authorized users can perform specific actions.
Contributions are welcome! Feel free to submit pull requests or open issues if you find any problems or improvements.