Skip to content

Server side errors are not displayed as a notification when authProvider is used #273

@RuSS-B

Description

@RuSS-B

API Platform version(s) affected: 2.0.1

Description
I'm facing with a really weird bug. When I'm using with authProvider the notifications from server stop being displayed when I try to edit an entity for example, however if I remove the authProvider it works properly.

How to reproduce

//App.js
import { Redirect, Route } from "react-router-dom";
import { HydraAdmin, hydraDataProvider as baseHydraDataProvider, fetchHydra as baseFetchHydra } from "@api-platform/admin";
import parseHydraDocumentation from "@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation";
import authProvider from "./authProvider";

const entrypoint = 'http://localhost/api';
const fetchHeaders = {};
const fetchHydra = (url, options = {credentials : 'include'}) => {
  return baseFetchHydra(url, {
    ...options,
    options: {credentials : 'include'},
    headers: new Headers(fetchHeaders),
  })
};
const apiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint, { headers: new Headers(fetchHeaders) })
    .then(
        ({ api }) => ({ api }),
        (result) => {
            switch (result.status) {
                case 401:
                    return Promise.resolve({
                        api: result.api,
                        customRoutes: [
                          <Route path="/" render={() => {
                            return window.localStorage.getItem("loggedIn") ? window.location.reload() : <Redirect to="/login" />
                        }} />
                        ],
                    });
                default:
                    return Promise.reject(result);
            }
        },
    );
const dataProvider = baseHydraDataProvider(entrypoint, fetchHydra, apiDocumentationParser);


const app = () => (<HydraAdmin
      authProvider={authProvider}
      dataProvider={dataProvider}
      entrypoint={ entrypoint }
  >
  </HydraAdmin>
);

export default app;
//.authProvider.js
const entrypoint = 'http://localhost/api';

const authProvider = {
    login: ({ username, password }) =>  {
        const request = new Request(entrypoint + '/login_check', {
            method: 'POST',
            body: JSON.stringify({ username, password }),
            // credentials : 'include',
            headers: new Headers({ 'Content-Type': 'application/json' }),
        });

        return fetch(request)
            .then(response => {
                if (response.status < 200 || response.status >= 300) {
                    throw new Error(response.statusText);
                }

                return response.json();
            })
            .then(({ token }) => {
                localStorage.setItem('loggedIn', true);
            });
    },
    logout: () => {
        localStorage.removeItem('loggedIn');
        return Promise.resolve();
    },
    checkAuth: () => { 
        return localStorage.getItem('loggedIn') ? Promise.resolve() : Promise.reject({ redirectTo: '/login' })
    },
    checkError: (error) => {
        Promise.resolve();
    },
    getPermissions: params => Promise.resolve(),
};

export default authProvider;

Additional Context
Also attaching the response from server, but it's just an ApiPlatform out of the box on Symfony 5 with couple of constraints such as @Assert\Email and @Assert\Length(min=2, max=2)

{"@context":"\/api\/contexts\/ConstraintViolationList","@type":"ConstraintViolationList","hydra:title":"An error occurred","hydra:description":"email: This value is not a valid email address.\ncountryCode: This value should have exactly 2 characters.","violations":[{"propertyPath":"email","message":"This value is not a valid email address."},{"propertyPath":"countryCode","message":"This value should have exactly 2 characters."}]}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions