|
import apiClient from './apiClient'; |
|
|
|
class LinkedInAuthService { |
|
|
|
|
|
|
|
|
|
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' |
|
}; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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' |
|
}; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getLinkedInAccounts() { |
|
try { |
|
if (import.meta.env.VITE_NODE_ENV === 'development') { |
|
console.log('π [LinkedIn] Fetching LinkedIn accounts...'); |
|
} |
|
|
|
const response = await apiClient.get('/accounts'); |
|
|
|
|
|
let accounts = []; |
|
|
|
if (response.data && response.data.success && response.data.accounts) { |
|
|
|
accounts = response.data.accounts; |
|
} else if (Array.isArray(response.data)) { |
|
|
|
accounts = response.data; |
|
} else { |
|
throw new Error('Unexpected response format from API'); |
|
} |
|
|
|
|
|
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' }; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
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' |
|
}; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
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(); |