2.0.4 • Published 11 months ago
authnodejs-mongodb v2.0.4
Authnodejs-mongodb Description
This Pakage contains code for user registration, login, and logout functionality. The code is implemented using Node.js and MongoDB. Below is a brief description of the code files and their functionalities.
Sample Code
Here is a sample code snippet demonstrating how to use the provided functions:
const { registerUser, loginUser, logoutUser } = require('authnodejs-mongodb');
// Register a new user
exports.registerUser = async (req, res, next) => {
const { email, password, phonenumber, customerShippingAddress, customerBillingAddress } = req.body;
const credentials = { email, password, phonenumber, customerShippingAddress, customerBillingAddress };
await registerUser(req, res, next, User, credentials);
};
// Login user
exports.loginUser = async (req, res, next) => {
await loginUser(req, res, next, User);
};
// Logout user
exports.logoutUser = (req, res, next) => {
logoutUser(req, res, next);
};
Here is Sample Code For Db Model
const mongoose = require('mongoose')
const validator = require('validator')
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const crypto = require('crypto')
const userSchema = new mongoose.Schema({
email:{
type: String,
required: [true, 'Please Enter The email'],
unique:true,
validate: [validator.isEmail,"please enter correct email address"]
},
password:{
type:String,
required:[true,"Please enter password"],
maxlength:[20,"password cannot exceed 20 characters"],
select:false,
validate: [validator.isStrongPassword,'Please enter one uppercase one lowercase and min 8 values']
},
})
userSchema.pre('save',async function(next){
if (!this.isModified('password')) {
next();
}
this.password = await bcrypt.hash(this.password,10)
})
userSchema.methods.getJwtToken = function (){
return jwt.sign({ id: this.id }, process.env.JWT_SECRET ,{
expiresIn: process.env.JWT_EXPIRES_TIME
})
}
userSchema.methods.isValidPassword = async function(EnteredPassword){
return bcrypt.compare(EnteredPassword,this.password)
}
let model = mongoose.model('user', userSchema)
module.exports = model
Make sure to replace User
with the appropriate model for user authentication.
2.0.4
11 months ago
2.0.3
11 months ago
2.0.2
11 months ago
2.0.1
11 months ago
2.0.0
11 months ago
1.0.12
11 months ago
1.0.11
11 months ago
1.0.10
11 months ago
1.0.9
11 months ago
1.0.8
11 months ago
1.0.7
11 months ago
1.0.6
11 months ago
1.0.5
11 months ago
1.0.4
11 months ago
1.0.3
11 months ago
1.0.2
11 months ago
1.0.1
11 months ago
1.0.0
11 months ago