Skip to content

Commit 8da18ba

Browse files
committed
Merge pull request #1 from GeneralZero/master
Changed Views to accept CSRF token
2 parents bca3fd4 + 9379526 commit 8da18ba

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

app.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ app.use(express.urlencoded());
6969
app.use(expressValidator());
7070
app.use(express.methodOverride());
7171
app.use(express.session({
72-
secret: 'your secret code',
72+
secret: secrets.sessionSecret,
7373
store: new MongoStore({
7474
db: mongoose.connection.db,
7575
auto_reconnect: true
7676
})
7777
}));
78+
app.use(express.csrf());
7879
app.use(passport.initialize());
7980
app.use(passport.session());
8081
app.use(function(req, res, next) {
8182
res.locals.user = req.user;
83+
res.locals.token = req.csrfToken();
8284
next();
8385
});
8486
app.use(flash());
@@ -90,6 +92,13 @@ app.use(function(req, res) {
9092
});
9193
app.use(express.errorHandler());
9294

95+
/*Helper function for CSRF
96+
app.dynamicHelpers({
97+
token: function(req, res) {
98+
return req.session._csrf;
99+
}
100+
});*/
101+
93102
/**
94103
* Application routes.
95104
*/

config/secrets.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module.exports = {
22
db: 'localhost',
33

4+
sessionSecret: "Your Session Secret goes here",
5+
46
sendgrid: {
57
user: 'Your SendGrid Username',
68
password: 'Your SendGrid Password'

views/account/login.jade

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ block content
2424
.form-group
2525
label.control-label(for='username') Password
2626
input.form-control(type='password', name='password', id='password', placeholder='Password')
27+
.form-group
28+
input.form-control(type='hidden', name='_csrf', value=token)
2729
.form-group
2830
button.btn.btn-primary(type='submit')
2931
i.fa.fa-unlock-alt

views/account/profile.jade

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ block content
3030
label.col-xs-2.control-label(for='website') Website
3131
.col-xs-4
3232
input.form-control(type='text', name='website', id='website', value='#{user.profile.website}')
33+
.form-group
34+
input.form-control(type='hidden', name='_csrf', value=token)
3335
.form-group
3436
.col-xs-offset-2.col-xs-4
3537
button.btn.btn.btn-primary(type='submit') Update Profile
3638

3739

3840

41+
3942
.page-header
4043
h3 Change Password
4144

@@ -48,6 +51,8 @@ block content
4851
label.col-xs-3.control-label(for='confirmPassword') Confirm Password
4952
.col-xs-4
5053
input.form-control(type='password', name='confirmPassword', id='confirmPassword')
54+
.form-group
55+
input.form-control(type='hidden', name='_csrf', value=token)
5156
.form-group
5257
.col-xs-offset-3.col-xs-4
5358
button.btn.btn.btn-primary(type='submit') Change Password
@@ -80,4 +85,4 @@ block content
8085
if user.github
8186
p: a.text-danger(href='/account/unlink/github') Unlink your GitHub account
8287
else
83-
p: a(href='/auth/github') Link your GitHub account
88+
p: a(href='/auth/github') Link your GitHub account

views/account/signup.jade

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ block content
1515
label.col-sm-3.control-label(for='username') Confirm Password
1616
.col-sm-7
1717
input.form-control(type='password', name='confirmPassword', id='confirmPassword', placeholder='Confirm Password')
18+
.form-group
19+
input.form-control(type='hidden', name='_csrf', value=token)
1820
.form-group
1921
.col-sm-offset-3.col-sm-7
2022
button.btn.btn-success(type='submit')

views/contact.jade

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ block content
1717
label(class='col-sm-2 control-label', for='contactBody') Body
1818
.col-sm-8
1919
textarea.form-control(type='text', name='message', id='message', rows='7')
20+
.form-group
21+
input.form-control(type='hidden', name='_csrf', value=token)
2022
.form-group
2123
.col-sm-offset-2.col-sm-8
2224
button.btn.btn-default(type='submit')

0 commit comments

Comments
 (0)