Skip to content

Instantly share code, notes, and snippets.

@slickplaid
Created February 16, 2012 19:25
Show Gist options
  • Save slickplaid/1847141 to your computer and use it in GitHub Desktop.
Save slickplaid/1847141 to your computer and use it in GitHub Desktop.
// User Authentication
// dumbed down a bit for this gist
function authenticateUser(req, res, next) {
//req.session.user = { firstName: 'Brooke', lastName: 'Thomas' };
console.log(req);
// If there is a session user
if (req.session.user) {
database.users.get(req.session.user, function(err, user) {
req.user = user;
res.local('user', user);
next();
});
} else {
res.render('sessions/login', { user: req.user });
}
};
app.use(express.logger('\x1b[33m:method\x1b[0m \x1b[32m:url\x1b[0m :status :response-time ms'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(app.router);
// app.use(chromeframe());
app.use(express.static(__dirname + '/public'));
app.helpers(helpers);
app.helpers(_);
app.configure('development', function() {
// ... we are running NODE_ENV="production"
});
app.configure('production', function() {
app.use(express.session({
secret: '<removed>',
store: new RedisStore({ port: redisPort, host: redisIP, pass: redisPwd })
}));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); // for testing
app.use(stylus.middleware({
src: __dirname + "/views"
,dest: __dirname + '/public'
,compress: true
}));
database.configure('<removed>', true, couch.production);
database.configureCache(redisPort, redisIP, redisPwd);
});
{ socket:
{ pair:
{ _secureEstablished: true,
_isServer: true,
_encWriteState: true,
_clearWriteState: true,
_doneFlag: false,
credentials: [Object],
_rejectUnauthorized: false,
_requestCert: false,
ssl: {},
servername: false,
cleartext: [Circular],
encrypted: [Object],
fd: undefined,
_events: [Object],
_writeCalled: false,
cycleEncryptedPullLock: false,
cycleCleartextPullLock: false,
cycleCleartextPushLock: true,
cycleEncryptedPushLock: false },
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
socket:
{ _handle: [Object],
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
bytesRead: 1778,
bytesWritten: 6247,
allowHalfOpen: false,
writable: true,
readable: true,
server: [Object],
_events: [Object],
_pipeCount: 1,
_idleTimeout: 120000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: Thu, 16 Feb 2012 19:13:50 GMT },
encrypted:
{ pair: [Object],
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
_events: [Object],
_pipeCount: 1 },
authorized: false,
_controlReleased: true,
npnProtocol: undefined,
servername: false,
ondrain: [Function],
_events: { timeout: [Function], error: [Function], close: [Object] },
ondata: [Function],
onend: [Function],
_httpMessage:
{ output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
_hasBody: true,
_trailer: '',
finished: false,
socket: [Circular],
connection: [Circular],
_events: [Object],
_headers: [Object],
_headerNames: [Object],
app: [Object],
req: [Circular],
end: [Function] } },
connection:
{ pair:
{ _secureEstablished: true,
_isServer: true,
_encWriteState: true,
_clearWriteState: true,
_doneFlag: false,
credentials: [Object],
_rejectUnauthorized: false,
_requestCert: false,
ssl: {},
servername: false,
cleartext: [Circular],
encrypted: [Object],
fd: undefined,
_events: [Object],
_writeCalled: false,
cycleEncryptedPullLock: false,
cycleCleartextPullLock: false,
cycleCleartextPushLock: true,
cycleEncryptedPushLock: false },
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
socket:
{ _handle: [Object],
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
bytesRead: 1778,
bytesWritten: 6247,
allowHalfOpen: false,
writable: true,
readable: true,
server: [Object],
_events: [Object],
_pipeCount: 1,
_idleTimeout: 120000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: Thu, 16 Feb 2012 19:13:50 GMT },
encrypted:
{ pair: [Object],
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
_events: [Object],
_pipeCount: 1 },
authorized: false,
_controlReleased: true,
npnProtocol: undefined,
servername: false,
ondrain: [Function],
_events: { timeout: [Function], error: [Function], close: [Object] },
ondata: [Function],
onend: [Function],
_httpMessage:
{ output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
_hasBody: true,
_trailer: '',
finished: false,
socket: [Circular],
connection: [Circular],
_events: [Object],
_headers: [Object],
_headerNames: [Object],
app: [Object],
req: [Circular],
end: [Function] } },
httpVersion: '1.1',
complete: false,
headers:
{ host: '<removed>',
connection: 'keep-alive',
'cache-control': 'max-age=0',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1041.0 Safari/535.21',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-encoding': 'gzip,deflate,sdch',
'accept-language': 'en-US,en;q=0.8',
'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
cookie: 'connect.sid=<removed>' },
trailers: {},
readable: true,
url: '/clients',
method: 'GET',
statusCode: null,
client:
{ pair:
{ _secureEstablished: true,
_isServer: true,
_encWriteState: true,
_clearWriteState: true,
_doneFlag: false,
credentials: [Object],
_rejectUnauthorized: false,
_requestCert: false,
ssl: {},
servername: false,
cleartext: [Circular],
encrypted: [Object],
fd: undefined,
_events: [Object],
_writeCalled: false,
cycleEncryptedPullLock: false,
cycleCleartextPullLock: false,
cycleCleartextPushLock: true,
cycleEncryptedPushLock: false },
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
socket:
{ _handle: [Object],
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
bytesRead: 1778,
bytesWritten: 6247,
allowHalfOpen: false,
writable: true,
readable: true,
server: [Object],
_events: [Object],
_pipeCount: 1,
_idleTimeout: 120000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: Thu, 16 Feb 2012 19:13:50 GMT },
encrypted:
{ pair: [Object],
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
_events: [Object],
_pipeCount: 1 },
authorized: false,
_controlReleased: true,
npnProtocol: undefined,
servername: false,
ondrain: [Function],
_events: { timeout: [Function], error: [Function], close: [Object] },
ondata: [Function],
onend: [Function],
_httpMessage:
{ output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
_hasBody: true,
_trailer: '',
finished: false,
socket: [Circular],
connection: [Circular],
_events: [Object],
_headers: [Object],
_headerNames: [Object],
app: [Object],
req: [Circular],
end: [Function] } },
httpVersionMajor: 1,
httpVersionMinor: 1,
upgrade: false,
originalUrl: '/clients',
query: {},
app:
{ stack:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
requestCert: false,
rejectUnauthorized: false,
key: '-----BEGIN RSA PRIVATE KEY-----\n<removed>\n-----END RSA PRIVATE KEY-----\n',
cert: '-----BEGIN CERTIFICATE-----\n<removed>\n-----END CERTIFICATE-----\n',
SNICallback: [Function],
_events:
{ connection: [Function],
secureConnection: [Function: connectionListener],
request: [Function],
listening: [Object] },
connections: 1,
allowHalfOpen: false,
_handle:
{ writeQueueSize: 0,
onconnection: [Function: onconnection],
socket: [Circular] },
httpAllowHalfOpen: false,
cache: {},
settings:
{ env: 'production',
'view cache': true,
views: '/home/<removed>/views',
'view engine': 'html',
'view options': [Object] },
redirects: {},
isCallbacks: {},
_locals:
{ settings: [Object],
app: [Circular],
formatString: [Function],
formatBoolean: [Function],
formatStrictBoolean: [Function],
formatShortDate: [Function],
formatDate: [Function],
formatPhone: [Function],
formatPercent: [Function],
formatCurrency: [Function],
optionsForSelect: [Function],
ageAt: [Function],
dateAtAge: [Function],
parseCurrency: [Function],
dateFieldToDate: [Function],
shortDateFieldToDate: [Function],
states: [Object],
stateAbbr: [Object],
stateTaxRates: [Object],
stateLTCCosts: [Object],
_: [Object],
VERSION: '1.3.1',
forEach: [Function],
each: [Function],
collect: [Function],
map: [Function],
inject: [Function],
foldl: [Function],
reduce: [Function],
foldr: [Function],
reduceRight: [Function],
detect: [Function],
find: [Function],
select: [Function],
filter: [Function],
reject: [Function],
all: [Function],
every: [Function],
any: [Function],
some: [Function],
contains: [Function],
include: [Function],
invoke: [Function],
pluck: [Function],
max: [Function],
min: [Function],
shuffle: [Function],
sortBy: [Function],
groupBy: [Function],
sortedIndex: [Function],
toArray: [Function],
size: [Function],
head: [Function],
first: [Function],
initial: [Function],
last: [Function],
tail: [Function],
rest: [Function],
compact: [Function],
flatten: [Function],
without: [Function],
unique: [Function],
uniq: [Function],
union: [Function],
intersect: [Function],
intersection: [Function],
difference: [Function],
zip: [Function],
indexOf: [Function],
lastIndexOf: [Function],
range: [Function],
bind: [Function: bind],
bindAll: [Function],
memoize: [Function],
delay: [Function],
defer: [Function],
throttle: [Function],
debounce: [Function],
once: [Function],
wrap: [Function],
compose: [Function],
after: [Function],
keys: [Function: keys],
values: [Function],
methods: [Function],
functions: [Function],
extend: [Function],
defaults: [Function],
clone: [Function],
tap: [Function],
isEqual: [Function],
isEmpty: [Function],
isElement: [Function],
isArray: [Function: isArray],
isObject: [Function],
isArguments: [Function],
isFunction: [Function],
isString: [Function],
isNumber: [Function],
isNaN: [Function],
isBoolean: [Function],
isDate: [Function],
isRegExp: [Function],
isNull: [Function],
isUndefined: [Function],
has: [Function],
noConflict: [Function],
identity: [Function],
times: [Function],
escape: [Function],
mixin: [Function],
uniqueId: [Function],
templateSettings: [Object],
template: [Function],
chain: [Function],
Date: [Object] },
dynamicViewHelpers:
{ request: [Function],
activeSection: [Function],
hasMessages: [Function],
messages: [Function],
user: [Function] },
errorHandlers: [],
route: '/',
routes:
{ app: [Circular],
routes: [Object],
params: {},
_params: [],
middleware: [Function] },
router: [Getter],
__usedRouter: true },
res:
{ output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
_hasBody: true,
_trailer: '',
finished: false,
socket:
{ pair: [Object],
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
socket: [Object],
encrypted: [Object],
authorized: false,
_controlReleased: true,
npnProtocol: undefined,
servername: false,
ondrain: [Function],
_events: [Object],
ondata: [Function],
onend: [Function],
_httpMessage: [Circular] },
connection:
{ pair: [Object],
writable: true,
readable: true,
_paused: false,
_needDrain: false,
_pending: [],
_pendingCallbacks: [],
_pendingBytes: 0,
socket: [Object],
encrypted: [Object],
authorized: false,
_controlReleased: true,
npnProtocol: undefined,
servername: false,
ondrain: [Function],
_events: [Object],
ondata: [Function],
onend: [Function],
_httpMessage: [Circular] },
_events: { finish: [Function] },
_headers: { 'x-powered-by': 'Express' },
_headerNames: { 'x-powered-by': 'X-Powered-By' },
app:
{ stack: [Object],
requestCert: false,
rejectUnauthorized: false,
key: '-----BEGIN RSA PRIVATE KEY-----\n<removed>\n-----END RSA PRIVATE KEY-----\n',
cert: '-----BEGIN CERTIFICATE-----\n<removed>\n-----END CERTIFICATE-----\n',
SNICallback: [Function],
_events: [Object],
connections: 1,
allowHalfOpen: false,
_handle: [Object],
httpAllowHalfOpen: false,
cache: {},
settings: [Object],
redirects: {},
isCallbacks: {},
_locals: [Object],
dynamicViewHelpers: [Object],
errorHandlers: [],
route: '/',
routes: [Object],
router: [Getter],
__usedRouter: true },
req: [Circular],
end: [Function] },
next: [Function: next],
_startTime: Thu, 16 Feb 2012 19:13:50 GMT,
_logging: true,
body: {},
originalMethod: 'GET',
cookies: { 'connect.sid': '<removed>' },
_route_index: 5,
route:
{ path: '/clients',
method: 'get',
callbacks: [ [Function: authenticateUser], [Function] ],
keys: [],
regexp: /^\/clients\/?$/i,
params: [] },
params: [] }
TypeError: Cannot read property 'user' of undefined
at authenticateUser (/home/<removed>/mvc.js:156:18)
at callbacks (/home/<removed>/node_modules/express/lib/router/index.js:272:11)
at param (/home/<removed>/node_modules/express/lib/router/index.js:246:11)
at pass (/home/<removed>/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/home/<removed>/node_modules/express/lib/router/index.js:280:4)
at Object.handle (/home/<removed>/node_modules/express/lib/router/index.js:45:10)
at next (/home/<removed>/node_modules/connect/lib/http.js:203:15)
at Object.cookieParser [as handle] (/home/<removed>/node_modules/connect/lib/middleware/cookieParser.js:44:5)
at next (/home/<removed>/node_modules/connect/lib/http.js:203:15)
at Object.methodOverride [as handle] (/home/<removed>/node_modules/connect/lib/middleware/methodOverride.js:35:5)
req.session === undefined in authenticateUser(req, res, next) function.
Server is running HTTPS on port 3000.
@slickplaid
Copy link
Author

Solution was to place the app.use(app.router); below everything else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment