UNPKG

1.44 kBJavaScriptView Raw
1/* eslint-disable prettier/prettier */
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';
6import window from 'ember-window-mock';
7import fetch from 'fetch';
8
9const { authmaker } = config;
10
11export 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 //save so we know where to invalidate token later
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 //ignore this error
45 }
46
47 return this._super();
48 }
49});