removed middleware and passport
parent
92a7f6159e
commit
bf83154066
|
@ -1,58 +0,0 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<style>
|
||||
[ng-cloak] {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
// include angular loader, which allows the files to load in any order
|
||||
/*
|
||||
AngularJS v1.0.7
|
||||
(c) 2010-2012 Google, Inc. http://angularjs.org
|
||||
License: MIT
|
||||
*/
|
||||
(function(i){'use strict';function d(c,b,e){return c[b]||(c[b]=e())}return d(d(i,"angular",Object),"module",function(){var c={};return function(b,e,f){e&&c.hasOwnProperty(b)&&(c[b]=null);return d(c,b,function(){function a(a,b,d){return function(){c[d||"push"]([a,b,arguments]);return g}}if(!e)throw Error("No module: "+b);var c=[],d=[],h=a("$injector","invoke"),g={_invokeQueue:c,_runBlocks:d,requires:e,name:b,provider:a("$provide","provider"),factory:a("$provide","factory"),service:a("$provide","service"),
|
||||
value:a("$provide","value"),constant:a("$provide","constant","unshift"),filter:a("$filterProvider","register"),controller:a("$controllerProvider","register"),directive:a("$compileProvider","directive"),config:h,run:function(a){d.push(a);return this}};f&&h(f);return g})}})})(window);
|
||||
|
||||
|
||||
// include a third-party async loader library
|
||||
/*!
|
||||
* $script.js v1.3
|
||||
* https://github.com/ded/script.js
|
||||
* Copyright: @ded & @fat - Dustin Diaz, Jacob Thornton 2011
|
||||
* Follow our software http://twitter.com/dedfat
|
||||
* License: MIT
|
||||
*/
|
||||
!function(a,b,c){function t(a,c){var e=b.createElement("script"),f=j;e.onload=e.onerror=e[o]=function(){e[m]&&!/^c|loade/.test(e[m])||f||(e.onload=e[o]=null,f=1,c())},e.async=1,e.src=a,d.insertBefore(e,d.firstChild)}function q(a,b){p(a,function(a){return!b(a)})}var d=b.getElementsByTagName("head")[0],e={},f={},g={},h={},i="string",j=!1,k="push",l="DOMContentLoaded",m="readyState",n="addEventListener",o="onreadystatechange",p=function(a,b){for(var c=0,d=a.length;c<d;++c)if(!b(a[c]))return j;return 1};!b[m]&&b[n]&&(b[n](l,function r(){b.removeEventListener(l,r,j),b[m]="complete"},j),b[m]="loading");var s=function(a,b,d){function o(){if(!--m){e[l]=1,j&&j();for(var a in g)p(a.split("|"),n)&&!q(g[a],n)&&(g[a]=[])}}function n(a){return a.call?a():e[a]}a=a[k]?a:[a];var i=b&&b.call,j=i?b:d,l=i?a.join(""):b,m=a.length;c(function(){q(a,function(a){h[a]?(l&&(f[l]=1),o()):(h[a]=1,l&&(f[l]=1),t(s.path?s.path+a+".js":a,o))})},0);return s};s.get=t,s.ready=function(a,b,c){a=a[k]?a:[a];var d=[];!q(a,function(a){e[a]||d[k](a)})&&p(a,function(a){return e[a]})?b():!function(a){g[a]=g[a]||[],g[a][k](b),c&&c(d)}(a.join("|"));return s};var u=a.$script;s.noConflict=function(){a.$script=u;return this},typeof module!="undefined"&&module.exports?module.exports=s:a.$script=s}(this,document,setTimeout)
|
||||
|
||||
// load all of the dependencies asynchronously.
|
||||
$script([
|
||||
'lib/angular/angular.js',
|
||||
'js/app.js',
|
||||
'js/services.js',
|
||||
'js/controllers.js',
|
||||
'js/filters.js',
|
||||
'js/directives.js'
|
||||
], function() {
|
||||
// when all is done, execute bootstrap angular application
|
||||
angular.bootstrap(document, ['myApp']);
|
||||
});
|
||||
</script>
|
||||
<title>My AngularJS App</title>
|
||||
<link rel="stylesheet" href="css/app.css">
|
||||
</head>
|
||||
<body ng-cloak>
|
||||
<ul class="menu">
|
||||
<li><a href="#/view1">view1</a></li>
|
||||
<li><a href="#/view2">view2</a></li>
|
||||
</ul>
|
||||
|
||||
<div ng-view></div>
|
||||
|
||||
<div>Angular seed app: v<span app-version></span></div>
|
||||
|
||||
</body>
|
||||
</html>
|
203
server.js
203
server.js
|
@ -1,211 +1,10 @@
|
|||
var express = require('express'),
|
||||
mongoose = require('mongoose'),
|
||||
fs = require('fs'),
|
||||
config = require('./conf'),
|
||||
passport = require('passport');
|
||||
config = require('./conf');
|
||||
|
||||
/**
|
||||
* Generic require login routing middleware
|
||||
*/
|
||||
var requiresLogin = function(req, res, next) {
|
||||
if (!req.isAuthenticated()) {
|
||||
return res.send(401, 'User is not authorized');
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
/**
|
||||
* User authorizations routing middleware
|
||||
*/
|
||||
var hasAuthorization = function(req, res, next) {
|
||||
if (req.profile.id != req.user.id) {
|
||||
return res.send(401, 'User is not authorized');
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
/**
|
||||
* Article authorizations routing middleware
|
||||
*/
|
||||
var hasAuthorization = function(req, res, next) {
|
||||
if (req.article.user.id != req.user.id) {
|
||||
return res.send(401, 'User is not authorized');
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
//Bootstrap db connection
|
||||
var db = mongoose.connect(config.db);
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
var mongoose = require('mongoose'),
|
||||
Schema = mongoose.Schema,
|
||||
crypto = require('crypto'),
|
||||
_ = require('underscore'),
|
||||
authTypes = ['github', 'twitter', 'facebook', 'google'];
|
||||
|
||||
|
||||
/**
|
||||
* User Schema
|
||||
*/
|
||||
var UserSchema = new Schema({
|
||||
name: String,
|
||||
email: String,
|
||||
username: {
|
||||
type: String,
|
||||
unique: true
|
||||
},
|
||||
provider: String,
|
||||
hashed_password: String,
|
||||
salt: String,
|
||||
facebook: {},
|
||||
twitter: {},
|
||||
github: {},
|
||||
google: {}
|
||||
});
|
||||
|
||||
/**
|
||||
* Virtuals
|
||||
*/
|
||||
UserSchema.virtual('password').set(function(password) {
|
||||
this._password = password;
|
||||
this.salt = this.makeSalt();
|
||||
this.hashed_password = this.encryptPassword(password);
|
||||
}).get(function() {
|
||||
return this._password;
|
||||
});
|
||||
|
||||
/**
|
||||
* Validations
|
||||
*/
|
||||
var validatePresenceOf = function(value) {
|
||||
return value && value.length;
|
||||
};
|
||||
|
||||
// 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;
|
||||
}, '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;
|
||||
}, '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;
|
||||
}, '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;
|
||||
}, 'Password cannot be blank');
|
||||
|
||||
|
||||
/**
|
||||
* Pre-save hook
|
||||
*/
|
||||
UserSchema.pre('save', function(next) {
|
||||
if (!this.isNew) return next();
|
||||
|
||||
if (!validatePresenceOf(this.password) && authTypes.indexOf(this.provider) === -1)
|
||||
next(new Error('Invalid password'));
|
||||
else
|
||||
next();
|
||||
});
|
||||
|
||||
/**
|
||||
* Methods
|
||||
*/
|
||||
UserSchema.methods = {
|
||||
/**
|
||||
* Authenticate - check if the passwords are the same
|
||||
*
|
||||
* @param {String} plainText
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
authenticate: function(plainText) {
|
||||
return this.encryptPassword(plainText) === this.hashed_password;
|
||||
},
|
||||
|
||||
/**
|
||||
* Make salt
|
||||
*
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
makeSalt: function() {
|
||||
return Math.round((new Date().valueOf() * Math.random())) + '';
|
||||
},
|
||||
|
||||
/**
|
||||
* Encrypt password
|
||||
*
|
||||
* @param {String} password
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
encryptPassword: function(password) {
|
||||
if (!password) return '';
|
||||
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
|
||||
}
|
||||
};
|
||||
|
||||
mongoose.model('User', UserSchema);
|
||||
|
||||
/**
|
||||
* Article Schema
|
||||
*/
|
||||
var ArticleSchema = new Schema({
|
||||
created: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
},
|
||||
title: {
|
||||
type: String,
|
||||
default: '',
|
||||
trim: true
|
||||
},
|
||||
content: {
|
||||
type: String,
|
||||
default: '',
|
||||
trim: true
|
||||
},
|
||||
user: {
|
||||
type: Schema.ObjectId,
|
||||
ref: 'User'
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Validations
|
||||
*/
|
||||
ArticleSchema.path('title').validate(function(title) {
|
||||
return title.length;
|
||||
}, 'Title cannot be blank');
|
||||
|
||||
/**
|
||||
* Statics
|
||||
*/
|
||||
ArticleSchema.statics = {
|
||||
load: function(id, cb) {
|
||||
this.findOne({
|
||||
_id: id
|
||||
}).populate('user', 'name username').exec(cb);
|
||||
}
|
||||
};
|
||||
|
||||
mongoose.model('Article', ArticleSchema);
|
||||
|
||||
var app = express();
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue