From 51a59e681184cdec411ef00485eaed58dc7e81fd Mon Sep 17 00:00:00 2001 From: Zak Burke Date: Thu, 12 Aug 2021 23:40:20 -0400 Subject: [PATCH 1/3] FOLIO-3253 tweak login to avoid users-bl POC refactor of login to avoid `users-bl`, which has lots of dependencies, in favor of `authn/login`, which has very few. This is not intended to be pretty (obviously, the extra requests need cleaning up and error handling) but just to demonstrate that it is possible to login from the UI against a minimal set of back-end and front-end modules. Refs FOLIO-3253 --- src/loginServices.js | 47 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/loginServices.js b/src/loginServices.js index 9b972bbd7..9736f229e 100644 --- a/src/loginServices.js +++ b/src/loginServices.js @@ -348,6 +348,8 @@ function createOkapiSession(okapiUrl, store, tenant, token, data) { servicePoints[0] : servicePoints.find(sp => sp.id === curSpId); + console.log(data) + const user = { id: data.user.id, username: data.user.username, @@ -487,7 +489,29 @@ function processOkapiSession(okapiUrl, store, tenant, resp, ssoToken) { handleLoginError(dispatch, resp); } else { resp.json() - .then(json => createOkapiSession(okapiUrl, store, tenant, token, json)) + .then(json => json.users[0]) + .then(user => { + const headers = { + 'X-Okapi-Tenant': tenant, + 'X-Okapi-Token': token, + 'Content-Type': 'application/json', + }; + + return fetch(`${okapiUrl}/perms/users/${user.id}?indexField=userId`, { + headers, + method: 'GET', + }) + .then(response => response.json()) + .then(permissions => { + return { + user, + permissions + } + }) + }) + .then(data => { + createOkapiSession(okapiUrl, store, tenant, token, { user: data.user, permissions: data.permissions }); + }) .then(() => { store.dispatch(setOkapiReady()); }); @@ -528,12 +552,29 @@ export function checkOkapiSession(okapiUrl, store, tenant) { * @param {*} data */ export function requestLogin(okapiUrl, store, tenant, data) { - return fetch(`${okapiUrl}/bl-users/login?expandPermissions=true&fullPermissions=true`, { + let token = null; + return fetch(`${okapiUrl}/authn/login`, { method: 'POST', headers: { 'X-Okapi-Tenant': tenant, 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) - .then(resp => processOkapiSession(okapiUrl, store, tenant, resp)); + .then(resp => { + if (resp.status >= 400) { + handleLoginError(dispatch, resp); + } else { + token = resp.headers.get('X-Okapi-Token') || ssoToken; + const headers = { + 'X-Okapi-Tenant': tenant, + 'X-Okapi-Token': token, + 'Content-Type': 'application/json', + }; + return fetch(`${okapiUrl}/users?query=username==${data.username}`, { + headers, + method: 'GET', + }) + } + }) + .then(resp => processOkapiSession(okapiUrl, store, tenant, resp, token)); } /** From 4d577f2e21f96ddf1d23a1aabb8d190008e16d93 Mon Sep 17 00:00:00 2001 From: Zak Burke Date: Wed, 29 Sep 2021 15:05:12 -0400 Subject: [PATCH 2/3] lint --- src/loginServices.js | 50 +++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/loginServices.js b/src/loginServices.js index 5edc87233..dfd529b4c 100644 --- a/src/loginServices.js +++ b/src/loginServices.js @@ -348,8 +348,6 @@ function createOkapiSession(okapiUrl, store, tenant, token, data) { servicePoints[0] : servicePoints.find(sp => sp.id === curSpId); - console.log(data) - const user = { id: data.user.id, username: data.user.username, @@ -503,13 +501,13 @@ function processOkapiSession(okapiUrl, store, tenant, resp, ssoToken) { headers, method: 'GET', }) - .then(response => response.json()) - .then(permissions => { - return { - user, - permissions - } - }) + .then(response => response.json()) + .then(permissions => { + return { + user, + permissions + }; + }); }) .then(data => { createOkapiSession(okapiUrl, store, tenant, token, { user: data.user, permissions: data.permissions }); @@ -560,23 +558,23 @@ export function requestLogin(okapiUrl, store, tenant, data) { headers: { 'X-Okapi-Tenant': tenant, 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) - .then(resp => { - if (resp.status >= 400) { - handleLoginError(dispatch, resp); - } else { - token = resp.headers.get('X-Okapi-Token') || ssoToken; - const headers = { - 'X-Okapi-Tenant': tenant, - 'X-Okapi-Token': token, - 'Content-Type': 'application/json', - }; - return fetch(`${okapiUrl}/users?query=username==${data.username}`, { - headers, - method: 'GET', - }) - } - }) - .then(resp => processOkapiSession(okapiUrl, store, tenant, resp, token)); + .then(resp => { + if (resp.status >= 400) { + handleLoginError(dispatch, resp); + } else { + token = resp.headers.get('X-Okapi-Token') || ssoToken; + const headers = { + 'X-Okapi-Tenant': tenant, + 'X-Okapi-Token': token, + 'Content-Type': 'application/json', + }; + return fetch(`${okapiUrl}/users?query=username==${data.username}`, { + headers, + method: 'GET', + }); + } + }) + .then(resp => processOkapiSession(okapiUrl, store, tenant, resp, token)); } /** From 4acbc25f8cf6ae8122404cab8441c25691fadf2c Mon Sep 17 00:00:00 2001 From: Zak Burke Date: Thu, 30 Sep 2021 11:57:42 -0400 Subject: [PATCH 3/3] even _more_ lint --- src/loginServices.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/loginServices.js b/src/loginServices.js index dfd529b4c..0d0fcde78 100644 --- a/src/loginServices.js +++ b/src/loginServices.js @@ -560,9 +560,9 @@ export function requestLogin(okapiUrl, store, tenant, data) { }) .then(resp => { if (resp.status >= 400) { - handleLoginError(dispatch, resp); + handleLoginError(store.dispatch, resp); } else { - token = resp.headers.get('X-Okapi-Token') || ssoToken; + token = resp.headers.get('X-Okapi-Token'); const headers = { 'X-Okapi-Tenant': tenant, 'X-Okapi-Token': token,