-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
37 lines (30 loc) · 1.03 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const urlSlug = require("url-slug");
const removeMd = require("remove-markdown");
//Helper function to hash passwords
const hashPassword = async (password) => {
const salt = await bcrypt.genSalt(10);
const hashPassword = await bcrypt.hash(password, salt);
return hashPassword;
};
//Helper function to compare passwords
const passwordsAreEqual = async (password, hashedPassword) => {
return bcrypt.compare(password, hashedPassword);
};
//Helper function to create error messsages
const generateError = (message = "") => ({ error: message });
//Helper function to generate a token
const generateToken = (data, secret) => jwt.sign(data, secret);
//Helper function to generate url slug
const generateSlug = (text) => urlSlug(text);
//Helper function to extract summary
const extractSummary = (text, length) => removeMd(text).substring(0, length);
module.exports = {
hashPassword,
generateError,
passwordsAreEqual,
generateToken,
generateSlug,
extractSummary,
};