Skip to content

Commit

Permalink
Merge pull request #280 from Tonomy-Foundation/bug/792-no-login-reque…
Browse files Browse the repository at this point in the history
…st-from-other-account

Bug/792 no login request from other account
  • Loading branch information
theblockstalk authored Aug 11, 2023
2 parents 6c6e2c0 + 4ca9e79 commit 4d16993
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
21 changes: 17 additions & 4 deletions src/sdk/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ export class User {
await this.storage.status;
await this.createDid();

if (getSettings().loggerLevel === 'debug') {
console.log('Created account', {
accountName: await this.storage.accountName,
username: (await this.getUsername()).getBaseUsername(),
did: await this.getDid(),
});
}

return res;
}

Expand Down Expand Up @@ -475,10 +483,15 @@ export class User {

async logout(): Promise<void> {
// remove all keys
await this.keyManager.removeKey({ level: KeyManagerLevel.PASSWORD });
await this.keyManager.removeKey({ level: KeyManagerLevel.PIN });
await this.keyManager.removeKey({ level: KeyManagerLevel.BIOMETRIC });
await this.keyManager.removeKey({ level: KeyManagerLevel.LOCAL });
for (const level of Object.keys(KeyManagerLevel)) {
try {
await this.keyManager.getKey({ level: KeyManagerLevel.from(level) });
this.keyManager.removeKey({ level: KeyManagerLevel.from(level) });
} catch (e) {
if (!(e instanceof SdkError) || e.code !== SdkErrors.KeyNotFound) throw e;
}
}

// clear storage data
this.storage.clear();

Expand Down
30 changes: 22 additions & 8 deletions src/sdk/services/communication/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export class Communication {
private static identifier: 0;
// TODO fix problem: if server restarts, this will be lost and all clients will need to reconnect
private subscribers = new Map<number, Subscriber>();
private authMessage: AuthenticationMessage;
private authMessage?: AuthenticationMessage;
private loggedIn = false;
private url: string;

constructor(singleton = true) {
Expand All @@ -26,8 +27,12 @@ export class Communication {
Communication.singleton = this;
}

isConnected(): boolean {
return this.socketServer && this.socketServer.connected;
}

isLoggedIn(): boolean {
return this.authMessage !== undefined && typeof this.authMessage === 'object';
return this.loggedIn && this.authMessage !== undefined && typeof this.authMessage === 'object';
}

/**
Expand All @@ -37,20 +42,21 @@ export class Communication {
* @throws {SdkError} CommunicationNotConnected
*/
private async connect(): Promise<void> {
if (this.socketServer?.connected) return; // dont override socket if connected
if (this.isConnected()) return;

this.socketServer.connect();

await new Promise((resolve, reject) => {
this.socketServer.on('connect', async () => {
if (this.isLoggedIn()) {
await this.login(this.authMessage);
await this.login(this.authMessage as AuthenticationMessage);
}

resolve(true);
return;
});
setTimeout(() => {
if (this.socketServer.connected) return;
if (this.isConnected()) return;

reject(
createSdkError(
Expand All @@ -74,6 +80,7 @@ export class Communication {
if (getSettings().loggerLevel === 'debug')
console.log(
'emitMessage',
event,
message.getType(),
message.getSender(),
message.getRecipient(),
Expand Down Expand Up @@ -121,7 +128,11 @@ export class Communication {

const result = await this.emitMessage('login', authorization);

if (result) this.authMessage = authorization;
if (result) {
this.loggedIn = true;
this.authMessage = authorization;
}

return result;
}

Expand All @@ -141,7 +152,7 @@ export class Communication {
* function that adds a new subscriber, which is called every time a message is received
*
* @param {Subscriber} subscriber - the message object
* @param {string} [type] - shows itsan optional parameters
* @param {string} [type] - the type of message to subscribe to
* @returns {number} - identifier which will be used for unsubscribe
*/
subscribeMessage(subscriber: Subscriber, type?: string): number {
Expand Down Expand Up @@ -181,7 +192,10 @@ export class Communication {
}

disconnect() {
if (this.socketServer?.connected) {
this.loggedIn = false;
delete this.authMessage;

if (this.isConnected()) {
this.socketServer.disconnect();
}
}
Expand Down

0 comments on commit 4d16993

Please sign in to comment.