1
1
// src/lib/auth.ts
2
2
import { IAgentRuntime , elizaLogger } from "@elizaos/core" ;
3
- import { IgLoginTwoFactorRequiredError } from ' instagram-private-api' ;
3
+ import { IgLoginTwoFactorRequiredError } from " instagram-private-api" ;
4
4
import { InstagramConfig } from "../environment" ;
5
5
import { InstagramState } from "../types" ;
6
6
import { fetchProfile } from "./profile" ;
@@ -10,96 +10,94 @@ import { createInitialState, getIgClient } from "./state";
10
10
* Authenticates with Instagram
11
11
*/
12
12
async function authenticate (
13
- runtime : IAgentRuntime ,
14
- config : InstagramConfig
13
+ runtime : IAgentRuntime ,
14
+ config : InstagramConfig
15
15
) : Promise < InstagramState > {
16
- const ig = getIgClient ( ) ;
17
- let state = createInitialState ( ) ;
16
+ const ig = getIgClient ( ) ;
17
+ const state = createInitialState ( ) ;
18
18
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
40
19
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
+ }
45
41
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
+ ) ;
49
48
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 ) ;
51
52
52
- return {
53
- ...state ,
54
- isInitialized : true ,
55
- profile
56
- } ;
53
+ const profile = await fetchProfile ( runtime , config ) ;
57
54
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
+ }
58
67
} 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 ;
64
70
}
65
-
66
- } catch ( error ) {
67
- elizaLogger . error ( 'Authentication failed:' , error ) ;
68
- throw error ;
69
- }
70
71
}
71
72
72
73
/**
73
74
* Sets up webhooks for real-time updates if needed
74
75
*/
75
76
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
78
79
}
79
80
80
81
/**
81
82
* Initializes the Instagram client
82
83
*/
83
84
export async function initializeClient (
84
- runtime : IAgentRuntime ,
85
- config : InstagramConfig
85
+ runtime : IAgentRuntime ,
86
+ config : InstagramConfig
86
87
) : 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 ) ;
90
91
91
- // Set up webhook handlers if needed
92
- await setupWebhooks ( ) ;
92
+ // Set up webhook handlers if needed
93
+ await setupWebhooks ( ) ;
93
94
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
+ }
99
100
}
100
101
101
102
// Export other authentication related functions if needed
102
- export {
103
- authenticate ,
104
- setupWebhooks
105
- } ;
103
+ export { authenticate , setupWebhooks } ;
0 commit comments