-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete-auth-fix.js
More file actions
170 lines (148 loc) · 5.73 KB
/
complete-auth-fix.js
File metadata and controls
170 lines (148 loc) · 5.73 KB
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
163
164
165
166
167
168
169
170
// Complete Authentication Fix - Addresses all issues comprehensively
console.log('🔧 AquaChain Complete Authentication Fix v2.0');
// 1. Stop all error spam immediately
console.log('🛑 Step 1: Stopping error spam...');
for (let i = 1; i < 10000; i++) {
clearInterval(i);
clearTimeout(i);
}
console.log('✅ All intervals and timeouts cleared');
// 2. Clear all authentication tokens
console.log('🧹 Step 2: Clearing all tokens...');
localStorage.removeItem('REACT_APP_USE_MOCK_DATA');
localStorage.removeItem('aquachain_token');
localStorage.removeItem('authToken');
localStorage.removeItem('token');
localStorage.removeItem('aquachain_user');
console.log('✅ All tokens cleared');
// 3. Check API endpoint availability
const API_ENDPOINT = 'https://vtqjfznspc.execute-api.ap-south-1.amazonaws.com/dev';
window.diagnoseAuthIssue = async function() {
console.log('🔍 Step 3: Diagnosing authentication issue...');
// Test different endpoints to see what exists
const endpointsToTest = [
'/api/auth/signin',
'/auth/signin',
'/api/v1/auth/signin',
'/api/health',
'/alerts',
'/api/devices'
];
for (const endpoint of endpointsToTest) {
try {
console.log(`🧪 Testing ${endpoint}...`);
const response = await fetch(`${API_ENDPOINT}${endpoint}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
console.log(` ${endpoint}: ${response.status} ${response.statusText}`);
if (response.status === 401) {
console.log(` ✅ ${endpoint} exists but requires authentication`);
} else if (response.status === 404) {
console.log(` ❌ ${endpoint} does not exist`);
} else if (response.status === 200) {
console.log(` ✅ ${endpoint} exists and is accessible`);
} else {
console.log(` ⚠️ ${endpoint} returned ${response.status}`);
}
} catch (error) {
console.log(` ❌ ${endpoint} failed: ${error.message}`);
}
}
};
// 4. Test with mock authentication
window.testWithMockAuth = function() {
console.log('🎭 Step 4: Testing with mock authentication...');
// Create a mock token
const mockToken = 'mock-token-' + Date.now();
localStorage.setItem('aquachain_token', mockToken);
localStorage.setItem('authToken', mockToken);
localStorage.setItem('token', mockToken);
console.log('✅ Mock token set:', mockToken);
console.log('🔄 Now test an API call to see the actual error');
// Test the alerts endpoint that was failing
fetch(`${API_ENDPOINT}/alerts?limit=50`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${mockToken}`
}
})
.then(response => {
console.log('📡 Mock auth test response:', response.status, response.statusText);
return response.text();
})
.then(text => {
console.log('📦 Response body:', text);
})
.catch(error => {
console.error('❌ Mock auth test failed:', error);
});
};
// 5. Enable mock data mode (safest option)
window.enableMockDataMode = function() {
console.log('🎭 Step 5: Enabling mock data mode...');
localStorage.setItem('REACT_APP_USE_MOCK_DATA', 'true');
localStorage.removeItem('aquachain_token');
localStorage.removeItem('authToken');
localStorage.removeItem('token');
console.log('✅ Mock data mode enabled');
console.log('🔄 Reloading page to use fake data...');
window.location.reload();
};
// 6. Try Cognito authentication (if available)
window.tryCognitoAuth = function() {
console.log('🔐 Step 6: Checking for Cognito authentication...');
// Check if AWS Amplify or Cognito is available
if (window.AWS || window.AWSCognito) {
console.log('✅ AWS SDK detected - Cognito might be available');
console.log('💡 This system might use Cognito instead of custom auth');
} else {
console.log('❌ No AWS SDK detected');
}
// Check for Cognito user pool configuration
const userPoolId = process.env.REACT_APP_USER_POOL_ID;
const clientId = process.env.REACT_APP_USER_POOL_CLIENT_ID;
if (userPoolId && clientId) {
console.log('✅ Cognito configuration found:');
console.log(' User Pool ID:', userPoolId);
console.log(' Client ID:', clientId);
console.log('💡 This system uses Cognito authentication');
} else {
console.log('❌ No Cognito configuration found');
}
};
// 7. Check current system state
console.log('📊 Current System State:');
console.log(' API Endpoint:', API_ENDPOINT);
console.log(' Mock Data Mode:', localStorage.getItem('REACT_APP_USE_MOCK_DATA') || 'Disabled');
console.log(' Environment:', process.env.NODE_ENV || 'Unknown');
// 8. Provide clear next steps
console.log('');
console.log('🎯 Available Commands:');
console.log(' diagnoseAuthIssue() - Test which endpoints exist');
console.log(' testWithMockAuth() - Test with fake token to see real error');
console.log(' tryCognitoAuth() - Check if system uses Cognito');
console.log(' enableMockDataMode() - Switch to fake data (safest)');
console.log('');
console.log('🚀 Recommended sequence:');
console.log(' 1. diagnoseAuthIssue()');
console.log(' 2. If endpoints exist: testWithMockAuth()');
console.log(' 3. If nothing works: enableMockDataMode()');
// 9. Suppress console errors temporarily
const originalError = console.error;
let errorCount = 0;
console.error = function(...args) {
errorCount++;
if (errorCount <= 3) {
originalError.apply(console, args);
} else if (errorCount === 4) {
originalError('🔇 Suppressing further errors to reduce noise...');
}
};
setTimeout(() => {
console.error = originalError;
console.log('🔊 Error logging restored');
}, 60000);
console.log('');
console.log('✅ Error spam should now be stopped!');
console.log('🔍 Run diagnoseAuthIssue() to continue debugging');