Skip to content

Commit e792aa8

Browse files
committed
Base version 0.2.0.
1 parent d670dd4 commit e792aa8

File tree

114 files changed

+1514
-830
lines changed

Some content is hidden

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

114 files changed

+1514
-830
lines changed

.bowerrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"directory": "resources/assets/bower"
3+
}

.gitignore

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
.idea/*
2-
node_modules/*
3-
public/dist/*
4-
bower_components/*
5-
/config.js
6-
/config/*
1+
.idea
2+
node_modules
3+
!node_modules/app
4+
!node_modules/config
5+
!node_modules/modules
6+
!node_modules/resources
7+
public
8+
resources/assets/bower
9+
config/config-*.js

License

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MEANStack bringing together the best of MEAN MongoDB, Express, AngularJS and Node.js
1+
MEANStack.io bringing together the best of MEAN MongoDB, Express, AngularJS and Node.js
22
The purpose of the application is to facilitate the development offering the best of MEAN with a stack of packages, frameworks, libraries and strategies.
33

44
The MIT License (MIT)

app.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,32 @@
33
var express = require('express'),
44
app = express(),
55
path = require('path'),
6+
compression = require('compression'),
67
favicon = require('serve-favicon'),
78
logger = require('morgan'),
89
cookieParser = require('cookie-parser'),
910
bodyParser = require('body-parser'),
1011
session = require('express-session'),
1112
store = new session.MemoryStore(),
12-
passport = require('passport'),
13-
response = require('./modules/response'),
14-
passportStrategies = require('./passport'),
15-
policies = require('./routes/policies'),
16-
routes = require('./routes'),
17-
settings = require('./config'),
18-
hbs = require('express-handlebars').create(
13+
middleware = require('app/http/middleware'),
14+
routes = require('app/http/routes'),
15+
settings = require('config'),
16+
hbs = require('express-hbs'),
17+
hbsEngine = hbs.express4(
1918
{
2019
extname: ".hbs",
21-
partialsDir: path.join(__dirname, 'views/partials/')
20+
layoutsDir: path.join(__dirname, 'resources/views/layouts/'),
21+
partialsDir: path.join(__dirname, 'resources/views/partials/')
2222
}
23-
);
23+
),
24+
hbsHelpers = require('app/helpers');
2425

25-
app.engine('hbs', hbs.engine);
26+
app.engine('hbs', hbsEngine);
2627
app.set('view engine', 'hbs');
27-
app.set('views', path.join(__dirname, 'views'));
28-
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
28+
hbsHelpers();
29+
app.use(compression());
30+
app.set('views', path.join(__dirname, 'resources/views'));
31+
app.use(favicon(path.join(__dirname, 'public', 'favicon.png')));
2932
app.use(express.static(path.join(__dirname, 'public')));
3033
app.use(logger('dev'));
3134
app.use(bodyParser.json());
@@ -38,36 +41,26 @@ app.use(session(
3841
saveUninitialized: false,
3942
store: store,
4043
cookie: {
41-
httpOnly: true, maxAge: 2419200000
44+
httpOnly: true,
45+
maxAge: (typeof settings.cookie.maxAge !== 'undefined')? settings.cookie.maxAge : 2419200000
4246
}
4347
}
4448
)
4549
);
4650

4751
/**
48-
* Module Response
52+
* Init Middleware.
4953
*/
50-
app.use(response());
54+
middleware(app);
5155

52-
/**
53-
* Init Passaport
54-
*/
55-
app.use(passport.initialize());
56-
app.use(passport.session());
57-
passportStrategies(passport);
58-
59-
/**
60-
* Module Auth
61-
*/
62-
policies(app);
6356

6457
/**
65-
* Initialize Routes
58+
* Initialize Routes.
6659
*/
6760
routes(app);
6861

6962
/**
70-
* error handler
63+
* Error handler.
7164
*/
7265
app.use(function (err, req, res, next) {
7366
res.status(err.status || 500);

app/helpers/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/**
4+
* Helpers for Handlebars.
5+
*
6+
* Example:
7+
* {{ foo }} or {{ ucfirst variable }}
8+
*
9+
* More documentation:
10+
* http://handlebarsjs.com/expressions.html#helpers,
11+
* https://github.com/barc/express-hbs
12+
*/
13+
var hbs = require('express-hbs'),
14+
ucfirst = require('./ucfirst');
15+
16+
module.exports = function () {
17+
18+
// Make a string's first character uppercase.
19+
ucfirst();
20+
21+
};

app/helpers/ucfirst.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var hbs = require('express-hbs');
4+
5+
module.exports = function () {
6+
/**
7+
* Make a string's first character uppercase.
8+
*
9+
* @param str
10+
* @returns {string}
11+
*/
12+
hbs.registerHelper('ucfirst', function (str) {
13+
var f = str.charAt(0)
14+
.toUpperCase();
15+
return f + str.substr(1);
16+
});
17+
};
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'use strict';
2+
3+
var express = require('express'),
4+
router = express.Router(),
5+
User = require('app/models/user').User,
6+
settings = require('config'),
7+
mailer = require('app/mail'),
8+
mailerSmtp = mailer.smtp(settings.mail),
9+
mailerSendMail = mailer.sendMail,
10+
crypto = require('crypto'),
11+
login = require('modules/account/login');
12+
13+
14+
/**
15+
* Forgot password.
16+
* If user logged with application OAuth send email with application logged.
17+
* If logged with application local send email with token for reset password.
18+
*/
19+
router.post('/', function (req, res, next) {
20+
21+
var data = req.body,
22+
validaEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
23+
24+
if (!validaEmail.test(data.email)) {
25+
req.response.setMsg('Please enter a valid email address !');
26+
return res.json(req.response.return());
27+
}
28+
29+
User.findOne({email: new RegExp('^' + data.email + '$', "i")}, function (err, user) {
30+
if (err) {
31+
return next(err);
32+
}
33+
34+
if (!user) {
35+
req.response.setMsg('E-mail "' + data.email + '" not registered.');
36+
return res.json(req.response.return());
37+
}
38+
39+
var paramsTemplate = {
40+
user: user,
41+
settings: settings
42+
},
43+
paramsSendMail = {
44+
from: '"' + settings.name + '" <' + settings.mail.smtp.auth.user + '>',
45+
to: data.email
46+
},
47+
errorSendMail = 'Error sending email, try again, if still does not work please contact support !',
48+
successSendMail = 'Message has been sent to the e-mail ' + user.email;
49+
50+
if (typeof user.oauth.provider !== 'undefined') {
51+
mailerSendMail(
52+
mailerSmtp,
53+
'forgot-account/logged-with-application',
54+
paramsTemplate,
55+
paramsSendMail,
56+
function (success) {
57+
if (!success) {
58+
req.response.setMsg(errorSendMail);
59+
return res.json(req.response.return());
60+
}
61+
62+
req.response.setSuccess();
63+
req.response.setMsg(successSendMail);
64+
return res.json(req.response.return());
65+
}
66+
);
67+
68+
} else {
69+
crypto.randomBytes(20, function (err, buf) {
70+
user.resetPassword.token = buf.toString('hex');
71+
// 1 hour expires token
72+
user.resetPassword.expires = Date.now() + 3600000;
73+
// Get IP address client.
74+
user.resetPassword.ip = req.header('x-forwarded-for') || req.connection.remoteAddress;
75+
user.save(function (err) {
76+
if (err) {
77+
return next(err);
78+
}
79+
80+
paramsTemplate.user = user;
81+
82+
mailerSendMail(
83+
mailerSmtp,
84+
'forgot-account/link-reset-password',
85+
paramsTemplate,
86+
paramsSendMail,
87+
function (success) {
88+
if (!success) {
89+
req.response.setMsg(errorSendMail);
90+
return res.json(req.response.return());
91+
}
92+
93+
req.response.setSuccess();
94+
req.response.setMsg(successSendMail);
95+
return res.json(req.response.return());
96+
}
97+
);
98+
});
99+
});
100+
}
101+
});
102+
});
103+
104+
/**
105+
* Reset Password.
106+
*/
107+
router.post('/reset', function (req, res, next) {
108+
109+
var data = req.body,
110+
remoteAddress = req.header('x-forwarded-for') || req.connection.remoteAddress;
111+
112+
if (!data.token || (data.token).length < 20) {
113+
req.response.setMsg('Token invalid !');
114+
}
115+
if (!data.password || (data.password).length < 6) {
116+
req.response.setMsg('Your password must have more than 6 characters !');
117+
}
118+
if (data.password != data.repassword) {
119+
req.response.setMsg('Make sure your passwords are the same !');
120+
}
121+
if (req.response.hasMsg()) {
122+
return res.json(req.response.return());
123+
}
124+
125+
User.findOne(
126+
{
127+
'resetPassword.token': data.token,
128+
'resetPassword.expires': {$gt: Date.now()},
129+
'resetPassword.ip': remoteAddress
130+
},
131+
function (err, user) {
132+
if (err) {
133+
return next(err);
134+
}
135+
if (!user) {
136+
req.response.setMsg('Token is invalid or has expired.');
137+
return res.json(req.response.return());
138+
}
139+
140+
user.password = user.generateHash(data.password);
141+
user.resetPassword.token = null;
142+
user.resetPassword.expires = null;
143+
user.resetPassword.ip = null;
144+
user.save(function (err) {
145+
req.response.setSuccess();
146+
login(req, res, next, err, user);
147+
});
148+
}
149+
);
150+
});
151+
152+
module.exports = router;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var express = require('express'),
4+
router = express.Router();
5+
6+
/**
7+
* Logoff.
8+
*/
9+
router.post('/', function (req, res) {
10+
req.setCookie(false,
11+
function () {
12+
req.logout();
13+
req.response.setSuccess();
14+
res.json(req.response.return());
15+
}
16+
);
17+
});
18+
19+
module.exports = router;

0 commit comments

Comments
 (0)