Skip to content
Browse files

Work on setting up user model

  • Loading branch information...
1 parent b2b20ed commit 3929a8eb53326f44354ce9bb0aa52e24a87af5c4 David Petersen committed Feb 11, 2012
Showing with 47 additions and 3 deletions.
  1. +44 −2 lib/irchandler.js
  2. +3 −1 package.json
View
46 lib/irchandler.js
@@ -1,4 +1,16 @@
-var irc = require('irc');
+var irc = require('irc'),
+ bcrypt = require('bcrypt'),
+ mongoose = require('mongoose');
+
+Schema = mongoose.Schema;
+
+var UserSchema = new Schema({
+ username: String,
+ password: String
+});
+
+var User = mongoose.model('User', UserSchema);
+mongoose.connect('mongodb://localhost/my_database');
var irchandler = exports.irchandler = function(socket) {
// Events to signal TO the front-end
@@ -20,9 +32,40 @@ var irchandler = exports.irchandler = function(socket) {
'netError': ['message'],
};
+ socket.on('register', function(data) {
+ bcrypt.genSalt(10, function(err, salt) {
+ bcrypt.hash(data.password, salt, function(err, hash) {
+ // Store hash in your password DB.
+ var user = new User();
+ user.username = data.username;
+ user.password = hash;
+ user.save();
+ console.log(user);
+ });
+ });
+ });
+
+ socket.on('login', function(data){
+ console.log(data);
+ User.find({}, function(err, docs) { console.log(docs); });
+ User.findOne({username: data.username}, function(err, user) {
+ console.log(user);
+ bcrypt.compare(data.password, user.password, function(err, res) {
+ socket.emit('loginStatus', {res: res});
+ if(res === true){
+ client.logged_in = true;
+ console.log('logged in');
+ } else {
+ console.log('Wrong password');
+ }
+ });
+ });
+ });
+
socket.on('connect', function(data) {
var client = new irc.Client(data.server, data.nick, {
debug: true,
+ logged_in: false,
showErrors: true,
channels: data.channels,
userName: 'subway',
@@ -39,7 +82,6 @@ var irchandler = exports.irchandler = function(socket) {
});
socket.on('action', function(data) {
- console.log(data);
client.action(data.target, data.message);
socket.emit('message', {to:data.target, from: client.nick,
text:'\u0001ACTION ' + data.message})
View
4 package.json
@@ -16,6 +16,8 @@
"jade": "",
"connect-assets": "",
"irc": "git://github.com/martynsmith/node-irc.git",
- "socket.io": ""
+ "socket.io": "",
+ "mongoose": "",
+ "bcrypt": ""
}
}

0 comments on commit 3929a8e

Please sign in to comment.
Something went wrong with that request. Please try again.