crypto-flavor
v1.0.4
Published
digest password & encrypt/decrypt salt
Downloads
8
Maintainers
Readme
crypto-flavor
Digest password, encrypt/decrypt salt
Usage
npm install --save crypto-flavor
user creation example
const express = require("express");
const crypto = require("crypto-flavor");
const randomstring = require("randomstring");
const mongoose = require("mongoose");
const router = express.Router();
const User = mongoose.model("User");
router.put("/user", (req, res) => {
if(req.body.password == null){
res.status(400).send();
}else{
let salt = randomstring.generate();
req.body.salt = crypto.encryptSalt(salt, req.body.password);
req.body.password = crypto.digestPassword(req.body.password, salt);
let user = new User(req.body);
user.save((err, user) => {
if(err){
res.status(400).send(err);
}else{
res.status(201).send(user._id);
}
});
}
});
user auth example
const express = require("express");
const crypto = require("crypto-flavor");
const randomstring = require("randomstring");
const mongoose = require("mongoose");
const jwt = require("jwt");
const env = require("../env");
const router = express.Router();
const User = mongoose.model("User");
router.post("/user/login", (req, res) => {
User.find({
email: req.body.email
}).limit(1)
.exec((err, users) => {
if(!users || users.length == 0){
res.status(401).send();
}else{
user = users[0];
try{
let digestedPassword = crypto.digestPassword(req.body.password, crypto.decryptSalt(user.salt, req.body.password));
if(digestedPassword === user.password){
let token = jwt.sign({
user: user
}, env.jwtSecret);
res.status(200).send({
token: token,
userId: user._id
});
}
else{
res.status(401).send();
}
}catch(err){
res.status(401).send(err);
}
}
});
});