AuthenticationModule
is a reusable class designed to facilitate common authentication tasks such as registering new users, logging in, and updating user details. It is designed to be model-agnostic, allowing it to work with any Mongoose model by passing the model as a parameter to the class constructor.
const authModule = new AuthenticationModule(Model);
Model
: A Mongoose model that represents the collection you wish to perform authentication operations on. This model must have anemail
field and apassword
field for thelogin
andregister
methods to function correctly.
The passwordHashingPlugin offered by the pxf-user-module establishes a standardized approach for securely storing and validating passwords across various models and projects. By leveraging the bcrypt algorithm, this plugin ensures that passwords are safely hashed prior to their storage in a MongoDB database through Mongoose.
It streamlines the process of password management by automatically handling the complexity of hashing operations and offering a straightforward method for comparing submitted passwords against stored hashes. This consistency in password handling enhances security practices and simplifies authentication workflows across different application contexts.
var { passwordHashingPlugin } = require('pxf-user-module').plugins
// Apply the password hashing plugin with a custom salt work factor.
schema.plugin(passwordHashingPlugin, { saltWorkFactor: 10 });
Authenticates a user based on their email and password.
email
: The user's email address.password
: The user's password.
- A promise that resolves with the user object if authentication is successful.
- An error if the user cannot be found or if the password is incorrect.
Registers a new user with the provided user data.
userData
: An object containing the new user's data, including at least anemail
and apassword
, along with any other fields your model requires.
- A promise that resolves with the newly created user object.
- An error if a user with the given email already exists.
Updates the details of an existing user.
userId
: The ID of the user to update.updates
: An object containing the fields to update.
- A promise that resolves with the updated user object.
- An error if the update operation fails.
const mongoose = require('mongoose');
const User = require('./models/User');
const { AuthenticationModule } = require('pxf-user-module')
// Connect to MongoDB
mongoose.connect('mongodb://localhost/yourDatabase', { useNewUrlParser: true, useUnifiedTopology: true });
// Instantiate the module with the User model
const auth = new AuthenticationModule(User);
// Register a new user
auth.register({
email: '[email protected]',
password: 'password123',
// other fields as needed
}).then(user => {
console.log('Registered user:', user);
}).catch(err => {
console.error('Registration error:', err);
});
// Authenticate a user
auth.login('[email protected]', 'password123').then(user => {
console.log('Logged in user:', user);
}).catch(err => {
console.error('Login error:', err);
});
This module assumes the Mongoose model provided to it includes a method for password comparison (bcrypt.compare
) and follows the conventions of having email
and password
fields. Ensure any model used with this module adheres to these requirements or adjust the module accordingly to fit your model's schema.
This documentation provides a clear overview of how to integrate and utilize the AuthenticationModule
in your Node.js applications with Mongoose models.