Skip to content

Instantly share code, notes, and snippets.

@stdavis
Created October 12, 2012 15:55
Show Gist options
  • Select an option

  • Save stdavis/3879916 to your computer and use it in GitHub Desktop.

Select an option

Save stdavis/3879916 to your computer and use it in GitHub Desktop.
define([
'dojo/_base/declare',
'dojo/_base/Deferred',
'cem/Helpers',
'dojo/cookie',
'dojo/json'
],
function (declare, Deferred, helpers, cookie, json) {
return declare('cem.Authentication', null, {
// summary:
// In charge of everything related to authentication
// Stores username and password.
// Communicates with server.
// isSignedIn: Boolean
// description
isSignedIn: false,
constructor: function () {
// summary:
// description
console.log(this.declaredClass + "::" + arguments.callee.nom, arguments);
localStorage.removeItem('sessionid');
},
signIn: function (email, password) {
// summary:
// Set's the values to local storage
// Sends them to the log in service
// returns: dojo.Deferred
console.log(this.declaredClass + "::" + arguments.callee.nom, arguments);
var that = this;
// store the values in local storage
localStorage.email = email;
localStorage.password = password;
var params = {
url: CEM.urls.login,
handleAs: 'json',
headers: {
"Content-Type": "application/json",
"accept": "application/json",
"accept-charset": "ISO-8859-1,utf-8"
},
postData: json.stringify({
"userName": email,
"password": password
}),
load: function(response) {
console.log('login response: ', response);
if (helpers.checkResponseForSuccess(response)) {
that.onSignInSuccess(response);
that.isSignedIn = true;
}
},
error: function(error) {
console.error('error with login xhr: ', error);
}
};
return dojo.xhrPost(params);
},
// forgotPassword: function (email) {
// // summary:
// // Sends the email to the forgot password service
// console.log(this.declaredClass + "::" + arguments.callee.nom, arguments);
// var def = new dojo.Deferred();
// // mock return
// function mockReturn() {
// def.resolve({
// status: 'success'
// });
// }
// setTimeout(mockReturn, 1000);
// return def;
// },
onSignInSuccess: function () {
// summary:
// Event for other objects to hook to.
// Also sets the localStorage data
// response: Object
// The response object as returned by the service.
console.log(this.declaredClass + "::" + arguments.callee.nom, arguments);
},
signOut: function () {
// summary:
// description
console.log(this.declaredClass + "::" + arguments.callee.nom, arguments);
localStorage.removeItem('email');
localStorage.removeItem('password');
console.log('cookie: ' + cookie('sessionid'));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment