Skip to content
Closed
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# Changelog

## [2.0.2](https://github.com/collective/volto-authomatic/compare/2.0.1...2.0.2) (2025-08-19)

### Bug Fixes

- Remove unnecessary local storage (redux) for anonymous users (#38) - Prevents auth reducer data from being stored in local storage when users are not authenticated, improving performance and reducing unnecessary data persistence

## [2.0.1](https://github.com/collective/volto-authomatic/compare/2.0.0...2.0.1) (2024-01-23)

## [2.0.0](https://github.com/collective/volto-authomatic/compare/1.3.0...2.0.0) (2023-11-17)


### Features


- feat: Add support for pas.plugins.oidc (#12) ([fe191764b7f81f2c87a7618e165cfa3f23cb60aa](https://github.com/collective/volto-authomatic/commit/fe191764b7f81f2c87a7618e165cfa3f23cb60aa))
- feat: Added Spanish support ([cd760708797b3465b88b96594c77ea3811a96828](https://github.com/collective/volto-authomatic/commit/cd760708797b3465b88b96594c77ea3811a96828))

## [1.3.0](https://github.com/collective/volto-authomatic/compare/1.2.0...1.3.0) (2022-12-05)


### Features


- feat: Add fallback_login route pointing to the normal login form ([7cc9cffda2a0aab50f2c9729a60aeae709fca4d4](https://github.com/collective/volto-authomatic/commit/7cc9cffda2a0aab50f2c9729a60aeae709fca4d4))

## [1.2.0](https://github.com/collective/volto-authomatic/compare/1.1.0...1.2.0) (2022-07-25)


### Features


- feat: Apply i18n to Login and Logout views ([0c996958828e18e4d058e0fa2c8d4806297764ff](https://github.com/collective/volto-authomatic/commit/0c996958828e18e4d058e0fa2c8d4806297764ff))
- feat: Rename package to @plone-collective/volto-authomatic ([8bdd8dd1d6e7f676f05f47db449307fbd5ba026e](https://github.com/collective/volto-authomatic/commit/8bdd8dd1d6e7f676f05f47db449307fbd5ba026e))

Expand Down Expand Up @@ -61,4 +61,4 @@

### Feature

- Initial release
- Initial release
2 changes: 1 addition & 1 deletion src/components/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function Login({ intl }) {
window.location.href = next_url;
}, 500);
}
}, [startedOIDC, loginOIDCValues]);
}, [startedOIDC, loginOIDCValues]); // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
if (options !== undefined && options.length === 1 && options[0].id === 'oidc') {
Expand Down
2 changes: 1 addition & 1 deletion src/components/LoginOIDC/LoginOIDC.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function LoginOIDC({ intl }) {
toast.dismiss('loginFailed');
}
}
}, [token, history]);
}, [token, history]); // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
if (error) {
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FallbackLogin from './components/FallbackLogin';
import Login from './components/Login/Login';
import conditionalPersistenceMiddleware from './middleware/conditionalPersistenceMiddleware';
import LoginAuthomatic from './components/LoginAuthomatic/LoginAuthomatic';
import LoginOIDC from './components/LoginOIDC/LoginOIDC';
import Logout from './components/Logout/Logout';
Expand All @@ -13,8 +14,9 @@ const applyConfig = (config) => {
oidcLogout,
oidcRedirect,
};
config.settings.persistentReducers = [...config.settings.persistentReducers, 'authomaticRedirect', 'oidcLogout', 'oidcRedirect'];
config.settings.nonContentRoutes = [...config.settings.nonContentRoutes, /^\/login-authomatic\/.*$/, /^\/login-oidc\/.*$/, '/fallback_login'];

config.settings.storeExtenders = [...(config.settings.storeExtenders || []), (middlewares) => [conditionalPersistenceMiddleware, ...middlewares]];
config.settings.nonContentRoutes = [...config.settings.nonContentRoutes, /^\/login-authomatic\/.*$/, /^\/login-oidc\/.*$/];
config.addonRoutes.push(
{ path: '/fallback_login', component: FallbackLogin },
{ path: '/**/fallback_login', component: FallbackLogin },
Expand Down
47 changes: 47 additions & 0 deletions src/middleware/conditionalPersistenceMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Conditional Persistence Middleware
* Prevents local storage initialization for authentication reducers when user is anonymous
* @module middleware/conditionalPersistenceMiddleware
*/

const CONDITIONAL_REDUCERS = ['authomaticRedirect', 'oidcLogout', 'oidcRedirect'];

const isUserAuthenticated = (state) => {
return !!(state?.userSession?.token || state?.users?.user?.token || state?.users?.user?.id || state?.userSession?.user?.id);
};

/**
* Removes auth reducer data from local storage for anonymous users
*/
const preventAnonymousLocalStorage = () => {
if (typeof window === 'undefined' || !window.localStorage) {
return;
}

try {
CONDITIONAL_REDUCERS.forEach((reducerName) => {
const key = `redux_localstorage_simple_${reducerName}`;
if (window.localStorage.getItem(key)) {
window.localStorage.removeItem(key);
}
});
} catch (error) {}
};

/**
* Manages conditional persistence of auth reducers in local storage
*/
const conditionalPersistenceMiddleware = (store) => (next) => (action) => {
const result = next(action);
const state = store.getState();
const isAuthenticated = isUserAuthenticated(state);

// Clean up local storage for anonymous users after every action
if (!isAuthenticated) {
preventAnonymousLocalStorage();
}

return result;
};

export default conditionalPersistenceMiddleware;