1 | import $ from 'jquery';
|
2 | import { isEmpty } from '@ember/utils';
|
3 | import { Promise as EmberPromise } from 'rsvp';
|
4 | import Base from 'ember-simple-auth/authenticators/base';
|
5 | import config from 'ember-get-config';
|
6 |
|
7 | const { authmaker } = config;
|
8 |
|
9 | export 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
|
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 | });
|