Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit e6f8553

Browse files
committed
First Commit
1 parent 58a7ebe commit e6f8553

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1731
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: ./node_modules/.bin/forever -m 5 server.js

app/.DS_Store

6 KB
Binary file not shown.

app/controllers/.DS_Store

6 KB
Binary file not shown.

app/controllers/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
5+
var mongoose = require('mongoose')
6+
, async = require('async')
7+
, _ = require('underscore')
8+
9+
10+
exports.render = function(req, res){
11+
res.render('index', {
12+
user: req.user ? JSON.stringify(req.user) : "null"
13+
})
14+
}

app/controllers/users.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var mongoose = require('mongoose')
7+
, User = mongoose.model('User')
8+
9+
//exports.signin = function (req, res) {}
10+
11+
/**
12+
* Auth callback
13+
*/
14+
15+
exports.authCallback = function (req, res, next) {
16+
res.redirect('/')
17+
}
18+
19+
/**
20+
* Show login form
21+
*/
22+
23+
exports.signin = function (req, res) {
24+
res.render('users/signin', {
25+
title: 'Signin',
26+
message: req.flash('error')
27+
})
28+
}
29+
30+
/**
31+
* Show sign up form
32+
*/
33+
34+
exports.signup = function (req, res) {
35+
res.render('users/signup', {
36+
title: 'Sign up',
37+
user: new User()
38+
})
39+
}
40+
41+
/**
42+
* Logout
43+
*/
44+
45+
exports.signout = function (req, res) {
46+
req.logout()
47+
res.redirect('/')
48+
}
49+
50+
/**
51+
* Session
52+
*/
53+
54+
exports.session = function (req, res) {
55+
res.redirect('/')
56+
}
57+
58+
/**
59+
* Create user
60+
*/
61+
62+
exports.create = function (req, res) {
63+
var user = new User(req.body)
64+
user.provider = 'local'
65+
user.save(function (err) {
66+
if (err) {
67+
return res.render('users/signup', { errors: err.errors, user: user })
68+
}
69+
req.logIn(user, function(err) {
70+
if (err) return next(err)
71+
return res.redirect('/')
72+
})
73+
})
74+
}
75+
76+
/**
77+
* Show profile
78+
*/
79+
80+
exports.show = function (req, res) {
81+
var user = req.profile
82+
res.render('users/show', {
83+
title: user.name,
84+
user: user
85+
})
86+
}
87+
88+
exports.me = function (req, res) {
89+
res.jsonp(req.user || null);
90+
}
91+
92+
/**
93+
* Find user by id
94+
*/
95+
96+
exports.user = function (req, res, next, id) {
97+
User
98+
.findOne({ _id : id })
99+
.exec(function (err, user) {
100+
if (err) return next(err)
101+
if (!user) return next(new Error('Failed to load User ' + id))
102+
req.profile = user
103+
next()
104+
})
105+
}

app/mailer/notify.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var mongoose = require('mongoose')
7+
, Notifier = require('notifier')
8+
, env = process.env.NODE_ENV || 'development'
9+
, config = require('../../config/config')[env]
10+
11+
/**
12+
* Notification methods
13+
*/
14+
15+
var Notify = {
16+
17+
/**
18+
* Comment notification
19+
*
20+
* @param {Object} options
21+
* @param {Function} cb
22+
* @api public
23+
*/
24+
25+
comment: function (options, cb) {
26+
var article = options.article
27+
var author = article.user
28+
var user = options.currentUser
29+
var notifier = new Notifier(config.notifier)
30+
31+
var obj = {
32+
to: author.email,
33+
from: 'your@product.com',
34+
subject: user.name + ' added a comment on your article ' + article.title,
35+
alert: user.name + ' says: "' + options.comment,
36+
locals: {
37+
to: author.name,
38+
from: user.name,
39+
body: options.comment,
40+
article: article.name
41+
}
42+
}
43+
44+
// for apple push notifications
45+
/*notifier.use({
46+
APN: true
47+
parseChannels: ['USER_' + author._id.toString()]
48+
})*/
49+
50+
notifier.send('comment', obj, cb)
51+
}
52+
}
53+
54+
/**
55+
* Expose
56+
*/
57+
58+
module.exports = Notify

app/mailer/templates/comment.jade

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
p Hello #{to}
2+
3+
p #{from} has added a comment "#{body}" on your article #{article}
4+
5+
p Cheers

app/models/user.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var mongoose = require('mongoose')
7+
, Schema = mongoose.Schema
8+
, crypto = require('crypto')
9+
, _ = require('underscore')
10+
, authTypes = ['github', 'twitter', 'facebook', 'google']
11+
12+
/**
13+
* User Schema
14+
*/
15+
16+
var UserSchema = new Schema({
17+
name: String,
18+
email: String,
19+
username: String,
20+
provider: String,
21+
hashed_password: String,
22+
salt: String,
23+
facebook: {},
24+
twitter: {},
25+
github: {},
26+
google: {}
27+
})
28+
29+
/**
30+
* Virtuals
31+
*/
32+
33+
UserSchema
34+
.virtual('password')
35+
.set(function(password) {
36+
this._password = password
37+
this.salt = this.makeSalt()
38+
this.hashed_password = this.encryptPassword(password)
39+
})
40+
.get(function() { return this._password })
41+
42+
/**
43+
* Validations
44+
*/
45+
46+
var validatePresenceOf = function (value) {
47+
return value && value.length
48+
}
49+
50+
// the below 4 validations only apply if you are signing up traditionally
51+
52+
UserSchema.path('name').validate(function (name) {
53+
// if you are authenticating by any of the oauth strategies, don't validate
54+
if (authTypes.indexOf(this.provider) !== -1) return true
55+
return name.length
56+
}, 'Name cannot be blank')
57+
58+
UserSchema.path('email').validate(function (email) {
59+
// if you are authenticating by any of the oauth strategies, don't validate
60+
if (authTypes.indexOf(this.provider) !== -1) return true
61+
return email.length
62+
}, 'Email cannot be blank')
63+
64+
UserSchema.path('username').validate(function (username) {
65+
// if you are authenticating by any of the oauth strategies, don't validate
66+
if (authTypes.indexOf(this.provider) !== -1) return true
67+
return username.length
68+
}, 'Username cannot be blank')
69+
70+
UserSchema.path('hashed_password').validate(function (hashed_password) {
71+
// if you are authenticating by any of the oauth strategies, don't validate
72+
if (authTypes.indexOf(this.provider) !== -1) return true
73+
return hashed_password.length
74+
}, 'Password cannot be blank')
75+
76+
77+
/**
78+
* Pre-save hook
79+
*/
80+
81+
UserSchema.pre('save', function(next) {
82+
if (!this.isNew) return next()
83+
84+
if (!validatePresenceOf(this.password)
85+
&& authTypes.indexOf(this.provider) === -1)
86+
next(new Error('Invalid password'))
87+
else
88+
next()
89+
})
90+
91+
/**
92+
* Methods
93+
*/
94+
95+
UserSchema.methods = {
96+
97+
/**
98+
* Authenticate - check if the passwords are the same
99+
*
100+
* @param {String} plainText
101+
* @return {Boolean}
102+
* @api public
103+
*/
104+
105+
authenticate: function(plainText) {
106+
return this.encryptPassword(plainText) === this.hashed_password
107+
},
108+
109+
/**
110+
* Make salt
111+
*
112+
* @return {String}
113+
* @api public
114+
*/
115+
116+
makeSalt: function() {
117+
return Math.round((new Date().valueOf() * Math.random())) + ''
118+
},
119+
120+
/**
121+
* Encrypt password
122+
*
123+
* @param {String} password
124+
* @return {String}
125+
* @api public
126+
*/
127+
128+
encryptPassword: function(password) {
129+
if (!password) return ''
130+
return crypto.createHmac('sha1', this.salt).update(password).digest('hex')
131+
}
132+
}
133+
134+
mongoose.model('User', UserSchema)

app/views/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)