1. add login api
2. add get channel list api
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
const config = require('@config/index')
|
||||
const bcrypt = require('bcrypt')
|
||||
const {
|
||||
toInt
|
||||
} = require('@libs/route-utils')
|
||||
|
||||
/**
|
||||
* hash password func
|
||||
* @param {string} pass password string
|
||||
* @return {string}
|
||||
*/
|
||||
const hashPassword = async (pass = '') => {
|
||||
if (typeof pass !== 'string' || pass.trim().length === 0) return null
|
||||
let saltRound = toInt(config.salt_round, 1, 1)
|
||||
// gen salt
|
||||
let salt = await new Promise((resolve, reject) => {
|
||||
bcrypt.genSalt(saltRound, (err, saltStr) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(saltStr)
|
||||
})
|
||||
})
|
||||
|
||||
let passHash = new Promise((resolve, reject) => {
|
||||
bcrypt.hash(pass, salt, (err, hash) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(hash)
|
||||
})
|
||||
})
|
||||
|
||||
return passHash
|
||||
}
|
||||
|
||||
/**
|
||||
* compare password and hash
|
||||
* @param {string} password password string
|
||||
* @param {string} hash hash string
|
||||
* @return {boolean}
|
||||
*/
|
||||
const comparePassword = async (password = '', hash = '') => {
|
||||
if (typeof password !== 'string' || password.trim().length === 0) return null
|
||||
if (typeof hash !== 'string' || hash.trim().length === 0) return null
|
||||
let check = new Promise((resolve, reject) => {
|
||||
bcrypt.compare(password, hash, (err, res) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
})
|
||||
|
||||
return !!check
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hashPassword,
|
||||
comparePassword
|
||||
}
|
||||
Reference in New Issue
Block a user