-
Notifications
You must be signed in to change notification settings - Fork 2
Home
The two-factor-auth repository provides a robust and easy-to-implement solution for adding two-factor authentication (2FA) to your web application. This repository includes a comprehensive implementation using the latest security standards to ensure your users' accounts are protected.
To get started with the two-factor-auth library, follow these steps:
-
Clone the Repository
git clone https://github.com/jmrashed/two-factor-auth.git cd two-factor-auth -
Install Dependencies Ensure you have Node.js installed. Then, run:
npm install
Integrating two-factor-auth into your application is straightforward. Here is a step-by-step guide:
-
Initialize 2FA in Your Application
const TwoFactorAuth = require('two-factor-auth'); const twoFactor = new TwoFactorAuth({ secretKey: 'YOUR_SECRET_KEY', algorithm: 'SHA1', digits: 6, period: 30, });
-
Generate a QR Code for User Enrollment
const user = { username: 'exampleUser' }; const qrCodeUrl = twoFactor.generateQRCodeUrl(user.username); console.log(`Scan this QR code with your authenticator app: ${qrCodeUrl}`);
-
Verify a User's Token
const userToken = '123456'; // Token entered by the user const isValid = twoFactor.verifyToken(user.username, userToken); if (isValid) { console.log('Token is valid!'); } else { console.log('Token is invalid.'); }
The two-factor-auth library can be configured with the following options:
- secretKey: The base secret key used for generating tokens.
-
algorithm: The hashing algorithm to use (
SHA1,SHA256,SHA512). - digits: The number of digits in the token (default is 6).
- period: The time period in seconds for which a token is valid (default is 30 seconds).
Example configuration:
const twoFactor = new TwoFactorAuth({
secretKey: process.env.SECRET_KEY || 'defaultSecretKey',
algorithm: 'SHA256',
digits: 6,
period: 30,
});We welcome contributions from the community! To contribute:
- Fork the Repository
-
Create a Branch
git checkout -b feature/your-feature-name
-
Commit Your Changes
git commit -m 'Add some feature' -
Push to the Branch
git push origin feature/your-feature-name
- Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.
The two-factor-auth library uses industry-standard algorithms and practices to ensure the highest level of security for your users.
Yes, you can customize the token generation by configuring the algorithm, digits, and period options.
It's crucial to keep your secret key secure. If it's compromised, generate a new key and update your configuration.