Skip to content

Commit 00f8a9b

Browse files
authored
Merge pull request #1 from typelets/fix/master-password-prompt
LGTM
2 parents 4979640 + e006899 commit 00f8a9b

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/components/layout/MainLayout.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default function MainLayout() {
4343
setCurrentView,
4444
setSearchQuery,
4545
refetch,
46+
reinitialize,
4647
} = useNotes();
4748

4849
const handleCreateNote = useCallback((templateContent?: { title: string; content: string }) => {
@@ -90,6 +91,12 @@ export default function MainLayout() {
9091
}, 1500);
9192
}, [setSelectedNote]);
9293

94+
const handleMasterPasswordUnlock = useCallback(() => {
95+
handleUnlockSuccess();
96+
// Force re-initialize and fetch data after successful unlock/setup
97+
void reinitialize();
98+
}, [handleUnlockSuccess, reinitialize]);
99+
93100
const folderPanelProps: FolderPanelProps = {
94101
currentView, folders, selectedFolder, searchQuery, notesCount, starredCount, archivedCount, trashedCount, expandedFolders,
95102
onUpdateFolder: (id, name, color) => updateFolder(id, { name, color }),
@@ -144,7 +151,7 @@ export default function MainLayout() {
144151
<p className="text-gray-600">Your notes are protected with end-to-end encryption</p>
145152
</div>
146153
</div>
147-
<MasterPasswordDialog userId={userId} onSuccess={handleUnlockSuccess} />
154+
<MasterPasswordDialog userId={userId} onSuccess={handleMasterPasswordUnlock} />
148155
</>
149156
);
150157
}

src/hooks/useMasterPassword.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export function useMasterPassword() {
1717
const hasPassword = hasMasterPassword(user.id);
1818
const isUnlocked = isMasterPasswordUnlocked(user.id);
1919

20-
// Needs unlock if has password but not unlocked
21-
setNeedsUnlock(hasPassword && !isUnlocked);
20+
// Needs unlock if:
21+
// 1. User has password but not unlocked (returning user)
22+
// 2. User doesn't have password at all (new user needs to set one up)
23+
setNeedsUnlock(!hasPassword || (hasPassword && !isUnlocked));
2224
setIsChecking(false);
2325
};
2426

src/hooks/useNotes.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState, useMemo, useEffect, useCallback, useRef } from 'react';
22
import { useAuth, useUser } from '@clerk/clerk-react';
33
import { api, type ApiNote, type ApiFolder } from '@/lib/api/api.ts';
44
import { fileService } from '@/services/fileService';
5-
import { clearEncryptionKeys } from '@/lib/encryption';
5+
import { clearEncryptionKeys, hasMasterPassword, isMasterPasswordUnlocked } from '@/lib/encryption';
66
import type { Note, Folder, ViewMode } from '@/types/note';
77
import { getDescendantIds } from '@/utils/folderTree';
88

@@ -39,7 +39,15 @@ export function useNotes() {
3939
try {
4040
setError(null);
4141
api.setCurrentUser(clerkUser!.id);
42-
await api.getCurrentUser();
42+
43+
// Only call API if user has a master password set up and unlocked
44+
const hasPassword = hasMasterPassword(clerkUser!.id);
45+
const isUnlocked = isMasterPasswordUnlocked(clerkUser!.id);
46+
47+
if (hasPassword && isUnlocked) {
48+
await api.getCurrentUser();
49+
}
50+
4351
setEncryptionReady(true);
4452
} catch (error) {
4553
console.error('Failed to initialize user:', error);
@@ -110,7 +118,13 @@ export function useNotes() {
110118

111119
useEffect(() => {
112120
if (encryptionReady && clerkUser) {
113-
void loadData();
121+
const hasPassword = hasMasterPassword(clerkUser.id);
122+
const isUnlocked = isMasterPasswordUnlocked(clerkUser.id);
123+
124+
// Only load data if user has master password set up and unlocked
125+
if (hasPassword && isUnlocked) {
126+
void loadData();
127+
}
114128
}
115129
}, [encryptionReady, clerkUser, loadData]);
116130

@@ -541,5 +555,11 @@ export function useNotes() {
541555
setSearchQuery,
542556
setFolders,
543557
refetch: loadData,
558+
reinitialize: async () => {
559+
if (clerkUser) {
560+
await initializeUser();
561+
await loadData();
562+
}
563+
},
544564
};
545565
}

0 commit comments

Comments
 (0)