|
| 1 | +/** |
| 2 | + * OVERRIDE: AuthToken.js |
| 3 | + * REASON: Removing '__ac' cookie to avoid login-logout flicker issues |
| 4 | + * DATE: 2026-01-06 |
| 5 | + * DEVELOPER: @Tishasoumya-02 |
| 6 | + */ |
| 7 | + |
| 8 | +import Cookies from 'universal-cookie'; |
| 9 | +import jwtDecode from 'jwt-decode'; |
| 10 | +import { loginRenew } from '@plone/volto/actions/userSession/userSession'; |
| 11 | +import { getCookieOptions } from '@plone/volto/helpers/Cookies/cookies'; |
| 12 | +import { push } from 'connected-react-router'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Get auth token method (does not work in SSR) |
| 16 | + * @method getAuthToken |
| 17 | + * @returns {undefined} |
| 18 | + */ |
| 19 | +export function getAuthToken() { |
| 20 | + const cookies = new Cookies(); |
| 21 | + return cookies.get('auth_token'); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Persist auth token method. |
| 26 | + * @method persistAuthToken |
| 27 | + * @param {object} store Redux store. |
| 28 | + * @returns {undefined} |
| 29 | + */ |
| 30 | +export function persistAuthToken(store, req) { |
| 31 | + const cookies = new Cookies(); |
| 32 | + let currentValue; |
| 33 | + if (req) { |
| 34 | + // We are in SSR |
| 35 | + currentValue = req.universalCookies.get('auth_token'); |
| 36 | + } else { |
| 37 | + currentValue = cookies.get('auth_token'); |
| 38 | + } |
| 39 | + /** |
| 40 | + * handleChange method. |
| 41 | + * @method handleChange |
| 42 | + * @param {bool} initial Initial call. |
| 43 | + * @returns {undefined} |
| 44 | + */ |
| 45 | + function handleChange(initial) { |
| 46 | + const previousValue = currentValue; |
| 47 | + const state = store.getState(); |
| 48 | + currentValue = state.userSession.token; |
| 49 | + if ( |
| 50 | + module.hot && |
| 51 | + module.hot.data && |
| 52 | + module.hot.data.reloaded && |
| 53 | + previousValue |
| 54 | + ) { |
| 55 | + currentValue = previousValue; |
| 56 | + } |
| 57 | + if (previousValue !== currentValue || initial) { |
| 58 | + if (!currentValue) { |
| 59 | + if (previousValue) { |
| 60 | + cookies.remove('auth_token', { path: '/' }); |
| 61 | + //START CUSTOMIZATION |
| 62 | + cookies.remove('__ac', { path: '/' }); |
| 63 | + //END CUSTOMIZATION |
| 64 | + } |
| 65 | + } else { |
| 66 | + if (previousValue !== currentValue) { |
| 67 | + cookies.set( |
| 68 | + 'auth_token', |
| 69 | + currentValue, |
| 70 | + getCookieOptions({ |
| 71 | + expires: new Date(jwtDecode(currentValue).exp * 1000), |
| 72 | + }), |
| 73 | + ); |
| 74 | + } |
| 75 | + const exp = |
| 76 | + (jwtDecode(store.getState().userSession.token).exp * 1000 - |
| 77 | + new Date().getTime()) * |
| 78 | + 0.9 || 3600000; |
| 79 | + setTimeout(() => { |
| 80 | + if (store.getState().userSession.token) { |
| 81 | + if ( |
| 82 | + jwtDecode(store.getState().userSession.token).exp * 1000 > |
| 83 | + new Date().getTime() |
| 84 | + ) { |
| 85 | + store.dispatch(loginRenew()); |
| 86 | + } else { |
| 87 | + // Logout |
| 88 | + store.dispatch( |
| 89 | + push( |
| 90 | + `/logout?return_url=${ |
| 91 | + store.getState().router.location.pathname |
| 92 | + }`, |
| 93 | + ), |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | + }, exp); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + store.subscribe(handleChange); |
| 102 | + handleChange(true); |
| 103 | +} |
| 104 | +if (module?.hot) { |
| 105 | + module.hot.dispose((data) => { |
| 106 | + data.reloaded = true; |
| 107 | + }); |
| 108 | +} |
0 commit comments