diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 8e283375b..56dbd88c9 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -70,7 +70,7 @@ "changePassword": "Change Password", "changePasswordInvalid": "The old password you entered is incorrect", "changePasswordTitle": "Password Changed", - "changePasswordToastDescription": "Your password has been changed. Please keep it safe.", + "changePasswordToastDescription": "Your password has been changed. Please keep it safe and restart Session.", "chooseAnAction": "Choose an action to start a conversation", "classicDarkThemeTitle": "Classic Dark", "classicLightThemeTitle": "Classic Light", @@ -440,7 +440,7 @@ "removePassword": "Remove Password", "removePasswordInvalid": "Incorrect password", "removePasswordTitle": "Password Removed", - "removePasswordToastDescription": "Your password has been removed.", + "removePasswordToastDescription": "Your password has been removed. Please restart Session.", "removeResidueMembers": "Clicking ok will also remove those members as they left the group.", "replyingToMessage": "Replying to:", "replyToMessage": "Reply", @@ -477,7 +477,7 @@ "setPasswordFail": "Failed to set password", "setPasswordInvalid": "Passwords do not match", "setPasswordTitle": "Password Set", - "setPasswordToastDescription": "Your password has been set. Please keep it safe.", + "setPasswordToastDescription": "Your password has been set. Please keep it safe and restart Session", "settingAppliesToEveryone": "This setting applies to everyone in this conversation.", "settingAppliesToYourMessages": "This setting applies to messages you send in this conversation.", "settingsHeader": "Settings", diff --git a/ts/components/dialog/SessionSetPasswordDialog.tsx b/ts/components/dialog/SessionSetPasswordDialog.tsx index f9d88c551..6dd658db0 100644 --- a/ts/components/dialog/SessionSetPasswordDialog.tsx +++ b/ts/components/dialog/SessionSetPasswordDialog.tsx @@ -2,13 +2,13 @@ import autoBind from 'auto-bind'; import { Component } from 'react'; -import { Data } from '../../data/data'; import { ToastUtils } from '../../session/utils'; import { sessionPassword } from '../../state/ducks/modalDialog'; import { LocalizerKeys } from '../../types/LocalizerKeys'; import type { PasswordAction } from '../../types/ReduxTypes'; import { assertUnreachable } from '../../types/sqlSharedTypes'; import { matchesHash, validatePassword } from '../../util/passwordUtils'; +import { getPasswordHash } from '../../util/storage'; import { SessionWrapperModal } from '../SessionWrapperModal'; import { SessionButton, SessionButtonColor, SessionButtonType } from '../basic/SessionButton'; import { SpacerSM } from '../basic/Text'; @@ -146,9 +146,9 @@ export class SessionSetPasswordDialog extends Component { ); } - public async validatePasswordHash(password: string | null) { + public validatePasswordHash(password: string | null) { // Check if the password matches the hash we have stored - const hash = await Data.getPasswordHash(); + const hash = getPasswordHash(); if (hash && !matchesHash(password, hash)) { return false; } @@ -224,7 +224,7 @@ export class SessionSetPasswordDialog extends Component { return; } - const isValidWithStoredInDB = Boolean(await this.validatePasswordHash(oldPassword)); + const isValidWithStoredInDB = this.validatePasswordHash(oldPassword); if (!isValidWithStoredInDB) { this.setState({ error: window.i18n('changePasswordInvalid'), @@ -246,7 +246,7 @@ export class SessionSetPasswordDialog extends Component { private async handleActionRemove(oldPassword: string) { // We don't validate oldPassword on change: this is validate on the validatePasswordHash below - const isValidWithStoredInDB = Boolean(await this.validatePasswordHash(oldPassword)); + const isValidWithStoredInDB = this.validatePasswordHash(oldPassword); if (!isValidWithStoredInDB) { this.setState({ error: window.i18n('removePasswordInvalid'), @@ -279,7 +279,7 @@ export class SessionSetPasswordDialog extends Component { return; } - const isValidWithStoredInDB = Boolean(await this.validatePasswordHash(enteredPassword)); + const isValidWithStoredInDB = this.validatePasswordHash(enteredPassword); if (!isValidWithStoredInDB) { this.setState({ error: window.i18n('invalidPassword'), diff --git a/ts/components/settings/SessionSettings.tsx b/ts/components/settings/SessionSettings.tsx index 90829c57a..86f913b2a 100644 --- a/ts/components/settings/SessionSettings.tsx +++ b/ts/components/settings/SessionSettings.tsx @@ -10,10 +10,10 @@ import { SessionIconButton } from '../icon'; import { SessionNotificationGroupSettings } from './SessionNotificationGroupSettings'; -import { Data } from '../../data/data'; import { sessionPassword } from '../../state/ducks/modalDialog'; import { SectionType, showLeftPaneSection } from '../../state/ducks/section'; import type { PasswordAction, SessionSettingCategory } from '../../types/ReduxTypes'; +import { getPasswordHash } from '../../util/storage'; import { SettingsCategoryAppearance } from './section/CategoryAppearance'; import { CategoryConversations } from './section/CategoryConversations'; import { SettingsCategoryHelp } from './section/CategoryHelp'; @@ -148,16 +148,8 @@ export const SessionSettingsView = (props: SettingsViewProps) => { const [hasPassword, setHasPassword] = useState(true); useMount(() => { - let isMounted = true; - // eslint-disable-next-line more/no-then - void Data.getPasswordHash().then(hash => { - if (isMounted) { - setHasPassword(!!hash); - } - }); - return () => { - isMounted = false; - }; + const hash = getPasswordHash(); + setHasPassword(!!hash); }); function onPasswordUpdated(action: string) { diff --git a/ts/hooks/usePasswordModal.ts b/ts/hooks/usePasswordModal.ts index afe85e4c9..3fd24f5d9 100644 --- a/ts/hooks/usePasswordModal.ts +++ b/ts/hooks/usePasswordModal.ts @@ -2,8 +2,8 @@ import { isEmpty } from 'lodash'; import { useState } from 'react'; import { useDispatch } from 'react-redux'; import useMount from 'react-use/lib/useMount'; -import { Data } from '../data/data'; import { updateEnterPasswordModal } from '../state/ducks/modalDialog'; +import { getPasswordHash } from '../util/storage'; /** * Password protection for a component if a password has been set @@ -27,15 +27,15 @@ export function usePasswordModal({ const dispatch = useDispatch(); - const validateAccess = async () => { + const validateAccess = () => { if (!isEmpty(passwordHash)) { return; } - const hash = await Data.getPasswordHash(); + const hash = getPasswordHash(); setHasPassword(!!hash); - if (hash && !isEmpty(hash)) { + if (hash) { setPasswordHash(hash); dispatch( updateEnterPasswordModal({ @@ -63,7 +63,7 @@ export function usePasswordModal({ }; useMount(() => { - void validateAccess(); + validateAccess(); }); return { hasPassword, passwordValid }; diff --git a/ts/mains/main_node.ts b/ts/mains/main_node.ts index 04174f986..31932843b 100644 --- a/ts/mains/main_node.ts +++ b/ts/mains/main_node.ts @@ -1059,7 +1059,7 @@ ipc.on('set-password', async (event, passPhrase, oldPhrase) => { return; } - if (_.isEmpty(passPhrase)) { + if (isEmpty(passPhrase)) { const defaultKey = getDefaultSQLKey(); sqlNode.setSQLPassword(defaultKey); sqlNode.removePasswordHash(); @@ -1070,7 +1070,6 @@ ipc.on('set-password', async (event, passPhrase, oldPhrase) => { sqlNode.savePasswordHash(newHash); userConfig.set('dbHasPassword', true); } - sendResponse(undefined); } catch (e) { const localisedError = locale.messages.setPasswordFail; diff --git a/ts/util/storage.ts b/ts/util/storage.ts index cf87cf422..6188d0618 100644 --- a/ts/util/storage.ts +++ b/ts/util/storage.ts @@ -93,6 +93,14 @@ function reset() { items = Object.create(null); } +function getBoolOrFalse(settingsKey: string): boolean { + const got = Storage.get(settingsKey, false); + if (isBoolean(got)) { + return got; + } + return false; +} + export async function setLocalPubKey(pubkey: string) { await put('number_id', `${pubkey}.1`); } @@ -159,12 +167,8 @@ export async function saveRecentReations(reactions: Array) { return Storage.put('recent_reactions', reactions.join(' ')); } -function getBoolOrFalse(settingsKey: string): boolean { - const got = Storage.get(settingsKey, false); - if (isBoolean(got)) { - return got; - } - return false; +export function getPasswordHash() { + return Storage.get('passHash') as string; } export const Storage = { fetch, put, get, getBoolOrFalse, remove, onready, reset };