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

Commit

Permalink
#326 merge latest
Browse files Browse the repository at this point in the history
  • Loading branch information
fyockm committed Feb 1, 2014
2 parents 412d6b3 + 8499c45 commit c2330e5
Show file tree
Hide file tree
Showing 25 changed files with 226 additions and 158 deletions.
7 changes: 6 additions & 1 deletion .bowerrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"directory": "public/lib"
"directory": "public/lib",
"storage": {
"packages": ".bower-cache",
"registry": ".bower-registry"
},
"tmp": ".bower-tmp"
}
25 changes: 12 additions & 13 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
crypto = require('crypto'),
authTypes = ['github', 'twitter', 'facebook', 'google'];

crypto = require('crypto');

/**
* User Schema
Expand All @@ -25,7 +23,8 @@ var UserSchema = new Schema({
facebook: {},
twitter: {},
github: {},
google: {}
google: {},
linkedin: {}
});

/**
Expand All @@ -49,26 +48,26 @@ var validatePresenceOf = function(value) {
// the below 4 validations only apply if you are signing up traditionally
UserSchema.path('name').validate(function(name) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
return name.length;
if (!this.provider) return true;
return (typeof name === 'string' && name.length > 0);
}, 'Name cannot be blank');

UserSchema.path('email').validate(function(email) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
return email.length;
if (!this.provider) return true;
return (typeof email === 'string' && email.length > 0);
}, 'Email cannot be blank');

UserSchema.path('username').validate(function(username) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
return username.length;
if (!this.provider) return true;
return (typeof username === 'string' && username.length > 0);
}, 'Username cannot be blank');

UserSchema.path('hashed_password').validate(function(hashed_password) {
// if you are authenticating by any of the oauth strategies, don't validate
if (authTypes.indexOf(this.provider) !== -1) return true;
return hashed_password.length;
if (!this.provider) return true;
return (typeof hashed_password === 'string' && hashed_password.length > 0);
}, 'Password cannot be blank');


Expand All @@ -78,7 +77,7 @@ UserSchema.path('hashed_password').validate(function(hashed_password) {
UserSchema.pre('save', function(next) {
if (!this.isNew) return next();

if (!validatePresenceOf(this.password) && authTypes.indexOf(this.provider) === -1)
if (!validatePresenceOf(this.password) && !this.provider)
next(new Error('Invalid password'));
else
next();
Expand Down
26 changes: 26 additions & 0 deletions app/routes/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

// Articles routes use articles controller
var articles = require('../controllers/articles');
var authorization = require('./middlewares/authorization');

// Article authorization helpers
var hasAuthorization = function(req, res, next) {
if (req.article.user.id !== req.user.id) {
return res.send(401, 'User is not authorized');
}
next();
};

module.exports = function(app) {

app.get('/articles', articles.all);
app.post('/articles', authorization.requiresLogin, articles.create);
app.get('/articles/:articleId', articles.show);
app.put('/articles/:articleId', authorization.requiresLogin, hasAuthorization, articles.update);
app.del('/articles/:articleId', authorization.requiresLogin, hasAuthorization, articles.destroy);

// Finish with setting up the articleId param
app.param('articleId', articles.article);

};
9 changes: 9 additions & 0 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(app) {

// Home route
var index = require('../controllers/index');
app.get('/', index.render);

};
11 changes: 11 additions & 0 deletions app/routes/middlewares/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

/**
* Generic require login routing middleware
*/
exports.requiresLogin = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.send(401, 'User is not authorized');
}
next();
};
31 changes: 13 additions & 18 deletions config/routes.js → app/routes/users.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

module.exports = function(app, passport, auth) {

// User Routes
var users = require('../app/controllers/users');
// User routes use users controller
var users = require('../controllers/users');

module.exports = function(app, passport) {

app.get('/signin', users.signin);
app.get('/signup', users.signup);
app.get('/signout', users.signout);
Expand Down Expand Up @@ -62,20 +63,14 @@ module.exports = function(app, passport, auth) {
failureRedirect: '/signin'
}), users.authCallback);

// Setting the linkedin oauth routes
app.get('/auth/linkedin', passport.authenticate('linkedin', {
failureRedirect: '/signin',
scope: [ 'r_emailaddress' ]
}), users.signin);

// Article Routes
var articles = require('../app/controllers/articles');
app.get('/articles', articles.all);
app.post('/articles', auth.requiresLogin, articles.create);
app.get('/articles/:articleId', articles.show);
app.put('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.update);
app.del('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.destroy);

// Finish with setting up the articleId param
app.param('articleId', articles.article);

// Home route
var index = require('../app/controllers/index');
app.get('/', index.render);
app.get('/auth/linkedin/callback', passport.authenticate('linkedin', {
failureRedirect: '/siginin'
}), users.authCallback);

};
2 changes: 1 addition & 1 deletion app/views/includes/foot.jade
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ script(type='text/javascript', src='/lib/angular-route/angular-route.js')
//Angular UI
script(type='text/javascript', src='/lib/angular-bootstrap/ui-bootstrap.js')
script(type='text/javascript', src='/lib/angular-bootstrap/ui-bootstrap-tpls.js')
script(type='text/javascript', src='/lib/angular-ui-utils/modules/route/route.js')
script(type='text/javascript', src='/lib/angular-ui-utils/ui-utils.min.js')

//Application Init
script(type='text/javascript', src='/js/app.js')
Expand Down
2 changes: 1 addition & 1 deletion app/views/includes/head.jade
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ head
meta(property='og:site_name', content='MEAN - A Modern Stack')
meta(property='fb:admins', content='APP_ADMIN')

link(rel='stylesheet', href='/lib/bootstrap/docs/assets/css/bootstrap.css')
link(rel='stylesheet', href='/lib/bootstrap/dist/css/bootstrap.min.css')
//- link(rel='stylesheet', href='/lib/bootstrap/dist/css/bootstrap-responsive.css')
link(rel='stylesheet', href='/css/common.css')

Expand Down
6 changes: 4 additions & 2 deletions app/views/users/auth.jade
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends ../layouts/default

block content
.row
.offset1.span5
.col-md-offset-1.col-md-5
a(href="/auth/facebook")
img(src="/img/icons/facebook.png")
a(href="/auth/github")
Expand All @@ -11,7 +11,9 @@ block content
img(src="/img/icons/twitter.png")
a(href="/auth/google")
img(src="/img/icons/google.png")
.span6
a(href="/auth/linkedin")
img(src="/img/icons/linkedin.png")
.col-md-6
if message && message.length
.fade.in.alert.alert-error
button.close(type="button", data-dismiss="alert") ×
Expand Down
27 changes: 14 additions & 13 deletions app/views/users/signin.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

block auth
form.signin.form-horizontal(action="/users/session", method="post")
.control-group
label.control-label(for='email') Email
.controls
input#email(type='text', name="email", placeholder='Email')
.form-group
label.col-md-4.control-label(for='email') Email
.col-md-8
input#email.form-control(type='text', name="email", placeholder='Email')

.control-group
label.control-label(for='password') Password
.controls
input#password(type='password', name="password", placeholder='Password')
.form-group
label.col-md-4.control-label(for='password') Password
.col-md-8
input#password.form-control(type='password', name="password", placeholder='Password')

.form-actions
button.btn.btn-primary(type='submit') Sign in
 
| or 
a.show-signup(href="/signup") Sign up
.form-group
.col-md-offset-4.col-md-8
button.btn.btn-primary(type='submit') Sign in
 
| or 
a.show-signup(href="/signup") Sign up
43 changes: 22 additions & 21 deletions app/views/users/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

block auth
form.signup.form-horizontal(action="/users", method="post")
.control-group
label.control-label(for='name') Full name
.controls
input#name(type='text', name="name", placeholder='Full name', value=user.name)
.form-group
label.col-md-4.control-label(for='name') Full Name
.col-md-8
input#name.form-control(type='text', name="name", placeholder='Full name', value=user.name)

.control-group
label.control-label(for='email') Email
.controls
input#email(type='text', name="email", placeholder='Email', value=user.email)
.form-group
label.col-md-4.control-label(for='email') Email
.col-md-8
input#email.form-control(type='text', name="email", placeholder='Email', value=user.email)

.control-group
label.control-label(for='username') Username
.controls
input#username(type='text', name="username", placeholder='Username', value=user.username)
.form-group
label.col-md-4.control-label(for='username') Username
.col-md-8
input#username.form-control(type='text', name="username", placeholder='Username', value=user.username)

.control-group
label.control-label(for='password') Password
.controls
input#password(type='password', name="password", placeholder='Password')
.form-group
label.col-md-4.control-label(for='password') Password
.col-md-8
input#password.form-control(type='password', name="password", placeholder='Password')

.form-actions
button.btn.btn-primary(type='submit') Sign up
 
| or 
a.show-login(href="/signin") login
.form-group
.col-md-offset-4.col-md-8
button.btn.btn-primary(type='submit') Sign up
 
| or 
a.show-login(href="/signin") login
12 changes: 6 additions & 6 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "mean",
"version": "0.1.2",
"version": "0.1.3",
"dependencies": {
"angular": "latest",
"angular": "1.2.10",
"angular-resource": "latest",
"angular-cookies": "latest",
"angular-mocks": "latest",
"angular-route": "latest",
"bootstrap": "2.3.2",
"angular-bootstrap": "0.7.0",
"angular-ui-utils": "0.0.4"
"bootstrap": "3.0.3",
"angular-bootstrap": "0.10.0",
"angular-ui-utils": "0.1.0"
}
}
}
5 changes: 5 additions & 0 deletions config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ module.exports = {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/google/callback"
},
linkedin: {
clientID: "API_KEY",
clientSecret: "SECRET_KEY",
callbackURL: "http://localhost:3000/auth/linkedin/callback"
}
}
5 changes: 5 additions & 0 deletions config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ module.exports = {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/google/callback"
},
linkedin: {
clientID: "API_KEY",
clientSecret: "SECRET_KEY",
callbackURL: "http://localhost:3000/auth/linkedin/callback"
}
}
6 changes: 3 additions & 3 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ module.exports = function(app, passport, db) {
})
}));

// Connect flash for flash messages
app.use(flash());

// Dynamic helpers
app.use(helpers(config.app.name));

// Use passport session
app.use(passport.initialize());
app.use(passport.session());

// Connect flash for flash messages
app.use(flash());

// Routes should be at the last
app.use(app.router);

Expand Down
35 changes: 0 additions & 35 deletions config/middlewares/authorization.js

This file was deleted.

Loading

0 comments on commit c2330e5

Please sign in to comment.