From a5afd0f5ae1ba7c45d4dd05cd69ae1b99b4b7042 Mon Sep 17 00:00:00 2001 From: Facundo Mainere Date: Thu, 9 Jul 2020 11:44:27 -0300 Subject: [PATCH] Change AuthClient according to new React Admin docs --- src/authClient.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/authClient.js b/src/authClient.js index 7c1f84e..163c482 100644 --- a/src/authClient.js +++ b/src/authClient.js @@ -1,13 +1,6 @@ -import { - AUTH_LOGIN, - AUTH_LOGOUT, - AUTH_CHECK, - AUTH_ERROR, - AUTH_GET_PERMISSIONS, -} from 'react-admin'; import decodeJwt from 'jwt-decode'; -export default (client, options = {}) => (type, params) => { +export default (client, options = {}) => { const { storageKey, authenticate, @@ -27,18 +20,20 @@ export default (client, options = {}) => (type, params) => { logoutOnForbidden: true, }, options); - switch (type) { - case AUTH_LOGIN: + return { + login: params => { const { username, password } = params; return client.authenticate({ ...authenticate, [usernameField]: username, [passwordField]: password, }); - case AUTH_LOGOUT: + }, + logout: params => { localStorage.removeItem(permissionsKey); return client.logout(); - case AUTH_CHECK: + }, + checkAuth: params => { const hasJwtInStorage = !!localStorage.getItem(storageKey); const hasReAuthenticate = Object.getOwnPropertyNames(client).includes('reAuthenticate') && typeof client.reAuthenticate === 'function'; @@ -51,7 +46,8 @@ export default (client, options = {}) => (type, params) => { } return hasJwtInStorage ? Promise.resolve() : Promise.reject({ redirectTo }); - case AUTH_ERROR: + }, + checkError: params => { const { code } = params; if (code === 401 || (logoutOnForbidden && code === 403)) { localStorage.removeItem(storageKey); @@ -59,7 +55,8 @@ export default (client, options = {}) => (type, params) => { return Promise.reject(); } return Promise.resolve(); - case AUTH_GET_PERMISSIONS: + }, + getPermissions: params => { /* JWT token may be provided by oauth, so that's why the permissions are decoded here and not in AUTH_LOGIN. @@ -80,8 +77,6 @@ export default (client, options = {}) => (type, params) => { } catch (e) { return Promise.reject(); } - - default: - return Promise.reject(`Unsupported FeathersJS authClient action type ${type}`); + }, } };