Skip to content

Commit

Permalink
New back & front /Profile
Browse files Browse the repository at this point in the history
  • Loading branch information
lecaoquochung committed Jul 26, 2016
1 parent c847d08 commit fe5e213
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ window.onload = function() {

## MONGODB (MANAGING USER)
- Node.js works well with MongoDB (http://www.mongodb.org/).
- init https://github.com/lecaoquochung/nodejs-example/commit/fe60d9366d20633e93ca8cfbd1c0742a5929244a
```
brew install mongodb
npm install mongodb --save
Expand Down Expand Up @@ -352,9 +353,28 @@ npm install sha1 --save
- connect the first time (save to cache as object for the nex call)
- Fetching the POST data - Request handling (common)
- Router api/user/add method
- https://github.com/lecaoquochung/nodejs-example/commit/8f734cd8cb18aaed0c8d60720f93ce52c2367d43


### User authentication with sessions
```
npm install cookie-session --save
```
- Update frontend user login (app.js, template)
- Note
```
delete user._id; // remove when return
delete user.password; // remote when return
req.session.user = user; // store to session
```
- https://github.com/lecaoquochung/nodejs-example/commit/c847d0835b345b49f7b9c61b3ca722fdb91deb94

### User profile
```
```
- Path: /profile
- API & MVC for /profile
- Frontend
- Route app.js
- Controller Profile.js
- Template tpl/profile.html
31 changes: 31 additions & 0 deletions backend/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,37 @@ Router
break;
case 'PUT':
// ...
processPOSTRequest(req, function(data) {
if(!data.firstName || data.firstName === '') {
error('Please fill your first name.', res);
} else if(!data.lastName || data.lastName === '') {
error('Please fill your last name.', res);
} else {
getDatabaseConnection(function(db) {
var collection = db.collection('users');
if(data.password) {
data.password = sha1(data.password);
}
collection.update(
{ email: req.session.user.email },
{ $set: data },
function(err, result) {
if(err) {
err('Error updating the data.');
} else {
if(data.password) delete data.password;
for(var key in data) {
req.session.user[key] = data[key];
}
response({
success: 'OK'
}, res);
}
}
);
});
}
});
break;
case 'POST':
processPOSTRequest(req, function(data) {
Expand Down
10 changes: 10 additions & 0 deletions frontend/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Home = require('./controllers/Home');
var currentPage;
var body;

// common
var showPage = function(newPage) {
if(currentPage) { currentPage.teardown(); }
currentPage = newPage;
Expand Down Expand Up @@ -32,8 +33,17 @@ window.onload = function() {
.check();
}

// roter
Router
.add('login', function() {
var p = new Login();
showPage(p);
})
.add('profile', function() {
if(userModel.isLogged()) {
var p = new Profile();
showPage(p);
} else {
Router.navigate('login');
}
})
26 changes: 26 additions & 0 deletions frontend/js/controllers/Profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = Ractive.extend({
template: require('../../tpl/profile'),
components: {
navigation: require('../views/Navigation'),
appfooter: require('../views/Footer')
},
onrender: function() {
var self = this;
this.set(userModel.get('value'));
this.on('updateProfile', function() {
userModel.set('value.firstName', this.get('firstName'));
userModel.set('value.lastName', this.get('lastName'));
if(this.get('password') != '') {
userModel.set('value.password', this.get('password'));
}
userModel.save(function(error, result) {
if(error) {
self.set('error', error.error);
} else {
self.set('error', false);
self.set('success', 'Profile updated successfully.');
}
});
});
}
});
Empty file added frontend/tpl/profile.html
Empty file.

0 comments on commit fe5e213

Please sign in to comment.