@@ -94,7 +94,7 @@ export default class SlicknodeLink extends ApolloLink {
9494
9595 // Check mutations for directives and logoutMutation
9696 const resultListeners : Array < ( value : any ) => void > = [ ] ;
97- if ( currentOperation . operation === 'mutation' ) {
97+ if ( currentOperation && currentOperation . operation === 'mutation' ) {
9898 const fields : FieldNode [ ] = [ ] ;
9999 currentOperation . selectionSet . selections . forEach ( ( selectionNode ) => {
100100 if ( selectionNode . kind === 'Field' ) {
@@ -127,18 +127,7 @@ export default class SlicknodeLink extends ApolloLink {
127127 typeof result . data [ fieldName ] === 'object'
128128 ) {
129129 const tokenSet = result . data [ fieldName ] ;
130- if (
131- typeof tokenSet . accessToken === 'string' &&
132- typeof tokenSet . accessTokenLifetime === 'number' &&
133- typeof tokenSet . refreshToken === 'string' &&
134- typeof tokenSet . refreshTokenLifetime === 'number'
135- ) {
136- // Update auth tokens in storage of link
137- this . setAuthTokenSet ( tokenSet ) ;
138- this . debug ( 'Login successful, auth token set updated' ) ;
139- } else {
140- this . debug ( 'The auth token set has no valid format' ) ;
141- }
130+ this . validateAndSetAuthTokenSet ( tokenSet ) ;
142131 } else {
143132 this . debug ( 'No valid token set returned' ) ;
144133 }
@@ -319,34 +308,46 @@ export default class SlicknodeLink extends ApolloLink {
319308 observer . subscribe ( {
320309 error : ( error ) => {
321310 this . debug ( `Error refreshing AuthTokenSet: ${ error . message } ` ) ;
311+ this . logout ( ) ;
312+ resolve ( { } ) ;
322313 } ,
323314 next : ( result ) => {
324- this . debug ( 'Result' + JSON . stringify ( result ) ) ;
315+ if ( result . data && result . data . refreshAuthToken ) {
316+ this . validateAndSetAuthTokenSet ( result . data . refreshAuthToken ) ;
317+ } else {
318+ this . debug ( 'Refreshing auth token mutation failed' ) ;
319+ this . logout ( ) ;
320+ }
325321 resolve ( { } ) ;
326322 } ,
327323 } ) ;
328- /*
329- // We have refresh token but expired auth token. Refresh auth token set.
330- const result = await this.fetch<{refreshAuthToken: IAuthTokenSet | null}>(
331- REFRESH_TOKEN_MUTATION,
332- {token: refreshToken}
333- );
334- if (result && result.data && result.data.refreshAuthToken) {
335- this.setAuthTokenSet(result.data.refreshAuthToken);
336- accessToken = this.getAccessToken();
337- } else {
338- await this.logout();
339- }
340- */
341324 } else {
342325 resolve ( { } ) ;
343326 }
344327 } ) ;
345328 }
346329
330+ protected validateAndSetAuthTokenSet ( tokenSet : any ) : boolean {
331+ if (
332+ typeof tokenSet === 'object' &&
333+ typeof tokenSet . accessToken === 'string' &&
334+ typeof tokenSet . accessTokenLifetime === 'number' &&
335+ typeof tokenSet . refreshToken === 'string' &&
336+ typeof tokenSet . refreshTokenLifetime === 'number'
337+ ) {
338+ // Update auth tokens in storage of link
339+ this . setAuthTokenSet ( tokenSet ) ;
340+ this . debug ( 'Login successful, auth token set updated' ) ;
341+ return true ;
342+ }
343+
344+ this . debug ( 'The auth token set has no valid format' ) ;
345+ return false ;
346+ }
347+
347348 protected debug ( message : string ) {
348349 if ( this . options . debug ) {
349- console . debug ( `[Slicknode Auth] ${ message } ` ) ; // tslint:disable-line no-console
350+ console . log ( `[Slicknode Auth] ${ message } ` ) ; // tslint:disable-line no-console
350351 }
351352 }
352353}
0 commit comments