Created
July 24, 2014 17:05
-
-
Save James1x0/f37d6e935eafe5e13aa8 to your computer and use it in GitHub Desktop.
Session Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
/* | |
App Initialized Session Controller | |
*/ | |
export default Ember.Controller.extend({ | |
logout: function () { | |
// Find the session | |
var ses = this.store.find('session', this.get('content').get('id')); | |
// Delete the session | |
ses.destroyRecord(); | |
// Setup our headers for no session | |
Ember.$.ajaxSetup({ | |
headers: { | |
'Session': null | |
} | |
}); | |
}, | |
login: function () { | |
var data = this.getProperties('email', 'password'), | |
self = this; | |
this.set('loggingIn', true); | |
Ember.$.post('/api/login', data).then(function (res) { | |
Ember.$.ajaxSetup({ | |
headers: { | |
'Session': res.token | |
} | |
}); | |
var session = self.store.createRecord('session', res); | |
session.save(); | |
self.setProperties({ | |
content: session, | |
authenticated: true, | |
loggingIn: false | |
}); | |
}, function (res) { | |
if (error.status === 401) { | |
self.setProperties({ | |
authenticated: false, | |
loggingIn: false | |
}); | |
} else { | |
self.setProperties({ | |
authenticated: false, | |
loginError: res.responseText, | |
loggingIn: false | |
}); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment