From 64078b5bab3e45bb8e93ef26976b2386757d71cd Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 28 Sep 2017 18:59:56 +0200 Subject: [PATCH 1/3] Update admin authentification Add https://github.com/api-platform/admin/pull/51 changes and change the request for compatibility with LexikJWTAuthenticationBundle (tested and works) --- admin/authentication-support.md | 67 +++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/admin/authentication-support.md b/admin/authentication-support.md index 3a4f2163099..3645028d7cf 100644 --- a/admin/authentication-support.md +++ b/admin/authentication-support.md @@ -17,8 +17,9 @@ export default (type, params) => { 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' }), + // the next two lines are compatible with LexikJWTAuthenticationBundle. Change this if you use something else ... + body: `_username=${username}&_password=${password}`, + headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }), }); return fetch(request) @@ -52,24 +53,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; ``` From 541d51dfd6c02f971895098be55e6bebae24999b Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 9 Oct 2017 16:57:47 +0200 Subject: [PATCH 2/3] Add window.location.replace('/'); --- admin/authentication-support.md | 1 + 1 file changed, 1 insertion(+) diff --git a/admin/authentication-support.md b/admin/authentication-support.md index 3645028d7cf..ba2b92dbf4c 100644 --- a/admin/authentication-support.md +++ b/admin/authentication-support.md @@ -30,6 +30,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: From 730bffdcf6b3a91d45c215e1098e7dd341d59a73 Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 10 Oct 2017 13:36:45 +0200 Subject: [PATCH 3/3] JSON authentification instead of form data --- admin/authentication-support.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/admin/authentication-support.md b/admin/authentication-support.md index ba2b92dbf4c..7e9e131d113 100644 --- a/admin/authentication-support.md +++ b/admin/authentication-support.md @@ -15,11 +15,15 @@ export default (type, params) => { switch (type) { case AUTH_LOGIN: const { username, password } = params; - const request = new Request(`${entrypoint}/login_check`, { - method: 'POST', - // the next two lines are compatible with LexikJWTAuthenticationBundle. Change this if you use something else ... - body: `_username=${username}&_password=${password}`, - headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }), + 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)