Skip to content

Commit

Permalink
Login system example implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Apr 27, 2014
1 parent 87f5005 commit 169de87
Show file tree
Hide file tree
Showing 29 changed files with 434 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
node_modules
node_modules
gen
5 changes: 5 additions & 0 deletions login/README.md
@@ -0,0 +1,5 @@
# Login system

That's a basic login system using middlewares and models.

Don't forget to have MongoDB open when running the example, so the users can be stored!
7 changes: 7 additions & 0 deletions login/app.js
@@ -0,0 +1,7 @@
var Rhapsody = require('rhapsody');

var rhapsody = new Rhapsody({
root: __dirname
});

rhapsody.open();
Empty file added login/app/classes/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions login/app/config/bootstrap.js
@@ -0,0 +1,6 @@
module.exports = function(rhapsodyApp, done) {
//Bootstrap everything you want to the app here
//call build tasks and so on, and then call done()

done();
};
3 changes: 3 additions & 0 deletions login/app/config/config.js
@@ -0,0 +1,3 @@
module.exports = {
environment: 'dev'
};
9 changes: 9 additions & 0 deletions login/app/config/cors.js
@@ -0,0 +1,9 @@
module.exports = {
enabled: false,
origin: false,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
exposedHeaders: ['Content-Range', 'X-Content-Range'],
credentials: true,
maxAge: 0
}
75 changes: 75 additions & 0 deletions login/app/config/envs/all.js
@@ -0,0 +1,75 @@
module.exports = {
host: 'localhost',

http: {
port: 4242,
socket: true
},

https: {
enabled: false,
port: 4243,
socket: true
},

methodOverride: {
enabled: false,
attributeName: 'newMethod'
},

database: {
enabled: true,
host: 'localhost',
port: 27017,
name: 'rhapspody-login',
username: undefined,
password: undefined,
mongoOptions: {}
},

log: {
//For debug levels, see WolverineJS documentation: https://github.com/talyssonoc/wolverinejs
level: 'all',

//if undefined, will print to the terminal
output: undefined,

//If true, will print the error stack if an Error object is passed as argument in some log method
printStack: false,

//If true, show the debug level before the message
printLevel: true,

//If true, shows the time the log was logged before the level name
time: true,

//If true, show and file and the line number of where the log was called
printFileInfo: true
},

routes: {
//Controller used when access the app's root
mainController: 'main',

//View used when the user doesn't specify it
mainView: 'index',

//If must be created REST routes for models
allowREST: true
},

//If true, uploaded files via form will be at req.file
upload: {
enabled: false,
},

compression: {
enabled: true
},

generateClientModels: true,

csrf: {
enabled: false
}
};
3 changes: 3 additions & 0 deletions login/app/config/envs/dev.js
@@ -0,0 +1,3 @@
module.exports = {

};
3 changes: 3 additions & 0 deletions login/app/config/envs/prod.js
@@ -0,0 +1,3 @@
module.exports = {

};
1 change: 1 addition & 0 deletions login/app/config/error/404.ejs
@@ -0,0 +1 @@
ERROR 404
1 change: 1 addition & 0 deletions login/app/config/error/500.ejs
@@ -0,0 +1 @@
ERROR 500
28 changes: 28 additions & 0 deletions login/app/config/error/error.js
@@ -0,0 +1,28 @@
var http = require('http'),
path = require('path');

module.exports = {
error404Handler: function(req, res) {
var code = 404;
if(req.xhr) {
res.send(code, http.STATUS_CODES[code]);
}
else {
res.status(code);
res.render(path.join(__dirname, '/' + code));
}
},

error500Handler: function(err, req, res, next) {
Rhapsody.log.error(err);

var code = 500;
if(req.xhr) {
res.send(code, http.STATUS_CODES[code]);
}
else {
res.status(code);
res.render(path.join(__dirname, '/' + code));
}
}
};
8 changes: 8 additions & 0 deletions login/app/config/https.js
@@ -0,0 +1,8 @@
module.exports = {
//Here must configuration files for HTTPS
//For explanation and options, access:
//http://nodejs.org/api/https.html#https_https_createserver_options_requestlistener

key: undefined,
cert: undefined
};
8 changes: 8 additions & 0 deletions login/app/config/session.js
@@ -0,0 +1,8 @@
module.exports = {
enabled: true,
sessionIDKey: 'sessionIdentification',
cookiesSecret: 'rhapsody',
sessionSecret: 'rhapsody',
maxAge: 70000,
sessionStore: undefined
};
7 changes: 7 additions & 0 deletions login/app/config/socket.js
@@ -0,0 +1,7 @@
/**
* Use the settings from: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
* Except that related with logging
*/

module.exports = function socketConfig(io, sessionSockets) {
};
22 changes: 22 additions & 0 deletions login/app/config/template-engines.js
@@ -0,0 +1,22 @@
module.exports = {
defaultEngine: 'ejs',

/**
* The default view engine can be any of:
* https://github.com/visionmedia/consolidate.js/#supported-template-engines
*
* Must follow the pattern:
*
* name: {
* extension: extension
* lib: lib-that-renders-the-extension
* }
*/

engines: {
ejs: {
extension: 'ejs',
lib: require('ejs')
}
}
};
110 changes: 110 additions & 0 deletions login/app/controllers/main/index.js
@@ -0,0 +1,110 @@
var crypto = require('crypto');

console.log();

var MainController = {
mainView: 'index',

views: {

index: {
action: 'index.html',
middlewares: ['not-logged']
},

'post:login': {
action: function(req, res) {
var username = req.body.username;
var password = req.body.password;
var encryptedPassword = crypto.createHash('md5').update(password).digest('hex');

var User = Rhapsody.requireModel('User');

User.findOne({username: username}, function(err, user) {
if(err || !user) {
res.redirect('/');
}
else {

if (encryptedPassword === user.password) {

req.session.user = {
name: user.name,
username: username
};

res.redirect('/info');
}
else {
res.redirect('/');
}
}
});
},

middlewares: ['not-logged']
},

'post:signup': {
action: function(req, res) {
var username = req.body.username;
var name = req.body.name;
var password = req.body.password;
var encryptedPassword = crypto.createHash('md5').update(password).digest('hex');

var User = Rhapsody.requireModel('User');

var newUser = new User({
username: username,
name: name,
password: encryptedPassword
});

newUser.save(function(err) {
if(err) {
Rhapsody.log.error(err);
res.redirect('/');
}
else {
req.session.user = {
name: name,
username: username
};

res.redirect('/info');
}

});

},

middlewares: ['not-logged']
},

exit: {
action: function(req, res) {
req.session.destroy();

res.redirect('/');
},

middlewares: ['logged']
},

info: {
action: function(req, res) {
res.view({
name: 'info',
locals: {
name: req.session.user.name
}
});
},

middlewares: ['logged']
}

}
}

module.exports = MainController;
57 changes: 57 additions & 0 deletions login/app/controllers/main/views/index.html
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<title>Login with RhapsodyJS</title>

<link rel="stylesheet" href="/style.css">

</head>
<body>
<div id="content">
<form action="/signup" method="POST">
<fieldset>
<legend>Sign Up</legend>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Login:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Register"></td>
</tr>
</table>

</fieldset>
</form>

<form action="/login" method="POST">
<fieldset>
<legend>Login</legend>
<table>
<tr>
<td>Login:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>

0 comments on commit 169de87

Please sign in to comment.