nuxt-alt / auth

An alternative module to @nuxtjs/auth
https://nuxt-alt-auth.vercel.app/
MIT License
112 stars 20 forks source link

Fix onResponseError chain call #92

Closed Vespand closed 10 months ago

Vespand commented 10 months ago

Method onResponseError doesn't work (also all $http.interceptors.response with errors handlers)

This PR fix problem

How to reproduce:


With fix onResponseErrorWork will be true and console.log will call
Without fix onResponseErrorWork will be false, because onResponseError never called in chain after request handler interceptor
Without fix all calls with `$http.get` and others methods newer throw error (`catch` never «catch»)
Denoder commented 10 months ago

Not sure I should go with with this PR, the true boolean I had set was only meant to work for a specific error to reset. (That error being the response status being 401) Otherwise you are able to set your own resetOnResponseError function.

Vespand commented 10 months ago

This is not about resetOnResponseError setting at all

Any «rejected» interceptor should throw error for next «rejected» interceptor, otherwise error will be silent and any request will be success (no error handling)

There is logic @refactorjs/ofetch for interceptors - https://github.com/refactorjs/ofetch/blob/main/src/ofetch.ts#L239 If function will not return anything - call Promise.reject(error), why current interceptor is different and break this logic in chain?

This PR will add same logic, throwing error

I can rewrite this code, for using onResponseError, which is actually better and cleaner, but onResponseError need to return number, such as

this.responseInterceptor = this.http.onResponseError(error => {
    if (typeof this.auth.options.resetOnResponseError === 'function') {
        this.auth.options.resetOnResponseError(error, this.auth, this.scheme)
    }
    else if (this.auth.options.resetOnResponseError && error?.response?.status === 401) {
        this.scheme.reset?.()
        throw new ExpiredAuthSessionError();
    }
});

But need fix for @refactorjs/ofetch, for returning number for responseInterceptor and similar hooks

onResponseError(fn: (error: any) => any): number {
    return this.interceptors.response.use(undefined, (error: any) => fn(error) || Promise.reject(error))
}

I hope it's cleaner answer what happening and where is problem :)

Denoder commented 10 months ago

I understand what you're saying now, I'll put this in v3.1.1 so for now ill close this.