1 |
|
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 | import window from 'ember-window-mock';
|
7 | import fetch from 'fetch';
|
8 |
|
9 | const { authmaker } = config;
|
10 |
|
11 | export default Base.extend({
|
12 | restore: function(data) {
|
13 | return new EmberPromise(function(resolve, reject) {
|
14 | if (!isEmpty(data.access_token)) {
|
15 | resolve(data);
|
16 | } else {
|
17 | reject();
|
18 | }
|
19 | });
|
20 | },
|
21 |
|
22 | authenticate: function() {
|
23 | if (window.location.hash) {
|
24 | return new EmberPromise((resolve) => {
|
25 | resolve({
|
26 | access_token: window.location.hash.replace('#', ''),
|
27 | oauthUrl: authmaker.domainUrl
|
28 | });
|
29 | });
|
30 | } else {
|
31 |
|
32 | return new EmberPromise(function(){
|
33 | window.location.href = `${authmaker.domainUrl}/signin?response_type=token&client_id=${authmaker.clientId}&redirect_uri=${encodeURIComponent(authmaker.redirectUri)}&previous_location=${encodeURIComponent(window.location)}`;
|
34 | });
|
35 | }
|
36 | },
|
37 | invalidate: async function(data) {
|
38 | try {
|
39 | await fetch(`${data.oauthUrl}/token?access_token=${data.access_token}`, {
|
40 | credentials: "include",
|
41 | method: 'DELETE',
|
42 | });
|
43 | } catch (err) {
|
44 |
|
45 | }
|
46 |
|
47 | return this._super();
|
48 | }
|
49 | });
|