@@ -45,88 +45,75 @@ export type LogoutResponse = {
4545}
4646
4747export function fetchAuthenticationInfo ( authUrl : string ) : Promise < AuthenticationInfo | null > {
48- return new Promise ( ( resolve , reject ) => {
49- const http = new XMLHttpRequest ( )
50-
51- http . onreadystatechange = function ( ) {
52- if ( http . readyState === XMLHttpRequest . DONE ) {
53- const status = http . status
54-
55- if ( status >= 200 && status < 300 ) {
56- try {
57- const refreshTokenAndUserInfo = parseJsonConvertingSnakeToCamel ( http . responseText )
58- resolve ( refreshTokenAndUserInfo )
59- } catch ( e ) {
60- console . error ( "Unable to process authentication response" , e )
61- reject ( {
62- status : 500 ,
63- message : "Unable to process authentication response" ,
64- } )
65- }
66- } else if ( status === 401 ) {
67- resolve ( null )
68- } else if ( status === 0 ) {
69- logCorsError ( )
70- reject ( {
71- status : 503 ,
72- message : "Unable to process authentication response" ,
73- } )
74- } else {
75- reject ( {
76- status,
77- message : http . responseText ,
78- } )
79- }
80- }
81- }
82-
83- http . open ( "get" , `${ authUrl } /api/v1/refresh_token` )
84- http . withCredentials = true
85- http . ontimeout = function ( ) {
86- reject ( {
87- status : 408 ,
88- message : "Request timed out" ,
48+ return fetch ( `${ authUrl } /api/v1/refresh_token` , {
49+ method : "GET" ,
50+ credentials : "include" ,
51+ headers : {
52+ "Content-Type" : "application/json" ,
53+ } ,
54+ } ) . then ( ( res ) => {
55+ if ( res . status === 401 ) {
56+ return null
57+ } else if ( res . status === 0 ) {
58+ logCorsError ( )
59+ return Promise . reject ( {
60+ status : 503 ,
61+ message : "Unable to process authentication response" ,
62+ } )
63+ } else if ( ! res . ok ) {
64+ return Promise . reject ( {
65+ status : res . status ,
66+ message : res . statusText ,
8967 } )
68+ } else {
69+ return parseResponse ( res )
9070 }
91- http . send ( null )
9271 } )
9372}
9473
9574export function logout ( authUrl : string ) : Promise < LogoutResponse > {
96- return new Promise ( ( resolve , reject ) => {
97- const http = new XMLHttpRequest ( )
98-
99- http . onreadystatechange = function ( ) {
100- if ( http . readyState === XMLHttpRequest . DONE ) {
101- const status = http . status
102- if ( status >= 200 && status < 300 ) {
103- const jsonResponse = JSON . parse ( http . responseText )
104- resolve ( jsonResponse )
105- } else if ( status === 0 ) {
106- logCorsError ( )
107- reject ( {
108- status : 503 ,
109- message : "Unable to process authentication response" ,
110- } )
111- } else {
112- console . error ( "Logout error" , http . status , http . responseText )
113- reject ( {
114- status,
115- message : http . responseText ,
116- } )
117- }
118- }
75+ return fetch ( `${ authUrl } /api/v1/logout` , {
76+ method : "POST" ,
77+ credentials : "include" ,
78+ headers : {
79+ "Content-Type" : "application/json" ,
80+ } ,
81+ } ) . then ( ( res ) => {
82+ if ( res . status === 0 ) {
83+ logCorsError ( )
84+ return Promise . reject ( {
85+ status : 503 ,
86+ message : "Unable to process authentication response" ,
87+ } )
88+ } else if ( ! res . ok ) {
89+ console . error ( "Logout error" , res . status , res . statusText )
90+ return Promise . reject ( {
91+ status : res . status ,
92+ message : res . statusText ,
93+ } )
94+ } else {
95+ return res . json ( )
11996 }
97+ } )
98+ }
12099
121- http . open ( "post" , `${ authUrl } /api/v1/logout` )
122- http . withCredentials = true
123- http . ontimeout = function ( ) {
124- reject ( {
125- status : 408 ,
126- message : "Request timed out" ,
100+ function parseResponse ( res : Response ) : Promise < AuthenticationInfo > {
101+ return res . text ( ) . then ( httpResponse => {
102+ try {
103+ return parseJsonConvertingSnakeToCamel ( httpResponse )
104+ } catch ( e ) {
105+ console . error ( "Unable to process authentication response" , e )
106+ return Promise . reject ( {
107+ status : 500 ,
108+ message : "Unable to process authentication response" ,
127109 } )
128110 }
129- http . send ( null )
111+ } , e => {
112+ console . error ( "Unable to process authentication response" , e )
113+ return Promise . reject ( {
114+ status : 500 ,
115+ message : "Unable to process authentication response" ,
116+ } )
130117 } )
131118}
132119
0 commit comments