Skip to content

fix: typescript issue and README style #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -12,32 +12,30 @@ 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:

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)

16 changes: 8 additions & 8 deletions controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}