File size: 5,492 Bytes
25f22bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import apiClient from './apiClient';
class LinkedInAuthService {
/**
* Initiate LinkedIn OAuth flow
* @returns {Promise} Promise that resolves to authorization URL and state
*/
async initiateAuth() {
try {
const response = await apiClient.post('/accounts', {
account_name: 'LinkedIn Account',
social_network: 'LinkedIn'
});
if (response.data.success && response.data.authorization_url) {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('π [LinkedIn] Auth initiated successfully');
}
return {
success: true,
authorization_url: response.data.authorization_url,
state: response.data.state
};
} else {
throw new Error(response.data.message || 'Failed to initiate LinkedIn authentication');
}
} catch (error) {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.error('π [LinkedIn] Auth initiation error:', error.response?.data || error.message);
}
return {
success: false,
message: error.response?.data?.message || 'Failed to start LinkedIn authentication'
};
}
}
/**
* Handle LinkedIn OAuth callback
* @param {string} code - Authorization code from LinkedIn
* @param {string} state - State parameter for security verification
* @returns {Promise} Promise that resolves to callback result
*/
async handleCallback(code, state) {
try {
const response = await apiClient.post('/accounts/callback', {
code: code,
state: state,
social_network: 'LinkedIn'
});
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('π [LinkedIn] OAuth callback handled successfully');
}
return response.data;
} catch (error) {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.error('π [LinkedIn] OAuth callback error:', error.response?.data || error.message);
}
return {
success: false,
message: error.response?.data?.message || 'Failed to complete LinkedIn authentication'
};
}
}
/**
* Get all LinkedIn accounts for the current user
* @returns {Promise} Promise that resolves to accounts data
*/
async getLinkedInAccounts() {
try {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('π [LinkedIn] Fetching LinkedIn accounts...');
}
const response = await apiClient.get('/accounts');
// Handle different response formats
let accounts = [];
if (response.data && response.data.success && response.data.accounts) {
// Format: { success: true, accounts: [...] }
accounts = response.data.accounts;
} else if (Array.isArray(response.data)) {
// Format: [...]
accounts = response.data;
} else {
throw new Error('Unexpected response format from API');
}
// Filter for LinkedIn accounts only
const linkedinAccounts = accounts.filter(
account => account && account.social_network && account.social_network.toLowerCase() === 'linkedin'
);
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('π [LinkedIn] Retrieved LinkedIn accounts:', linkedinAccounts);
}
return { success: true, accounts: linkedinAccounts };
} catch (error) {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.error('π [LinkedIn] Get LinkedIn accounts error:', error.response?.data || error.message);
}
return { success: false, accounts: [], message: error.response?.data?.message || 'Failed to fetch LinkedIn accounts' };
}
}
/**
* Delete a LinkedIn account
* @param {string} accountId - Account ID to delete
* @returns {Promise} Promise that resolves to deletion result
*/
async deleteLinkedInAccount(accountId) {
try {
const response = await apiClient.delete(`/accounts/${accountId}`);
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('π [LinkedIn] LinkedIn account deleted successfully');
}
return response.data;
} catch (error) {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.error('π [LinkedIn] Delete LinkedIn account error:', error.response?.data || error.message);
}
return {
success: false,
message: error.response?.data?.message || 'Failed to delete LinkedIn account'
};
}
}
/**
* Set a LinkedIn account as primary
* @param {string} accountId - Account ID to set as primary
* @returns {Promise} Promise that resolves to update result
*/
async setPrimaryAccount(accountId) {
try {
const response = await apiClient.put(`/accounts/${accountId}/primary`);
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.log('π [LinkedIn] Primary account set successfully');
}
return response.data;
} catch (error) {
if (import.meta.env.VITE_NODE_ENV === 'development') {
console.error('π [LinkedIn] Set primary account error:', error.response?.data || error.message);
}
return {
success: false,
message: error.response?.data?.message || 'Failed to set primary account'
};
}
}
}
export default new LinkedInAuthService(); |