Last active
September 13, 2019 12:35
-
-
Save mikarapace/aa776994b09f87a452676265d0a41532 to your computer and use it in GitHub Desktop.
Learning ES8/Node.js/Bluebird and Redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Bluebird = require('bluebird') | |
Bluebird.config({ | |
longStackTraces: true | |
}) | |
module.exports = Bluebird |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function logError(err) { | |
return console.error('Error caught !', err) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Redis = require('redis') | |
var Bluebird = require('bluebird') | |
Bluebird.promisifyAll(Redis.RedisClient.prototype) | |
Bluebird.promisifyAll(Redis.Multi.prototype) | |
var client = Redis.createClient() | |
client | |
.on('connect', function () { | |
console.log('connected') | |
}) | |
module.exports = client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var User = require('./user.model') | |
var UserService = require('./user.service') | |
var redis = require('./redis.config') | |
var user = new User(null, 'test', 'testDisplay', 'testpasswd'); | |
async function test () { | |
try { | |
user = await User.createUser(user) | |
console.log('createUser : ' + user) | |
var deleteUser = await User.deleteUser(user.id) | |
console.log('deleteUser : ' + deleteUser) | |
// user.password was replaced with hash password during User.createUser | |
user.password = 'testpasswd' | |
user = await User.createUser(user) | |
console.log('createUser : ' + user) | |
var userFound = await User.findUserById(user.id) | |
console.log('userFound : ' + userFound) | |
var userIdFound = await User.findUserIdByUsername(user.username) | |
console.log('userIdFound : ' + userIdFound) | |
var authenticateUserdFalsePromise = UserService.authenticateUser(user.username, 'mauvaisPwd') | |
var authenticateUserTruePromise = UserService.authenticateUser(user.username, 'testpasswd') | |
var [authenticateUserFalse, authenticateUserTrue] = await Promise.all([authenticateUserdFalsePromise, authenticateUserTruePromise]) | |
console.log('authenticateUserFalse : ' + authenticateUserFalse + ', authenticateUserTrue : ' + authenticateUserTrue) | |
return redis.flushallAsync() | |
} catch(err) { | |
console.error('Error caught !', err) | |
} | |
}; | |
test() | |
.then(() => console.log('Redis - FLUSHALL OK')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redis = require('./redis.config') | |
var bcrypt = require('bcrypt') | |
var uuidv4 = require('uuid/v4') | |
var Promise = require('bluebird') | |
module.exports = class User { | |
constructor (id, username, displayName, password) { | |
this.id = id | |
this.username = username | |
this.displayName = displayName | |
this.password = password | |
} | |
static async findUserById (id) { | |
var user = await redis.hgetallAsync('user:' + id) | |
console.log('findUserById - id : ' + id) | |
return Promise.resolve(new User(user.id, user.username, user.displayName, user.password)) | |
} | |
static async findUserIdByUsername (username) { | |
var username = await redis.getAsync('users:' + username) | |
console.log('findUserIdByUsername - username : ' + username) | |
return Promise.resolve(username) | |
} | |
static async createUser (user) { | |
var alreadyExistingUsername = await redis.existsAsync('users:' + user.username) | |
if (alreadyExistingUsername === 1) return Promise.reject(new Error('createUser(' + user.username + ') Error : Username Already Exists')) | |
user.id = uuidv4() | |
user.password = await bcrypt.hash(user.password, 10) | |
await Promise.all([ | |
redis.setAsync('users:' + user.username, user.id), | |
redis.hmsetAsync('user:' + user.id, user)]) | |
console.log('createUser - id : ' + user.id) | |
return Promise.resolve(user) | |
} | |
static async deleteUser (id) { | |
var userUsername = await redis.hgetAsync('user:' + id, 'username') | |
var delResult = await redis.delAsync(['users:' + userUsername, 'user:' + id]) | |
console.log('deleteUser - id :' + id) | |
return Promise.resolve(delResult) | |
} | |
toString () { | |
return 'user : [id:' + this.id + ', username:' + this.username + ', displayName:' + this.displayName + ', password:' + this.password + ']' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var User = require('./user.model') | |
var bcrypt = require('bcrypt') | |
var logError = require('./errorHandling.config') | |
async function findUserFromUsername (username) { | |
try { | |
var userId = await User.findUserIdByUsername(username) | |
if (userId != null) { | |
return User.findUserById(userId) | |
} | |
return Promise.reject(new Error('No User Id found for this username')) | |
} catch (err) { | |
logError(err) | |
} | |
} | |
module.exports = { | |
authenticateUser: async function (username, password) { | |
try { | |
var user = await findUserFromUsername(username) | |
return bcrypt.compare(password, user.password) | |
} catch(err) { | |
logError(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment