Skip to content

Commit 68408c9

Browse files
committed
Authentication occurs immediately and synchronously
1 parent d1c239c commit 68408c9

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

apps/pwabuilder-google-play/services/redisService.ts

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -59,59 +59,49 @@ export class RedisService implements DatabaseService {
5959
}
6060

6161
private async initializeConnection(): Promise<void> {
62-
return new Promise(async (resolve, reject) => {
63-
try {
64-
this.redis.on("ready", () => {
65-
console.info("Redis ready to accept commands");
66-
resolve();
67-
});
62+
try {
63+
// Set up error handler before connecting
64+
this.redis.on("error", (err: Error) => {
65+
console.error("Redis connection error:", err);
66+
});
6867

69-
this.redis.on("error", (err: Error) => {
70-
console.error("Redis connection error:", err);
71-
});
68+
this.redis.on("close", () => {
69+
console.warn("Redis connection closed");
70+
});
7271

73-
this.redis.on("connect", async () => {
74-
console.info("Redis connected, authenticating with managed identity...");
75-
try {
76-
// Enable RESP3 protocol (required for Azure AD authentication)
77-
await this.redis.call("HELLO", "3");
72+
// Connect to Redis
73+
await this.redis.connect();
74+
console.info("Redis connected, authenticating with managed identity...");
7875

79-
// Get token and authenticate
80-
const token = await this.credential.getToken("https://redis.azure.com/.default");
81-
await this.redis.call("AUTH", "default", token.token);
82-
console.info("Redis authenticated successfully with managed identity");
76+
// Authenticate immediately after connection, before any other commands
77+
await this.redis.call("HELLO", "3");
78+
const token = await this.credential.getToken("https://redis.azure.com/.default");
79+
await this.redis.call("AUTH", "default", token.token);
80+
console.info("Redis authenticated successfully with managed identity");
8381

84-
// Manually emit ready event after successful authentication
85-
this.redis.emit("ready");
86-
} catch (authError) {
87-
console.error("Redis authentication failed:", authError);
88-
reject(authError);
89-
}
90-
});
91-
92-
this.redis.on("close", () => {
93-
console.warn("Redis connection closed");
94-
});
82+
// Set up reconnection handler to re-authenticate
83+
this.redis.on("reconnecting", () => {
84+
console.info("Redis reconnecting...");
85+
});
9586

96-
this.redis.on("reconnecting", async () => {
97-
console.info("Redis reconnecting...");
98-
// Re-authenticate on reconnection
87+
// Re-authenticate after reconnection
88+
this.redis.on("connect", async () => {
89+
if (this.redis.status === "reconnecting" || this.redis.status === "connecting") {
90+
console.info("Re-authenticating after reconnection...");
9991
try {
10092
await this.redis.call("HELLO", "3");
10193
const token = await this.credential.getToken("https://redis.azure.com/.default");
10294
await this.redis.call("AUTH", "default", token.token);
95+
console.info("Re-authentication successful");
10396
} catch (authError) {
10497
console.error("Redis re-authentication failed:", authError);
10598
}
106-
});
107-
108-
// Connect to Redis
109-
await this.redis.connect();
110-
} catch (error) {
111-
console.error("Failed to initialize Redis connection:", error);
112-
reject(error);
113-
}
114-
});
99+
}
100+
});
101+
} catch (error) {
102+
console.error("Failed to initialize Redis connection:", error);
103+
throw error;
104+
}
115105
}
116106

117107
/**

0 commit comments

Comments
 (0)