Browse Source

fix: invalidate token if admin email or password changed

- Invalidate old token if admin email changed in env
- Invalidate token if password updated in env
- Avoid unnecessary update if both email and passwords are same

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/2393/head
Pranav C 2 years ago
parent
commit
bcd3c9ee50
  1. 19
      packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts

19
packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts

@ -169,7 +169,8 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) {
salt, salt,
email, email,
password, password,
email_verification_token email_verification_token,
token_version: null
}, },
ncMeta ncMeta
); );
@ -181,24 +182,34 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) {
salt, salt,
email, email,
password, password,
email_verification_token email_verification_token,
token_version: null
}, },
ncMeta ncMeta
); );
} }
} else { } else {
// if email's are not different update the password and hash const newPasswordHash = await promisify(bcrypt.hash)(
process.env.NC_ADMIN_PASSWORD,
superUser.hash
);
if (newPasswordHash !== superUser.password) {
// if email's are same and passwords are different
// then update the password and token version
await User.update( await User.update(
superUser.id, superUser.id,
{ {
salt, salt,
password, password,
email_verification_token email_verification_token,
token_version: null
}, },
ncMeta ncMeta
); );
} }
} }
}
await ncMeta.commit(); await ncMeta.commit();
} catch (e) { } catch (e) {
console.log('Error occurred while updating/creating admin user'); console.log('Error occurred while updating/creating admin user');

Loading…
Cancel
Save