brix / crypto-js

JavaScript library of crypto standards.
Other
15.88k stars 2.39k forks source link

'TypeError: Cannot read properties of undefined (reading 'words')' error occuring while refreshing the application. #441

Open vijay310301 opened 1 year ago

vijay310301 commented 1 year ago

Hi everyone!

I have implemented the crypto-Js in my application for local storage encryption and decryption. It works fine.

Problem: : While refreshing, the page goes black and the console throws the error below:

TypeError: Cannot read properties of undefined (reading 'words')
    at Object._doReset (aes.js:103:1)
    at Object.reset (cipher-core.js:119:1)
    at Object.reset (cipher-core.js:461:1)
    at Object.init (cipher-core.js:104:1)
    at subtype.init (core.js:149:1)
    at subtype.init (core.js:149:1)
    at subtype.init (core.js:149:1)
    at Object.create (core.js:176:1)
    at Object.createDecryptor (cipher-core.js:81:1)
    at Object.decrypt (cipher-core.js:728:1)

NOTE : If i clear the local storage and try agian it works.But whenever i refresh the page this error occures and page goes blank.

I would like to show you the encryption and decryption function that i have used as below:

................... imports .............
import * as CryptoJs from 'crypto-js';

let key: string;

const SECURE_DATA = {
  encrypt: (state: string) => CryptoJs.AES.encrypt(state, key).toString(),
  decrypt: (state: string) => CryptoJs.AES.decrypt(state, key).toString(CryptoJs.enc.Utf8),
};

const STORE_KEYS_TO_PERSIST = [
  { auth: SECURE_DATA },
 ...

];

export interface StoreState {
  auth: User;
....

}

export const reducers: ActionReducerMap<StoreState> = {
  auth: authReducer,
....
};

export function localStorageSyncReducer(
  reducer: ActionReducer<StoreState>
): ActionReducer<StoreState> {
  return localStorageSync({
    keys: STORE_KEYS_TO_PERSIST,
    rehydrate: true,
    syncCondition(state) {
      key = state.auth.token;
      return true;
    },
  })(reducer);
}

export function clearState(reducer) {
  return function (state, action) {
    if (action.type === ActionTypes.LOG_OUT) {
      state = undefined;
    }
    return reducer(state, action);
  };
}

export const metaReducers: Array<MetaReducer<StoreState, Action>> = [
  localStorageSyncReducer,
  clearState,
];
banahaker commented 1 year ago

I have the same problem.

qq378488249 commented 11 months ago

I have the same problem. Please help me

sakethtekumal commented 4 months ago

I had the same issue, this happens when your secretKey is undefined or null, please check your key