From 91cbc5ddf04ca86dd35c3229b1cac3f34f25f2d3 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Mon, 16 Feb 2026 13:06:54 +0500 Subject: [PATCH] core: simplify user signup --- packages/core/src/api/user-manager.ts | 88 ++++++++------------------- 1 file changed, 27 insertions(+), 61 deletions(-) diff --git a/packages/core/src/api/user-manager.ts b/packages/core/src/api/user-manager.ts index f4901ac62..4f098eeca 100644 --- a/packages/core/src/api/user-manager.ts +++ b/packages/core/src/api/user-manager.ts @@ -81,13 +81,34 @@ class UserManager { email = email.toLowerCase(); const hashedPassword = await this.db.storage().hash(password, email); - await http.post(`${constants.API_HOST}${ENDPOINTS.signup}`, { - email, - password: hashedPassword, - client_id: "notesnook" + await this.tokenManager.saveToken( + await http.post(`${constants.API_HOST}${ENDPOINTS.signup}`, { + email, + password: hashedPassword, + client_id: "notesnook" + }) + ); + + const user = await this.fetchUser(); + if (!user) return; + + await this.db.storage().deriveCryptoKey({ + password, + salt: user.salt }); - EV.publish(EVENTS.userSignedUp); - return await this._login({ email, password, hashedPassword }); + await this.db.setLastSynced(0); + await this.db.syncer.devices.register(); + + const masterKey = await this.getMasterKey(); + if (!masterKey) throw new Error("User encryption key not generated."); + await this.updateUser({ + dataEncryptionKey: await this.keyManager.wrapKey( + await this.db.crypto().generateRandomKey(), + masterKey + ) + }); + + this.db.eventManager.publish(EVENTS.userLoggedIn, user); } async authenticateEmail(email: string) { @@ -205,61 +226,6 @@ class UserManager { } } - async _login({ - email, - password, - hashedPassword, - code, - method - }: { - email: string; - password: string; - hashedPassword?: string; - code?: string; - method?: string; - }) { - email = email && email.toLowerCase(); - - if (!hashedPassword && password) { - hashedPassword = await this.db.storage().hash(password, email); - } - - await this.tokenManager.saveToken( - await http.post(`${constants.AUTH_HOST}${ENDPOINTS.token}`, { - username: email, - password: hashedPassword, - grant_type: code ? "mfa" : "password", - scope: "notesnook.sync offline_access IdentityServerApi", - client_id: "notesnook", - "mfa:code": code, - "mfa:method": method - }) - ); - - const user = await this.fetchUser(); - if (!user) return; - - await this.db.storage().deriveCryptoKey({ - password, - salt: user.salt - }); - await this.db.setLastSynced(0); - await this.db.syncer.devices.register(); - - // TODO: Uncomment this when we are done testing legacy password change - // support - // const masterKey = await this.getMasterKey(); - // if (!masterKey) throw new Error("User encryption key not generated."); - // await this.updateUser({ - // dataEncryptionKey: await this.keyManager.wrapKey( - // await this.db.crypto().generateRandomKey(), - // masterKey - // ) - // }); - - this.db.eventManager.publish(EVENTS.userLoggedIn, user); - } - async getSessions() { const token = await this.tokenManager.getAccessToken(); if (!token) return;