Created
June 24, 2014 22:51
-
-
Save clouddueling/b3cc437ff6a0bc463d95 to your computer and use it in GitHub Desktop.
flash module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Module Dependencies | |
// ================================================================== | |
var auth = require('../lib/auth'), | |
db = require('../models'), | |
hash = require('../lib/hash'), | |
flash = require('../lib/flash'); | |
// | |
// Controller | |
// ================================================================== | |
module.exports = { | |
getLogin: { | |
auth: { mode: 'try' }, | |
plugins: { 'hapi-auth-cookie': { redirectTo: false } }, | |
handler: function(request, reply) { | |
reply.view('auth/login', { | |
// | |
// FLASH | |
// | |
flashMessage: flash.get('flashMessage', request, reply), | |
flashType: flash.get('flashType', request, reply), | |
}); | |
} | |
}, | |
postLogin: { | |
auth: { mode: 'try' }, | |
plugins: { 'hapi-auth-cookie': { redirectTo: false } }, | |
handler: function(request, reply) { | |
if (request.auth.isAuthenticated) { | |
return reply.redirect('/'); | |
} | |
var input = request.payload; | |
auth.attempt({ | |
email: input.email, | |
password: input.password, | |
request: request, | |
success: function() { | |
reply.redirect('/app'); | |
}, | |
failure: function(err) { | |
// | |
// FLASH | |
// | |
flash.set('Oops! Email or password was incorrect', 'danger', reply); | |
reply.redirect('/login'); | |
} | |
}); | |
} | |
} | |
// etc etc... | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Flash = function() { | |
var self = {}; | |
self.get = function(name, request, reply) { | |
if (request.state[name]) { | |
// retrieve a base64 value | |
var value = new Buffer(request.state[name], 'base64').toString('ascii'); | |
// remove the old value | |
reply.state(name, '', { ttl: 0 }); | |
return value; | |
} | |
// if it is not set return false | |
return false; | |
}; | |
self.set = function(message, type, reply) { | |
// set the necessary values | |
reply.state('flashMessage', message, { | |
encoding: 'base64' | |
}); | |
reply.state('flashType', type, { | |
encoding: 'base64' | |
}); | |
}; | |
return self; | |
} | |
module.exports = new Flash(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "../layouts/master.html" %} | |
{% block content %} | |
etc. | |
{% if flashMessage %} | |
<div class='alert alert-{{ flashType }}'> | |
{{ flashMessage }} | |
</div> | |
{% endif %} | |
etc. | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment