Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmpe273-lab2 #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ lib-cov
*.out
*.pid
*.gz
*.js~

pids
logs
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmpe273-lab2
Node.js Cookie Bases Authentication System
============

Node.JS with [Connect]
Expand Down Expand Up @@ -43,4 +43,4 @@ curl -i http://localhost:8000/ -X PUT --cookie "session_id=xxxxxxxxxx"
curl -i http://localhost:8000/ -X DELETE --cookie "session_id=xxxxxxxxxx"
```

[Connect]:http://www.senchalabs.org/connect/
[Connect]:http://www.senchalabs.org/connect/
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,36 @@ function get(request, response) {
};

function post(request, response) {
// TODO: read 'name and email from the request.body'
// var newSessionId = login.login('xxx', '[email protected]');
// TODO: set new session id to the 'session_id' cookie in the response
// replace "Logged In" response with response.end(login.hello(newSessionId));

response.end("Logged In\n");
var name=request.body.name;
var email=request.body.email;
var newSessionId=login.login(name,email);
response.end("\nLogged In....\n welcome="+name+" with email id ="+email+"\n\n New session_id="+response.end(login.hello(newSessionId)));
};

function del(request, response) {
console.log("DELETE:: Logout from the server");
// TODO: remove session id via login.logout(xxx)
// No need to set session id in the response cookies since you just logged out!

// Taking cookies from response
var cookies=request.cookies;
// Get session id from cookies
var sessionId=cookies.session_id;
// Performed logout process
login.logout(sessionId);
response.end('Logged out from the server\n');
};

function put(request, response) {
console.log("PUT:: Re-generate new seesion_id for the same user");
// TODO: refresh session id; similar to the post() function

var cookies=request.cookies;
var sessionId=cookies.session_id;
// Taking data from sessionId from sessionMap
var OldName=login.getName(sessionId);
var OldEmail=login.getEmail(sessionId);
console.log("PUT:: Re-generate new session_id for the same user");
// Generate New Session ID and store New Session ID
var newSessionId = login.login(OldName, OldEmail);
response.setHeader('Set-Cookie', 'session_id=' + newSessionId);
// Remove Old Session ID from sessionMap
var temp=login.RefreshSession(sessionId);
response.end("Re-freshed session id\n");
};

Expand Down
52 changes: 41 additions & 11 deletions login.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,33 @@ function Login() {
* Say Hello {name} to the user
*/
Login.prototype.hello = function(sessionId) {
return 'Hello ' + this.sessionMap[sessionId].name + '\n';
if(this.sessionMap[sessionId]==null)
{

return 'Please Login.!';
}
else
{
return 'Hello, ' + this.sessionMap[sessionId].name;
}
};
/**
* Get Current Session id user name
*/
Login.prototype.getName = function(sessionId)
{
return this.sessionMap[sessionId].name;
};

/**
* Get Current Session id user email
*/
Login.prototype.getEmail = function(sessionId)
{
return this.sessionMap[sessionId].email;
};


/**
* Check whether the given session id is valid (is in sessionMap) or not.
*/
Expand All @@ -26,25 +50,31 @@ Login.prototype.isLoggedIn = function(sessionId) {
* Create a new session id for the given user.
*/
Login.prototype.login = function(_name, _email) {
/*
* Generate unique session id and set it into sessionMap like [email protected]
*/
var sessionId = new Date().getTime();
var sessionId = new Date().getTime();
this.sessionMap[sessionId] = { name: _name, email: _email }

console.log('new session id ' + sessionId + ' for login::' + _email);

console.log("inside login functionwh ich take email \n")
console.log('\n new session id ' + sessionId + ' for login::' + _email);
return sessionId;
};

/**
* Remove specific refreshed session from SessionMap
**/
Login.prototype.RefreshSession = function(_sessionId)
{
// Delete the session id from sessionMap
delete this.sessionMap[_sessionId];
return "done";
};

/**
* Logout from the server
*/
Login.prototype.logout = function(sessionId) {
console.log('logout::' + sessionId);
/*
* TODO: Remove the given sessionId from the sessionMap
*/
// Delete the session id from sessionMap
delete this.sessionMap[sessionId];

};

// Export the Login class
Expand Down