Tech Stack Advisor - Code Viewer

← Back to File Tree

test-auth.html

Language: html | Path: backend/static/test-auth.html | Lines: 62
<!DOCTYPE html>
<html>
<head>
    <title>Auth Test</title>
</head>
<body>
    <h1>Authentication Test</h1>
    <div id="status"></div>
    <div id="userInfo"></div>
    <button onclick="testAuth()">Test Auth</button>
    <button onclick="clearStorage()">Clear Storage</button>

    <script src="/auth.js"></script>
    <script>
        function testAuth() {
            const status = document.getElementById('status');
            const userInfo = document.getElementById('userInfo');

            status.innerHTML = '<h2>Test Results:</h2>';

            // Test 1: Check if functions exist
            status.innerHTML += '<p>isAuthenticated function exists: ' + (typeof isAuthenticated === 'function') + '</p>';
            status.innerHTML += '<p>getToken function exists: ' + (typeof getToken === 'function') + '</p>';
            status.innerHTML += '<p>fetchUserInfo function exists: ' + (typeof fetchUserInfo === 'function') + '</p>';

            // Test 2: Check token
            const token = typeof getToken === 'function' ? getToken() : 'function not found';
            status.innerHTML += '<p>Token: ' + (token ? token.substring(0, 20) + '...' : 'null') + '</p>';

            // Test 3: Check authentication status
            const isAuth = typeof isAuthenticated === 'function' ? isAuthenticated() : false;
            status.innerHTML += '<p>Is Authenticated: ' + isAuth + '</p>';

            // Test 4: Fetch user info
            if (isAuth && typeof fetchUserInfo === 'function') {
                status.innerHTML += '<p>Fetching user info...</p>';
                fetchUserInfo().then(user => {
                    if (user) {
                        userInfo.innerHTML = '<h3>User Info:</h3><pre>' + JSON.stringify(user, null, 2) + '</pre>';
                    } else {
                        userInfo.innerHTML = '<p>No user info returned</p>';
                    }
                }).catch(error => {
                    userInfo.innerHTML = '<p>Error: ' + error.message + '</p>';
                });
            } else {
                userInfo.innerHTML = '<p>Not authenticated or fetchUserInfo not available</p>';
            }
        }

        function clearStorage() {
            localStorage.clear();
            alert('localStorage cleared. Refresh to see changes.');
        }

        // Auto-test on load
        window.addEventListener('load', function() {
            setTimeout(testAuth, 500);
        });
    </script>
</body>
</html>