Untitled

mail@pastecode.io avatar
unknown
html
2 months ago
1.3 kB
5
Indexable
Never
<button onclick="authenticateAndCallApi()">Authenticate and Call API</button>

<script>
    async function authenticateAndCallApi() {
        const authToken = await getAuthToken();

        if (!authToken) {
            console.error('Authentication token not available.');
            return;
        }

        const apiResponse = await fetch('http://10.10.20.14/api/', {
            method: 'GET',
            headers: {
                'Authorization': `Bearer ${authToken}`,
            },
        });

        if (apiResponse.ok) {
            const apiData = await apiResponse.json();
            console.log('API Response:', apiData);
        } else {
            console.error('Failed to call API:', apiResponse.statusText);
        }
    }

    async function getAuthToken() {
        const response = await fetch('http://10.10.20.14:3001/authenticate', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
        });

        if (response.ok) {
            const data = await response.json();
            console.log('Token:', data.token);
            return data.token;
        } else {
            console.error('Failed to obtain authentication token:', response.statusText);
            return null;
        }
    }
</script>
Leave a Comment