wheresvic / mongoose-field-encryption

A simple symmetric encryption plugin for individual fields. Dependency free, only mongoose peer dependency.
MIT License
74 stars 32 forks source link

Object returned from "create" is encrypted #105

Closed sukruavcuoglu closed 1 year ago

sukruavcuoglu commented 1 year ago

Hi,

Thanks for providing this great library.

I have a problem with create operation. Objects returned from "create" operation has encrypted fields.

I have an example like below:

const userModel = getUserModel();
const createdUser = (
  await userModel.create([userDoc], { session: dbConn.session })
)[0];

Returned user is like this:

{
  id: 'some_user_id',
  email: 'XXXXXXXX_encrpyted_field_XXXXXXX',
  firstName: 'XXXXXXXX_encrpyted_field_XXXXXXX,
  lastName: 'XXXXXXXX_encrpyted_field_XXXXXXX',
  createdAt: 2023-07-24T09:02:45.408Z,
  createdBy: undefined,
  updatedAt: 2023-07-24T09:02:45.408Z,
  updatedBy: undefined,
}

Is there any future plan to change the behaviour?

For now, as a workaround, I am using "decryptFieldsSync" function on the object before returning.

Thanks.

wheresvic commented 1 year ago

@sukruavcuoglu From mongoose docs (https://mongoosejs.com/docs/api/model.html#Model.create()) create is a shortcut to make the document and save it so it makes sense that the fields are then encrypted.

As you have already figured it out, you can use decryptSync to then get the original values back.

sukruavcuoglu commented 1 year ago

Thanks for the response.