3
3
* @submodule timed-services
4
4
* @public
5
5
*/
6
- import { computed } from "@ember/object" ;
7
6
import { reads } from "@ember/object/computed" ;
8
7
import { inject as service } from "@ember/service" ;
8
+ import { isUnauthorizedError } from "ember-ajax/errors" ;
9
9
import AjaxService from "ember-ajax/services/ajax" ;
10
10
11
11
/**
@@ -15,22 +15,17 @@ import AjaxService from "ember-ajax/services/ajax";
15
15
* @extends EmberAjax.AjaxService
16
16
* @public
17
17
*/
18
- export default AjaxService . extend ( {
19
- /**
20
- * The session service
21
- *
22
- * @property {EmberSimpleAuth.SessionService } session
23
- * @public
24
- */
25
- session : service ( "session" ) ,
18
+ export default class AjaxCustomService extends AjaxService {
19
+ @service session ;
20
+ @service router ;
26
21
27
22
/**
28
23
* The JWT access token.
29
24
*
30
25
* @property {String } token
31
26
* @public
32
27
*/
33
- token : reads ( "session.data.authenticated.access_token" ) ,
28
+ @ reads ( "session.data.authenticated.access_token" ) token ;
34
29
35
30
/**
36
31
* The HTTP request headers
@@ -40,16 +35,28 @@ export default AjaxService.extend({
40
35
* @property {Object } headers
41
36
* @public
42
37
*/
43
- headers : computed ( "token" , function ( ) {
38
+ get headers ( ) {
44
39
const headers = {
45
40
Accept : "application/vnd.api+json" ,
46
41
"Content-Type" : "application/vnd.api+json"
47
42
} ;
48
43
49
- const auth = this . get ( "token" )
50
- ? { Authorization : `Bearer ${ this . get ( "token" ) } ` }
51
- : { } ;
44
+ const auth = this . token ? { Authorization : `Bearer ${ this . token } ` } : { } ;
52
45
53
46
return Object . assign ( headers , auth ) ;
54
- } )
55
- } ) ;
47
+ }
48
+
49
+ handleResponse ( status , headers , payload , requestData ) {
50
+ const response = super . handleResponse (
51
+ status ,
52
+ headers ,
53
+ payload ,
54
+ requestData
55
+ ) ;
56
+
57
+ if ( isUnauthorizedError ( response ) ) {
58
+ this . router . transitionTo ( "login" ) ;
59
+ }
60
+ return response ;
61
+ }
62
+ }
0 commit comments