UNPKG

1.3 kBJavaScriptView Raw
1import Base from 'ember-simple-auth/authenticators/base';
2import Ember from 'ember';
3
4export default Base.extend({
5 restore: function(data) {
6 return new Ember.RSVP.Promise(function(resolve, reject) {
7 if (!Ember.isEmpty(data.access_token)) {
8 resolve(data);
9 } else {
10 reject();
11 }
12 });
13 },
14
15 authenticate: function(options) {
16 if (window.location.hash) {
17 return new Ember.RSVP.Promise((resolve) => {
18 resolve({
19 access_token: window.location.hash.replace('#', ''),
20 oauthUrl: options.domainUrl //save so we know where to invalidate token later
21 });
22 });
23 } else {
24
25 return new Ember.RSVP.Promise(function(){
26 window.location = "%@/signin?response_type=token&client_id=%@&redirect_uri=%@&previous_location=%@".fmt(
27 options.domainUrl,
28 options.clientId,
29 encodeURIComponent(options.redirectUri),
30 encodeURIComponent(window.location));
31 });
32 }
33 },
34 invalidate: function(data) {
35 return new Ember.RSVP.Promise(function(resolve) {
36 Ember.$.ajax({
37 url: "%@/token?access_token=%@".fmt(data.oauthUrl, data.access_token),
38 type: 'DELETE',
39 crossDomain: true
40 }).always(function() {
41 resolve();
42 });
43 });
44 }
45});