UNPKG

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