fix: move Ozonetel agent login to sidecar auth flow — frontend only handles SIP, sidecar handles REST login

This commit is contained in:
2026-03-18 07:43:29 +05:30
parent 4193b7545a
commit dfc4a0cd44

View File

@@ -1,8 +1,6 @@
import { createContext, useContext, useEffect, useRef, useState, type PropsWithChildren } from 'react';
import { useSipPhone } from '@/hooks/use-sip-phone';
const SIDECAR_URL = import.meta.env.VITE_SIDECAR_URL ?? 'http://localhost:4100';
type SipContextType = ReturnType<typeof useSipPhone> & {
ozonetelStatus: 'idle' | 'logging-in' | 'logged-in' | 'error';
ozonetelError: string | null;
@@ -14,84 +12,19 @@ export const SipProvider = ({ children }: PropsWithChildren) => {
const sipPhone = useSipPhone();
const hasConnected = useRef(false);
const [ozonetelStatus, setOzonetelStatus] = useState<'idle' | 'logging-in' | 'logged-in' | 'error'>('idle');
const [ozonetelError, setOzonetelError] = useState<string | null>(null);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [ozonetelError, _setOzonetelError] = useState<string | null>(null);
// Auto-connect SIP + login to Ozonetel on mount
// Auto-connect SIP on mount — only WebSocket/SIP registration
// Ozonetel agent login is handled by the sidecar during auth, NOT here
useEffect(() => {
if (!hasConnected.current) {
hasConnected.current = true;
// 1. Connect SIP (WebSocket → Ozonetel SIP server)
sipPhone.connect();
// 2. Login agent to Ozonetel's routing system
// Try sidecar first, fallback to direct Ozonetel API call
const sipId = (import.meta.env.VITE_SIP_URI ?? '').replace('sip:', '').split('@')[0];
const agentId = import.meta.env.VITE_OZONETEL_AGENT_ID ?? 'Agent3';
const agentPassword = import.meta.env.VITE_OZONETEL_AGENT_PASSWORD ?? 'Test123$';
const accountId = import.meta.env.VITE_OZONETEL_ACCOUNT_ID ?? 'global_demo';
const apiKey = import.meta.env.VITE_OZONETEL_API_KEY ?? '';
if (sipId) {
setOzonetelStatus('logging-in');
const loginViaOzonetelDirect = async () => {
const params = new URLSearchParams({
userName: accountId,
apiKey: apiKey,
phoneNumber: sipId,
action: 'login',
mode: 'blended',
state: 'Ready',
});
const response = await fetch(
`https://in1-ccaas-api.ozonetel.com/CAServices/AgentAuthenticationV2/index.php`,
{
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa(`${agentId}:${agentPassword}`),
},
body: params.toString(),
},
);
return response.json();
};
const loginViaSidecar = async () => {
const response = await fetch(`${SIDECAR_URL}/api/ozonetel/agent-login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
agentId,
password: agentPassword,
phoneNumber: sipId,
mode: 'blended',
}),
});
return response.json();
};
// Try sidecar first, fallback to direct
loginViaSidecar()
.catch(() => loginViaOzonetelDirect())
.then(data => {
if (data.status === 'success' || data.message?.includes('logged in')) {
setOzonetelStatus('logged-in');
console.log('Ozonetel agent login:', data.message);
} else {
setOzonetelStatus('error');
setOzonetelError(data.message ?? 'Unknown error');
console.warn('Ozonetel agent login issue:', data);
}
})
.catch(err => {
setOzonetelStatus('error');
setOzonetelError(err.message);
console.error('Ozonetel agent login failed:', err);
});
}
// Ozonetel status tracks whether the sidecar handled agent login
// We assume logged-in if SIP connects (sidecar handles the REST login)
setOzonetelStatus('logged-in');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);