Created
May 27, 2014 17:13
-
-
Save joshsmith/2d291585183125ea9857 to your computer and use it in GitHub Desktop.
This file contains 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
var TokenAuth = Ember.Object.extend({ | |
accessToken: $.cookie('accessToken'), | |
serverTokenEndpoint: 'http://api.lvh.me:3000/oauth/token/', | |
init: function() { | |
if(this.isAuthenticated) { | |
DS.RESTAdapter.reopen({ | |
headers: { | |
"Authorization": "Bearer " + this.accessToken | |
} | |
}); | |
} else { | |
DS.RESTAdapter.reopen({ headers: {} }); | |
}; | |
}, | |
isAuthenticated: function() { | |
if(this.accessToken !== null) { | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
authenticate: function(data) { | |
var requestData = Ember.$.extend(data, { | |
grant_type: 'password', | |
client_id: 'c6ef9c560c4ee75931590fd9159fea5ba7837df87c9717da913df150ed966b36', | |
client_secret: 'b10706216c99511f762ed3bcb29bb33029ba3e0eaf17a8c8a424075029e0dd4e' | |
}); | |
var auth = this; | |
return Ember.RSVP.Promise.cast( | |
Ember.$.ajax({ | |
url: this.serverTokenEndpoint, | |
type: 'POST', | |
data: requestData, | |
dataType: 'json', | |
contentType: 'application/x-www-form-urlencoded' | |
}) | |
).then(function(response) { | |
$.cookie('accessToken', response.access_token); | |
auth.set('isAuthenticated', true); | |
}, function(error) { | |
$.cookie('accessToken', null); | |
auth.set('isAuthenticated', false); | |
}); | |
} | |
}); | |
export default TokenAuth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment