<p>I'm working on project which i need the client to send the data to the 
server
using my server that has some users that only allowed to log in the problem
that I can't send this information to my sever</p>

<pre><code>var mongoose = require('mongoose');
var q = require('q');

//defining schema for users table
var userSchema = new mongoose.Schema({
username: { type: String }, 
password: String,
activeSession: String
});

var User = mongoose.model('Users', userSchema);

//generating random session id
//todo: make sure no 2 users can have single sessionId
function makeSessionId()
{
var text = "";
var possible = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( var i=0; i &lt; 32; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));

return text;
}


//Initlizing interface object of this model.
var userModel = {};

//seeding database with default users
userModel.seed = function(){
var defaultUser = new User({username:'ali', 
password:'5f4dcc3b5aa765d61d8327deb882cf99', activeSession:''});
defaultUser.save(function(err, user) {
if(err) console.dir('error occured in populating database');
});

defaultUser = new User({username:'harry', 
password:'5f4dcc3b5aa765d61d8327deb882cf99', activeSession:''});
defaultUser.save(function(err, user) {
if(err) console.dir('error occured in populating database');
});

defaultUser = new User({username:'tom', 
password:'5f4dcc3b5aa765d61d8327deb882cf99', activeSession:''});
defaultUser.save(function(err, user) {
if(err) console.dir('error occured in populating database');
});

console.log('users table populated.');
}

//Function to auth user baed on username and password.
userModel.authUser = function(username, password){
var results = q.defer();

User.findOne({username: username, password: password},function(err, dbuser) 
{
if (err){
results.reject(err);
} 


if(dbuser){

dbuser.activeSession = makeSessionId();
dbuser.markModified('string');
dbuser.save(function(err, dbuser){
var response = {};

response.status = 'success';
response.sessionId = dbuser.activeSession;
response.username = dbuser.username;
results.resolve(response);
});


} else{
var response = {};
response.status = 'error';
response.error = 'Invalid username or password';
results.resolve(response); 
}
});

return results.promise;
}

//Function to return users by its sessionID.
userModel.getBySessionId = function(sessionId){
var results = q.defer();

User.findOne({activeSession: sessionId},function(err, dbuser) {
if (err){
results.reject(err);
} 

results.resolve(dbuser);
});

return results.promise;
}

//Function to return all users.
userModel.get = function(){
var results = q.defer();

User.find(function(err, users) {
if (err){
results.reject(err);
} 
results.resolve(users);
});

return results.promise;
}


//Function to logout user.
userModel.logout = function(sessionId){
var results = q.defer();

User.findOne({activeSession: sessionId},function(err, dbuser) {
if (err){
results.reject(err);
} 
if(dbuser){
dbuser.activeSession='';
dbuser.markModified('string');
dbuser.save();
results.resolve(dbuser); 
}
results.reject({status:'error', error:'No active session'});

});

return results.promise;
}

module.exports = userModel;
</code></pre>

<p>In Initial setup only three users are created - “ali”, “tom” and “harry” 
with password “password” for all. This API call is used to authenticate the 
user to enter the application. JSON object with with following attribute 
needs to be sent in Request: username. MD5 encrypted password. JSON object 
with Status, Session ID and username is returned in Response. URL: <a href="
http://localhost:3000/user/auth"; rel="nofollow">
http://localhost:3000/user/auth</a> Method: POST Examples: Input:</p>

<pre><code>{"username":"ali", "password":"5f4dcc3b5aa765d61d8327deb882cf99"}
</code></pre>

<p>Response:</p>

<pre><code>
{"status":"success","sessionId":"a8t9Rr9bjWD2InfeFLbNS3FNg5mnFqiV","username":"ali"}
</code></pre>

<p>sessionId is to be sent with each request after login.</p>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to