Skip to content

Commit

Permalink
adding session support and a million other things
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Braitsch committed May 30, 2012
1 parent 5f6404e commit 2de000e
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/core/config.js
Expand Up @@ -7,7 +7,7 @@ module.exports = function(app, exp) {
app.set('view options', { pretty: true });
app.use(exp.bodyParser());
app.use(exp.cookieParser());
app.use(exp.session({ secret: 'secret-constant' }));
app.use(exp.session({ secret: 'super-duper-secret-secret' }));
app.use(exp.methodOverride());
app.use(require('stylus').middleware({ src: app.root + '/app' }));
app.use(exp.static(app.root + '/app'));
Expand Down
13 changes: 13 additions & 0 deletions app/css/style.css
Expand Up @@ -82,12 +82,25 @@ body {
#sendCredentials .modal-footer {
height: 20px;
}
#loginFailure {
width: 310px;
margin: -200px 0 0 -150px;
}
#loginFailure .cancel {
float: right;
}
#btn-logout {
width: 100px;
top: 1px;
right: 8px;
position: absolute;
}
#userData {
margin: 50px 0 0 22px;
}
#userData p {
color: #808080;
}
#print {
padding: 20px;
}
Expand Down
15 changes: 15 additions & 0 deletions app/css/style.styl
Expand Up @@ -89,6 +89,14 @@ html, body
}
}

#loginFailure{
width 310px
margin -200px 0 0 -150px
.cancel{
float right
}
}


// logged-in page //

Expand All @@ -97,6 +105,13 @@ html, body
top 1px
right 8px
position absolute

#userData{
margin 50px 0 0 22px
p {
color grey
}
}

// support pages //

Expand Down
14 changes: 13 additions & 1 deletion app/js/home.js
@@ -1,7 +1,19 @@
$(document).ready(function(){

$('#btn-logout').click(function(){
console.log('logout clicked')
var req = $.ajax({
url: "/home",
type: "POST",
data: {logout : true}
});

req.done(function(msg) {
window.location.href = '/';
});

req.fail(function(jqXHR, textStatus) {
console.log( "request failed: " + textStatus );
});
})

})
42 changes: 42 additions & 0 deletions app/js/login-form.js
@@ -0,0 +1,42 @@
$(document).ready(function(){

// the login form that shows when the page loads //

$('#loginForm').ajaxForm({
beforeSubmit : function(formData, jqForm, options){
if ($('#user').val() == ''){
showLoginError('Whoops!', 'Please enter a valid username');
return false;
} else if ($('#pass').val() == ''){
showLoginError('Whoops!', 'Please enter a valid password');
return false;
} else{
console.log('about to submit :: '+$.param(formData));
return pass;
}
},
success : function(responseText, status, xhr, $form){
if (status == 'success') window.location.href = '/home';
},
error : function(e){
showLoginError('Login Failure', 'Please check your username and/or password');
}
});
$('#loginForm #forgot-pass').click(function(){ $('#sendCredentials').modal('show');});

// modal window to display any login form errors //

$('#loginFailure').modal({
show : false,
keyboard : true,
backdrop : true
});

function showLoginError(t, m)
{
$('#loginFailure .modal-header h3').text(t);
$('#loginFailure .modal-body p').text(m);
$('#loginFailure').modal('show');
}

})
41 changes: 8 additions & 33 deletions app/js/login.js → app/js/login-retrieval.js
@@ -1,40 +1,15 @@
$(document).ready(function(){

// login form shown when page loads //
// modal window to allow users to request credentials by email //

$('#loginForm').ajaxForm({
beforeSubmit : function(formData, jqForm, options){
if ($('#user').val() == ''){
alert('please enter your username');
return false;
} else if ($('#pass').val() == ''){
alert('please enter your password');
return false;
} else{
console.log('about to submit :: '+$.param(formData));
return pass;
}
},
success : function(responseText, status, xhr, $form){
if (status == 'success') window.location.href = '/print';
}
});

$('#forgot-pass').click(function(){
$('#sendCredentials').modal('show');
});

// retrieve password modal window & form //

var a = $('.alert');
$('#sendCredentials').modal({
show : false,
keyboard : true,
backdrop : true
});
$('#sendCredentials').on('show', function(){
$('#emailForm').resetForm(); a.hide();
});
var a = $('#sendCredentials .alert');
$('#sendCredentials').on('show', function(){ $('#emailForm').resetForm(); a.hide();});

$('#emailForm').ajaxForm({
beforeSubmit : function(formData, jqForm, options){
if (validate($('#email').val())){
Expand All @@ -49,7 +24,7 @@ $(document).ready(function(){
showSuccess("You've been sent an email with your login credentials.");
},
error : function(){
showError("<b> I'm Sorry.</b> I could not find that email address");
showError("I'm Sorry. I could not find that email address");
}
});

Expand All @@ -65,11 +40,11 @@ $(document).ready(function(){
a.html(m); a.show();
}

function showSucess(m)
function showSuccess(m)
{
a.attr('class', 'alert alert-success');
a.html(m);
a.fadeIn(500);
}

})
});
21 changes: 11 additions & 10 deletions app/js/signup.js
Expand Up @@ -7,7 +7,7 @@ $(document).ready(function(){

$('#signupForm').ajaxForm({
beforeSubmit : function(formData, jqForm, options){
return true; //validateForm();
return validateForm();
},
success : function(responseText, status, xhr, $form){
if (status == 'success') window.location.href = '/print';
Expand All @@ -22,6 +22,15 @@ $(document).ready(function(){
}
}
});
$('#signup-no').click(function(){ window.location.href = '/';})

// display errors in a modal window //

$('#signupErrors').modal({
show : false,
keyboard : true,
backdrop : true
});

function validateForm()
{
Expand Down Expand Up @@ -68,14 +77,6 @@ $(document).ready(function(){
ul.empty();
for (var i=0; i < a.length; i++) ul.append('<li>'+a[i]+'</li>');
$('#signupErrors').modal('show');
}

// display errors in a modal window //

$('#signupErrors').modal({
show : false,
keyboard : true,
backdrop : true
});
}

})
9 changes: 8 additions & 1 deletion app/modules/account-manager.js
Expand Up @@ -11,6 +11,13 @@ AccountManager = function() {
this.collection = this.db.collection('accounts');
};

AccountManager.prototype.login = function(credentials, callback) {
this.collection.find( { $and : credentials } ).toArray(function(e, results) {
if (e) callback(e)
else callback(null, results)
});
}

AccountManager.prototype.create = function(credentials, callback) {
// append date stamp when record was created //
credentials.date = new Date();
Expand All @@ -23,7 +30,7 @@ AccountManager.prototype.delete = function(id, callback) {

AccountManager.prototype.lookup = function(o, callback){
// function to search across multiple fields //
console.log(o);
console.log('AccountManager.prototype.lookup : '+o);
this.collection.find( { $or : o } ).toArray(function(e, results) {
if (e) callback(e)
else callback(null, results)
Expand Down
26 changes: 20 additions & 6 deletions app/modules/email-dispatcher.js
Expand Up @@ -11,18 +11,32 @@ var server = email.server.connect({
ssl : true
});

module.exports.send = function(recipientAddress, callback)
module.exports.send = function(credentials, callback)
{
server.send({
text: "i hope this works",
from: sendersAddress,
to: recipientAddress,
subject: "hello!"
from : sendersAddress,
to : credentials.email,
subject : 'login credentials',
text : 'something went wrong... :(',
attachment : drawEmail(credentials)
}, callback );
}


module.exports.test = function()
{
console.log(sendersAddress);
}

function drawEmail(o)
{
var html = "<html><body>"
html += "Hello "+o.name+",<br><br>"
html += "Here are your login credentials as you requested :<br><br>"
html += "<b>username : "+o.user+"</b><br>"
html += "<b>password : "+o.pass+"</b><br><br>"
html += "<a href='http://node-login.braitsch.io'>Click here to login</a><br><br>"
html += "Cheers,<br>"
html += "<a href='http://twitter.com/braitsch'>@braitsch</a><br><br>"
html += "</body></html>";
return [{data:html, alternative:true}];
}
50 changes: 39 additions & 11 deletions app/router.js
Expand Up @@ -28,16 +28,45 @@ module.exports = function(app) {
// logged in user page //

app.get('/home', function(req, res) {
res.render('home', {
locals: {
title: 'Welcome',
}
});
if (req.session.user == null){
res.redirect('/');
} else{
res.render('home', {
locals: {
title : 'Control Panel',
udata : req.session.user
}
});
}
});

app.post('/home', function(req, res){
if (req.param('logout') == 'true') {
req.session.destroy(function(e){
if (e == null){
res.send('ok', 200);
} else{
res.send('unable to destory user session', 400);
}
});
} else{
res.send('invalid request from client', 400);
}
});

function attemptLogin(req, res)
{
res.send('ok', 200);
AM.login([
{user : req.param('user')},
{pass : req.param('pass')}
], function(e, o){
if (o.length == 0){
res.send('invalid-credentials', 400);
} else{
req.session.user = o;
res.send(o, 200);
}
});
}

function getCredentials(req, res)
Expand All @@ -51,13 +80,12 @@ module.exports = function(app) {
});
}

function sendCredentials()
function sendCredentials(o, res)
{
EM.send(req.param('email'), function(err, msg){
console.log('err = '+err);
console.log(err || msg);
EM.send(o, function(err, msg){
// console.log('error = '+err, 'message = '+msg);
})
res.send('ok', 200);
res.send('ok', 200);
}

// creating new accounts //
Expand Down
11 changes: 7 additions & 4 deletions app/views/home.jade
@@ -1,13 +1,16 @@
- user= udata[0]

div.navbar.navbar-fixed-top
div.navbar-inner
div.container-fluid
ul#global-nav.nav.pull-left
li.divider-vertical
a(href='#').brand Hello !
li.divider-vertical
a(href='#').brand Control Panel
button#btn-logout.btn.btn-primary
i.icon-lock.icon-white
| Sign Out


#userData
p= 'Welcome :: '+ user.name +' , '+user.email+' , '+ user.country +' , '+ user.user +' , '+user.pass

script(src='/vendor/jquery.min.js')
script(src='/js/home.js')

0 comments on commit 2de000e

Please sign in to comment.