Skip to content

Instantly share code, notes, and snippets.

@James1x0
Created July 24, 2014 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save James1x0/f37d6e935eafe5e13aa8 to your computer and use it in GitHub Desktop.
Save James1x0/f37d6e935eafe5e13aa8 to your computer and use it in GitHub Desktop.
Session Controller
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