Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new authentication helper

  • Loading branch information...
commit 299fd9d73f48c0ca4380da7f43d7a8e9a55375ef 1 parent 468ea56
@brianshaler brianshaler authored
View
12 app.js
@@ -4,6 +4,7 @@
var express = require('express'),
fs = require('fs'),
+ querystring = require('querystring'),
mongoose = require('mongoose'),
conf = require('node-config'),
crypto = require('crypto'),
@@ -68,7 +69,14 @@ conf.initConfig(function(err) {
app.use(express.methodOverride());
// auth
app.use(function(req, res, next){
- req.user = {isUser: false};
+ req.is_user = false;
+ req.require_authentication = function () {
+ if (req.is_user == true) {
+ return true;
+ }
+ res.redirect('/admin/login?'+querystring.stringify({redirect_url: req.url}));
+ return false;
+ }
if (req.session.session_key) {
Settings = mongoose.model('Settings');
Settings.findOne({option: "app"}, function (err, s) {
@@ -76,7 +84,7 @@ conf.initConfig(function(err) {
if (s.value.session_key && s.value.session_key == req.session.session_key) {
var correct_token = crypto.createHash('sha1').update(req.session.user_name+"|"+req.session.session_key).digest('hex');
if (req.session.session_token == correct_token) {
- req.user.isUser = true;
+ req.is_user = true;
}
}
}
View
8 controllers/DashboardController.js
@@ -15,7 +15,7 @@ exports.controller = function(req, res, next) {
var self = this;
self.index = function() {
- if (!req.user.isUser) { return res.redirect('/admin/login'); }
+ if (!req.require_authentication("/dashboard")) { return; }
var activity_items = [];
var where = {analyzed_at: {"$gt": new Date(Date.now()-86400*1000)}};
@@ -45,7 +45,11 @@ exports.controller = function(req, res, next) {
res.send(activity_items);
break;
default:
- res.render("objects/stream", {activity_items: activity_items, stream: settings});
+ res.render("objects/stream", {
+ layout: "dashboard/dashboard-layout",
+ activity_items: activity_items,
+ stream: settings
+ });
}
});
}
View
6 controllers/index.js
@@ -14,7 +14,11 @@ Controller = function(req, res, next) {
}
var index = function(req, res, next) {
- res.render('index', { title: 'Saturn' })
+ if (req.is_user) {
+ res.redirect('/dashboard');
+ } else {
+ res.render('index', { title: 'Saturn' });
+ }
}
function addRoutes (app, controller, callback) {
Please sign in to comment.
Something went wrong with that request. Please try again.