From dfd8b156de61d34e1675636f1afefe8e2fb5766a Mon Sep 17 00:00:00 2001 From: Ahabibullah Date: Tue, 3 Sep 2024 11:45:10 +0300 Subject: [PATCH] fix: typescript issue and README style --- README.md | 26 ++++++++++++-------------- controllers/auth.controller.ts | 16 ++++++++-------- tsconfig.json | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 tsconfig.json diff --git a/README.md b/README.md index ffa7dcc..0077c03 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # How to Implement (2FA) Two-factor Authentication in Node.js -This article will teach you how to secure a Node.js API by implementing two-factor authentication (2FA) system using tokens generated by Google Authenticator or Authy. +This article will teach you how to secure a Node.js API by implementing two-factor authentication (2FA) system using tokens generated by Google Authenticator or Authy. The one-time passcode (OTP) can be delivered via different methods like SMS but we will use Google Authenticator or Authy to reduce the complexity of the project. ![How to Implement (2FA) Two-factor Authentication in Node.js](https://codevoweb.com/wp-content/uploads/2022/10/How-to-Implement-2FA-Two-factor-Authentication-in-Node.js.png) @@ -12,27 +12,26 @@ The one-time passcode (OTP) can be delivered via different methods like SMS but - Run the Node.js 2FA App Locally - Run the Frontend Built with React.js - Two-factor Authentication in Node.js Flow - - Setup the 2FA feature - - Scan the QRCode - - Verify the OTP token - - Verify the OTP token - - Disable the 2FA Feature + - Setup the 2FA feature + - Scan the QRCode + - Verify the OTP token + - Verify the OTP token + - Disable the 2FA Feature - Setup the Node.js Project - Setup Prisma ORM - Create the Prisma Database Model - Database Migration with Prisma - Setup the Node.js Express App - Create the Node.js Route Controllers - - Register User - - Sign-in User - - Generate the OTP - - Verify the OTP - - Validate the OTP - - Disable the OTP Feature + - Register User + - Sign-in User + - Generate the OTP + - Verify the OTP + - Validate the OTP + - Disable the OTP Feature - Create the Express API Routes - Add the Routes to the Middleware Stack - Read the entire article here: [https://codevoweb.com/two-factor-authentication-2fa-in-nodejs](https://codevoweb.com/two-factor-authentication-2fa-in-nodejs) Related articles: @@ -40,4 +39,3 @@ Related articles: 1. [How to Implement Two-factor Authentication (2FA) in React.js](https://codevoweb.com/two-factor-authentication-2fa-in-reactjs) 2. [Two-factor Authentication (2FA) in FastAPI and Python](https://codevoweb.com/two-factor-authentication-2fa-in-fastapi-and-python) 3. [How to Implement (2FA) Two-factor Authentication in Golang](https://codevoweb.com/two-factor-authentication-2fa-in-golang) - diff --git a/controllers/auth.controller.ts b/controllers/auth.controller.ts index b824832..cfb7e48 100644 --- a/controllers/auth.controller.ts +++ b/controllers/auth.controller.ts @@ -1,9 +1,9 @@ import crypto from "crypto"; -import { Prisma } from "@prisma/client"; -import { Request, Response, NextFunction } from "express"; import { prisma } from "../server"; import * as OTPAuth from "otpauth"; import { encode } from "hi-base32"; +import { Prisma } from "@prisma/client"; +import { Request, Response, NextFunction } from "express"; const RegisterUser = async ( req: Request, @@ -25,7 +25,7 @@ const RegisterUser = async ( status: "success", message: "Registered successfully, please login", }); - } catch (error) { + } catch (error: any) { if (error instanceof Prisma.PrismaClientKnownRequestError) { if (error.code === "P2002") { return res.status(409).json({ @@ -63,7 +63,7 @@ const LoginUser = async (req: Request, res: Response, next: NextFunction) => { otp_enabled: user.otp_enabled, }, }); - } catch (error) { + } catch (error: any) { res.status(500).json({ status: "error", message: error.message, @@ -114,7 +114,7 @@ const GenerateOTP = async (req: Request, res: Response) => { base32: base32_secret, otpauth_url, }); - } catch (error) { + } catch (error: any) { res.status(500).json({ status: "error", message: error.message, @@ -169,7 +169,7 @@ const VerifyOTP = async (req: Request, res: Response) => { otp_enabled: updatedUser.otp_enabled, }, }); - } catch (error) { + } catch (error: any) { res.status(500).json({ status: "error", message: error.message, @@ -209,7 +209,7 @@ const ValidateOTP = async (req: Request, res: Response) => { res.status(200).json({ otp_valid: true, }); - } catch (error) { + } catch (error: any) { res.status(500).json({ status: "error", message: error.message, @@ -245,7 +245,7 @@ const DisableOTP = async (req: Request, res: Response) => { otp_enabled: updatedUser.otp_enabled, }, }); - } catch (error) { + } catch (error: any) { res.status(500).json({ status: "error", message: error.message, diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..af57aeb --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "commonjs", + "outDir": "./dist", + "strict": true, + "types": ["node"], + "moduleResolution": "node", + "esModuleInterop": true, + "skipLibCheck": true, + "lib": ["es5", "es6", "dom"], + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "strictPropertyInitialization": false, + "forceConsistentCasingInFileNames": true, + "baseUrl": ".", + "paths": { + "@/*": ["/*"] + } + }, + "include": ["/**/*.ts"], + "exclude": ["node_modules"] +}