Skip to content

Commit 4f5c05d

Browse files
committedJan 16, 2025
fix: remove unused error var
1 parent bf29973 commit 4f5c05d

File tree

1 file changed

+65
-67
lines changed
  • packages/client-instagram/src/lib

1 file changed

+65
-67
lines changed
 
+65-67
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/lib/auth.ts
22
import { IAgentRuntime, elizaLogger } from "@elizaos/core";
3-
import { IgLoginTwoFactorRequiredError } from 'instagram-private-api';
3+
import { IgLoginTwoFactorRequiredError } from "instagram-private-api";
44
import { InstagramConfig } from "../environment";
55
import { InstagramState } from "../types";
66
import { fetchProfile } from "./profile";
@@ -10,96 +10,94 @@ import { createInitialState, getIgClient } from "./state";
1010
* Authenticates with Instagram
1111
*/
1212
async function authenticate(
13-
runtime: IAgentRuntime,
14-
config: InstagramConfig
13+
runtime: IAgentRuntime,
14+
config: InstagramConfig
1515
): Promise<InstagramState> {
16-
const ig = getIgClient();
17-
const state = createInitialState();
16+
const ig = getIgClient();
17+
const state = createInitialState();
1818

19-
try {
20-
// Generate device ID
21-
ig.state.generateDevice(config.INSTAGRAM_USERNAME);
22-
23-
// Attempt to load cached session
24-
const cachedSession = await runtime.cacheManager.get('instagram/session');
25-
if (cachedSession) {
26-
try {
27-
await ig.state.deserialize(cachedSession);
28-
const profile = await fetchProfile(runtime, config);
29-
return {
30-
...state,
31-
isInitialized: true,
32-
profile
33-
};
34-
} catch (error) {
35-
elizaLogger.warn('Cached session invalid, proceeding with fresh login');
36-
}
37-
}
38-
39-
// Proceed with fresh login
4019
try {
41-
await ig.account.login(
42-
config.INSTAGRAM_USERNAME,
43-
config.INSTAGRAM_PASSWORD
44-
);
20+
// Generate device ID
21+
ig.state.generateDevice(config.INSTAGRAM_USERNAME);
22+
23+
// Attempt to load cached session
24+
const cachedSession =
25+
await runtime.cacheManager.get("instagram/session");
26+
if (cachedSession) {
27+
try {
28+
await ig.state.deserialize(cachedSession);
29+
const profile = await fetchProfile(runtime, config);
30+
return {
31+
...state,
32+
isInitialized: true,
33+
profile,
34+
};
35+
} catch {
36+
elizaLogger.warn(
37+
`Cached session invalid, proceeding with fresh login`
38+
);
39+
}
40+
}
4541

46-
// Cache the session
47-
const serialized = await ig.state.serialize();
48-
await runtime.cacheManager.set('instagram/session', serialized);
42+
// Proceed with fresh login
43+
try {
44+
await ig.account.login(
45+
config.INSTAGRAM_USERNAME,
46+
config.INSTAGRAM_PASSWORD
47+
);
4948

50-
const profile = await fetchProfile(runtime, config);
49+
// Cache the session
50+
const serialized = await ig.state.serialize();
51+
await runtime.cacheManager.set("instagram/session", serialized);
5152

52-
return {
53-
...state,
54-
isInitialized: true,
55-
profile
56-
};
53+
const profile = await fetchProfile(runtime, config);
5754

55+
return {
56+
...state,
57+
isInitialized: true,
58+
profile,
59+
};
60+
} catch (error) {
61+
if (error instanceof IgLoginTwoFactorRequiredError) {
62+
// Handle 2FA if needed - would need to implement 2FA code generation
63+
throw new Error("2FA authentication not yet implemented");
64+
}
65+
throw error;
66+
}
5867
} catch (error) {
59-
if (error instanceof IgLoginTwoFactorRequiredError) {
60-
// Handle 2FA if needed - would need to implement 2FA code generation
61-
throw new Error('2FA authentication not yet implemented');
62-
}
63-
throw error;
68+
elizaLogger.error("Authentication failed:", error);
69+
throw error;
6470
}
65-
66-
} catch (error) {
67-
elizaLogger.error('Authentication failed:', error);
68-
throw error;
69-
}
7071
}
7172

7273
/**
7374
* Sets up webhooks for real-time updates if needed
7475
*/
7576
async function setupWebhooks() {
76-
// Implement webhook setup
77-
// This is a placeholder for future implementation
77+
// Implement webhook setup
78+
// This is a placeholder for future implementation
7879
}
7980

8081
/**
8182
* Initializes the Instagram client
8283
*/
8384
export async function initializeClient(
84-
runtime: IAgentRuntime,
85-
config: InstagramConfig
85+
runtime: IAgentRuntime,
86+
config: InstagramConfig
8687
): Promise<InstagramState> {
87-
try {
88-
// Authenticate and get initial state
89-
const state = await authenticate(runtime, config);
88+
try {
89+
// Authenticate and get initial state
90+
const state = await authenticate(runtime, config);
9091

91-
// Set up webhook handlers if needed
92-
await setupWebhooks();
92+
// Set up webhook handlers if needed
93+
await setupWebhooks();
9394

94-
return state;
95-
} catch (error) {
96-
elizaLogger.error('Failed to initialize Instagram client:', error);
97-
throw error;
98-
}
95+
return state;
96+
} catch (error) {
97+
elizaLogger.error("Failed to initialize Instagram client:", error);
98+
throw error;
99+
}
99100
}
100101

101102
// Export other authentication related functions if needed
102-
export {
103-
authenticate,
104-
setupWebhooks
105-
};
103+
export { authenticate, setupWebhooks };

0 commit comments

Comments
 (0)
Please sign in to comment.