Skip to content

Commit

Permalink
Change AuthClient according to new React Admin docs
Browse files Browse the repository at this point in the history
  • Loading branch information
FacundoMainere committed Jul 9, 2020
1 parent 5989a32 commit a5afd0f
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/authClient.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand All @@ -51,15 +46,17 @@ 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);
localStorage.removeItem(permissionsKey);
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.
Expand All @@ -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}`);
},
}
};

0 comments on commit a5afd0f

Please sign in to comment.