Skip to content

Commit 196455a

Browse files
committed
add hooks for user status (email verification)
1 parent 742d926 commit 196455a

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

packages/common-ui/src/components/auth/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export { default as ResetPassword } from './ResetPassword.vue';
1313
export {
1414
sendVerificationEmail,
1515
verifyEmail,
16+
getUserStatus,
1617
requestPasswordReset,
1718
resetPassword,
1819
type AuthResponse,
1920
type VerifyEmailResponse,
21+
type UserStatusResponse,
2022
} from '../../services/authAPI';

packages/common-ui/src/stores/useAuthStore.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface AuthState {
1414
onLoadComplete: boolean;
1515
}
1616

17-
export async function getCurrentUser(): Promise<UserDBInterface> {
17+
export async function getCurrentUser(): Promise<UserDBInterface | undefined> {
1818
const store = useAuthStore();
1919

2020
if (!store.onLoadComplete) {
@@ -58,12 +58,16 @@ export const useAuthStore = () => {
5858
try {
5959
this._user = getDataLayer().getUserDB();
6060

61-
this.loginAndRegistration.loggedIn = this._user.isLoggedIn();
61+
this.loginAndRegistration.loggedIn = this._user ? this._user.isLoggedIn() : false;
6262

6363
this.onLoadComplete = true;
6464
this.loginAndRegistration.init = true;
6565
} catch (e) {
6666
console.error('Failed to initialize auth store:', e);
67+
// Set onLoadComplete even on error to prevent timeout
68+
this.loginAndRegistration.loggedIn = false;
69+
this.onLoadComplete = true;
70+
this.loginAndRegistration.init = true;
6771
}
6872
},
6973

packages/common-ui/src/stores/useConfigStore.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,46 @@ export const useConfigStore = () => {
2828
async updateDarkMode(darkMode: boolean) {
2929
this.config.darkMode = darkMode;
3030
const user = await getCurrentUser();
31-
await user.setConfig({
32-
darkMode,
33-
});
31+
if (user) {
32+
await user.setConfig({
33+
darkMode,
34+
});
35+
}
3436
},
3537

3638
async updateLikesConfetti(likesConfetti: boolean) {
3739
this.config.likesConfetti = likesConfetti;
3840
const user = await getCurrentUser();
39-
await user.setConfig({
40-
likesConfetti,
41-
});
41+
if (user) {
42+
await user.setConfig({
43+
likesConfetti,
44+
});
45+
}
4246
},
4347

4448
async updateSessionTimeLimit(sessionTimeLimit: number) {
4549
this.config.sessionTimeLimit = sessionTimeLimit;
4650
const user = await getCurrentUser();
47-
await user.setConfig({
48-
sessionTimeLimit,
49-
});
51+
if (user) {
52+
await user.setConfig({
53+
sessionTimeLimit,
54+
});
55+
}
5056
},
5157

5258
async hydrate() {
53-
const user = await getCurrentUser();
54-
const cfg = await user.getConfig();
55-
console.log(`user config: ${JSON.stringify(cfg)}`);
56-
this.updateConfig(cfg);
59+
try {
60+
const user = await getCurrentUser();
61+
if (user) {
62+
const cfg = await user.getConfig();
63+
console.log(`user config: ${JSON.stringify(cfg)}`);
64+
this.updateConfig(cfg);
65+
} else {
66+
console.log('No user logged in, using default config');
67+
}
68+
} catch (e) {
69+
console.warn('Failed to hydrate config store, using defaults:', e);
70+
}
5771
},
5872
async init() {
5973
await this.hydrate();

packages/db/src/impl/common/BaseUserDB.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,15 @@ export function accomodateGuest(): {
10441044
} {
10451045
logger.log('[funnel] accomodateGuest() called');
10461046

1047+
// Check if localStorage is available (browser environment)
1048+
if (typeof localStorage === 'undefined') {
1049+
logger.log('[funnel] localStorage not available (Node.js environment), returning default guest');
1050+
return {
1051+
username: GuestUsername + 'nodejs-test',
1052+
firstVisit: true,
1053+
};
1054+
}
1055+
10471056
const dbUUID = 'sk-guest-uuid';
10481057
let firstVisit: boolean;
10491058

packages/express/src/routes/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ router.get('/status', (req: Request, res: Response) => {
173173
res.json({
174174
ok: true,
175175
username: userDoc.name,
176-
status: userDoc.status || 'verified',
176+
status: userDoc.status || 'pending_verification', // Default to pending if not explicitly set
177177
email: userDoc.email || null,
178178
});
179179
} catch (error) {

0 commit comments

Comments
 (0)