user.js |
|
'use strict';
|
|
¶ Telepat User Class |
var Admin = require('./admin');
var Channel = require('./channel');
|
¶ User ConstructorThis is generally invoked by the main Telepat object. Params
api
Object
The API connection object. This is injected by Telepat.
log
Object
The logging-handling object. This is injected by Telepat.
error
Object
The error-handling object. This is injected by Telepat.
monitor
Object
The monitoring object. This is injected by Telepat.
setAdmin
function
Method to allow this class to set administrators. This is injected by Telepat.
|
var User = function (tapi, tlog, terror, tevent, tmonitor, tsetAdmin) {
var api = tapi;
var error = terror;
var log = tlog;
var setAdmin = tsetAdmin;
var event = tevent;
var monitor = tmonitor;
var userChannel = null;
var self = this;
function _login(endpoint, options, isAdmin) {
function success(res) {
for(var k in res.body.content.user) {
self[k] = res.body.content.user[k];
}
if (isAdmin) {
self.isAdmin = true;
setAdmin(new Admin(api, log, error));
}
|
userChannel = new Channel(api, log, error, monitor, { channel: { model: 'users', id: self.id } }); userChannel.subscribe(); |
api.authenticationToken = res.body.content.token;
event.emit('login');
}
api.call(endpoint, options, function (err, res) {
if (err) {
if (err.status == 404 && options.hasOwnProperty('access_token')) {
log.info('Got 404 on Facebook login, registering user first');
api.call('user/register_facebook', options, function (err, res) {
if (err) {
log.error('Failed to login with Facebook. Could not register or login user.');
event.emit('login_error', error('Login failed with error: ' + err));
} else {
api.call(endpoint, options, function (err, res) {
if (err) {
log.error('Failed to login with Facebook. User registration was successful, but login failed.');
event.emit('login_error', error('Login failed with error: ' + err));
} else {
success(res);
}
});
}
});
}
else {
event.emit('login_error', error('Login failed with error: ' + err));
}
} else {
success(res);
}
});
}
this.update = function(callback) {
var result = {};
for (var key in self) {
if (self.hasOwnProperty(key)) {
result[key] = self[key];
}
}
api.call('user/update_immediate',
{ user: result },
function (err, res) {
if (err) {
callback(error('Updating user failed with error: ' + err), null);
} else {
callback(null, res.body.content);
}
});
};
|
¶ User.registerThis function creates a new user profile. Params
user
Object
The object representing the user profile
callback
function
The callback function to be invoked when operation is done. The function receives 2 parameters, an error object and the user array.
|
this.register = function (user, callback) {
api.call('user/register', user, callback);
};
|
¶ User.loginWithFacebookThis function associates the current anonymous device to a Telepat user profile, using a Facebook account for authentication. Two events can be asynchronously triggered by this function:
Params
facebookToken
string
The user token obtained from Facebook after login
|
this.loginWithFacebook = function (facebookToken) {
return _login('user/login_facebook', { 'access_token': facebookToken });
};
|
¶ User.loginThis function associates the current anonymous device to a Telepat user profile, using a password for authentication. Two events can be asynchronously triggered by this function:
Params
email
string
The user's email address
password
string
The user's password
|
this.login = function (email, password) {
return _login('user/login_password', { email: email, password: password });
};
|
¶ User.loginAdminThis function associates the current anonymous device to a Telepat administrator profile, using a password for authentication. Two events can be asynchronously triggered by this function:
Params
email
string
The admin email address
password
string
The admin password
|
this.loginAdmin = function (email, password) {
return _login('admin/login', { email: email, password: password }, true);
};
|
¶ User.logoutLogs a user out. Two events can be asynchronously triggered by this function:
|
this.logout = function () {
api.call('user/logout', {}, function (err) {
if (err) {
event.emit('logout_error', error('Logout failed with error: ' + err));
} else {
api.authenticationToken = null;
setAdmin(null);
event.emit('logout');
}
});
};
};
module.exports = User;
|