Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataProvider] Empty Headers and 401 response #100

Closed
RSalo opened this issue Jun 24, 2021 · 8 comments
Closed

[DataProvider] Empty Headers and 401 response #100

RSalo opened this issue Jun 24, 2021 · 8 comments

Comments

@RSalo
Copy link

RSalo commented Jun 24, 2021

API Platform version(s) affected: 2.6.4

Description
I try to add JWT token to the header, but it stays empty no matter what returning 401 response.

How to reproduce

function App() {

    const getHeaders = () => localStorage.getItem("token") ? {
        Authorization: `Bearer ${localStorage.getItem("token")}`,
    } : {};

    const customFetchHydra = (url, options = {}) => {
        fetchHydra(url, {
            ...options,
            headers: new Headers(getHeaders()),
        })
    };

    const dataProvider = hydraDataProvider(API_ENTRYPOINT, customFetchHydra);

    return (
        <HydraAdmin ... dataProvide={dataProvider} ...>
            ...
        </HydraAdmin>
    );
}

Possible Solution
~

Additional Context
I tried to debug compiled fetchJsonLd.js:
image

then I was trying to show all headers in the chrome console at that moment:
image

I have no idea where "next" comes from...

@alanpoulain
Copy link
Member

Hello, have you followed https://api-platform.com/docs/admin/authentication-support/?

@RSalo
Copy link
Author

RSalo commented Jun 24, 2021

@alanpoulain Hello. Yes, I did all according to this documentation.

@alanpoulain
Copy link
Member

It doesn't seem so. For instance:

    const customFetchHydra = (url, options = {}) => {
        fetchHydra(url, {
            ...options,
            headers: new Headers(getHeaders()),
        })
    };

Should be:

    const customFetchHydra = (url, options = {}) => {
        fetchHydra(url, {
            ...options,
            headers: getHeaders,
        })
    };

@RSalo
Copy link
Author

RSalo commented Jun 24, 2021

@alanpoulain i did it too. Was trying different variants and nothing change...

@alanpoulain
Copy link
Member

You need to change the apiDocumentationParser too:

const apiDocumentationParser = async (entrypoint) => {
    const { api } = await parseHydraDocumentation(entrypoint, { headers: getHeaders });
    return { api };
};

@RSalo
Copy link
Author

RSalo commented Jun 24, 2021

@alanpoulain added as third parameter

    const apiDocumentationParser = async (entrypoint) => {
        try {
            const { api } = await parseHydraDocumentation(entrypoint, { headers: getHeaders });
            return { api };
        } catch (result) {
            if (result.status === 401) {
                // Prevent infinite loop if the token is expired
                localStorage.removeItem("token");

                return {
                    api: result.api,
                    customRoutes: [
                        <Route path="/" />
                    ],
                };
            }

            throw result;
        }
    };
    const dataProvider = hydraDataProvider(API_ENTRYPOINT, customFetchHydra, apiDocumentationParser);

And the same situation. I have no idea, it's my fail or bug...

@RSalo
Copy link
Author

RSalo commented Jun 24, 2021

Find out that we have no header options at all:
https://github.com/api-platform/admin/blob/main/src/hydra/dataProvider.js#L245
So "options.headers" is empty and we creating an empty header object
https://github.com/api-platform/admin/blob/main/src/hydra/fetchHydra.js#L20
As we have clear headers our parser makes a request without any headers
https://github.com/api-platform/admin/blob/main/src/hydra/fetchHydra.js#L28

I can be mistaken...

@alanpoulain
Copy link
Member

Closing because it was not followed. It seems other users don't have similar issues. Please reopen if it's the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants