https://github.com/yujhenchen/express-mongo-otp-auth-ts
An authentication Restful API featuring OTP verification, JSON Web Token authentication ( JWT ), and database CRUD. Built with Express.js, MongoDB, Mongoose, and TypeScript
https://github.com/yujhenchen/express-mongo-otp-auth-ts
express express-js expressjs jsonwebtoken jwt jwt-authentication mongodb mongoose nodejs nodemailer otp restful-api restfulapi restfull-api typescipt typescript
Last synced: 3 months ago
JSON representation
An authentication Restful API featuring OTP verification, JSON Web Token authentication ( JWT ), and database CRUD. Built with Express.js, MongoDB, Mongoose, and TypeScript
- Host: GitHub
- URL: https://github.com/yujhenchen/express-mongo-otp-auth-ts
- Owner: yujhenchen
- License: mit
- Created: 2024-05-31T08:30:19.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-30T12:40:28.000Z (almost 2 years ago)
- Last Synced: 2024-07-30T18:55:45.170Z (almost 2 years ago)
- Topics: express, express-js, expressjs, jsonwebtoken, jwt, jwt-authentication, mongodb, mongoose, nodejs, nodemailer, otp, restful-api, restfulapi, restfull-api, typescipt, typescript
- Language: TypeScript
- Homepage:
- Size: 309 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Express MongoDB OTP Auth API
Table of Contents
## About The Project
A Restful API backend application built with Express.js and TypeScript, offering robust and scalable solutions for user authentication and data management.
### Feautres
1. mean Stack Architecture: Follows the [mean stack](https://github.com/linnovate/mean) for a well-structured and maintainable project
2. Schema and Data Validation: Utilizes Joi for defining schemas and validating data, including environment variables (process.env)
3. MongoDB Integration: Employs Mongoose to model and interact with MongoDB Atlas
4. User Authentication: Supports user sign-up, sign-in, and logout functionalities
5. Email Service with OTP: Built with Nodemailer and Brevo, allowing the generation and sending of OTP codes via email
6. CRUD Operations: Provides create, read, update, and delete (CRUD) operations for user data in the MongoDB Atlas database
7. Role-Based Access Control: Implements role-based access control using Bearer Authentication for sensitive API endpoints, such as changing user roles and deleting users
8. Request Validation Middleware: Includes middleware for validating incoming requests to ensure data integrity and security
9. JWT Authentication: Generates JSON Web Tokens (JWT) using the jsonwebtoken library for secure user authentication and authorization
### Built With
- [![Express.js]][Express.js-url]
- [![MongoDB]][MongoDB-url]
- [![TypeScript]][TypeScript-url]
- [Joi]
- [Mongoose]
- [jsonwebtoken]
- [module-alias]
- [Nodemailer]
## Getting Started
some steps
### Installation
Install packages
```sh
yarn
```
Launch at localhost in development mode
```sh
yarn dev
```
## Issues and solutions
### Get error `Error: Method no longer accepts array arguments: valid`
The full error message:
````
node_modules\@hapi\hoek\lib\error.js:23
Error.captureStackTrace(this, exports.assert);
^
Error: Method no longer accepts array arguments: valid
````
When passing the array `Object.values(UserRole)` into `Joi.string().valid()`
````
export const UserRole = {
ADMIN: 'admin',
visitor: 'user',
GUEST: 'guest'
} as const;
...
role: Joi.string().valid(Object.values(UserRole)),
...
````
##### Solution
- https://github.com/hapijs/joi/issues/2468
Use Spread syntax (...) to expand the array.
````
...
role: Joi.string().valid(...Object.values(UserRole)),
...
````
## License
Distributed under the MIT License. See `LICENSE.txt` for more information.
[Express.js]: https://img.shields.io/badge/express.js-%23404d59.svg?style=for-the-badge&logo=express&logoColor=%2361DAFB
[Express.js-url]: https://expressjs.com/
[MongoDB]: https://img.shields.io/badge/MongoDB-%234ea94b.svg?style=for-the-badge&logo=mongodb&logoColor=white
[MongoDB-url]: https://www.mongodb.com/products/platform/atlas-database
[TypeScript]: https://img.shields.io/badge/typescript-%23007ACC.svg?style=for-the-badge&logo=typescript&logoColor=white
[TypeScript-url]: https://www.typescriptlang.org/
[Joi]: https://joi.dev/
[Mongoose]: https://mongoosejs.com/
[jsonwebtoken]: https://github.com/auth0/node-jsonwebtoken
[module-alias]: https://github.com/ilearnio/module-alias
[Nodemailer]: https://nodemailer.com/