diff --git a/admin/authentication-support.md b/admin/authentication-support.md index 3a4f2163099..7e9e131d113 100644 --- a/admin/authentication-support.md +++ b/admin/authentication-support.md @@ -15,10 +15,15 @@ export default (type, params) => { switch (type) { case AUTH_LOGIN: const { username, password } = params; - const request = new Request(`${entrypoint}/login_check`, { - method: 'POST', - body: JSON.stringify({ email: username, password }), - headers: new Headers({ 'Content-Type': 'application/json' }), + const request = new Request(`${API_ENTRYPOINT}/login`, { + body: JSON.stringify({ + username: params.username, + password: params.password, + }), + headers: new Headers({ + 'Content-Type': 'application/json', + }), + method: 'POST', }); return fetch(request) @@ -29,6 +34,7 @@ export default (type, params) => { }) .then(({ token }) => { localStorage.setItem('token', token); // The JWT token is stored in the browser's local storage + window.location.replace('/'); }); case AUTH_LOGOUT: @@ -52,24 +58,60 @@ export default (type, params) => { Then, configure the `Admin` component to use the authentication client we just created: ```javascript -import React, { Component } from 'react'; -import { HydraAdmin, hydraClient, fetchHydra } from 'api-platform-admin'; +import React from 'react'; +import parseHydraDocumentation from 'api-doc-parser/lib/hydra/parseHydraDocumentation'; +import { HydraAdmin, hydraClient, fetchHydra as baseFetchHydra } from '@api-platform/admin'; import authClient from './authClient'; +import { Redirect } from 'react-router-dom'; -const entrypoint = 'https://demo.api-platform.com'; - -const fetchWithAuth = (url, options = {}) => { - if (!options.headers) options.headers = new Headers({ Accept: 'application/ld+json' }); +const entrypoint = 'https://demo.api-platform.com'; // Change this by your own entrypoint - options.headers.set('Authorization', `Bearer ${localStorage.getItem('token')}`); - return fetchHydra(url, options); +const fetchHeaders = { + 'Authorization': `Bearer ${window.localStorage.getItem('token')}`, }; -class Admin extends Component { - render() { - return - } -} +const fetchHydra = (url, options = {}) => baseFetchHydra(url, { + ...options, + headers: new Headers(fetchHeaders), +}); + +const restClient = api => hydraClient(api, fetchHydra); + +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: [ + { + props: { + path: '/', + render: () => ( + + ), + }, + }, + ], + }); + + default: + return Promise.reject(result); + } + }, + ) +; + +export default props => ( + +); export default Admin; ```