Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Change AuthClient according to React Admin v3 docs #146

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 +20,19 @@ 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 => {
return client.logout().then(() => localStorage.removeItem(permissionsKey));
case AUTH_CHECK:
},
checkAuth: params => {
const hasJwtInStorage = !!localStorage.getItem(storageKey);
const hasReAuthenticate = Object.getOwnPropertyNames(client).includes('reAuthenticate')
&& typeof client.reAuthenticate === 'function';
Expand All @@ -50,15 +45,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 @@ -79,8 +76,6 @@ export default (client, options = {}) => (type, params) => {
} catch (e) {
return Promise.reject();
}

default:
return Promise.reject(`Unsupported FeathersJS authClient action type ${type}`);
},
}
};