-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth-diagnostic.js
More file actions
150 lines (122 loc) · 4.39 KB
/
oauth-diagnostic.js
File metadata and controls
150 lines (122 loc) · 4.39 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
#!/usr/bin/env node
// OAuth Configuration Diagnostic Tool
console.log('OAuth Configuration Diagnostic\n');
console.log('============================\n');
// Check environment variables
const requiredVars = [
'NEXTAUTH_SECRET',
'NEXTAUTH_URL',
'GOOGLE_CLIENT_ID',
'GOOGLE_CLIENT_SECRET',
'GITHUB_ID',
'GITHUB_SECRET'
];
console.log('1. Environment Variables Check:');
console.log('-------------------------------');
// Load .env.local file
const fs = require('fs');
const path = require('path');
try {
const envContent = fs.readFileSync('.env.local', 'utf8');
const envVars = {};
envContent.split('\n').forEach(line => {
const [key, value] = line.split('=');
if (key && value) {
envVars[key.trim()] = value.trim();
}
});
requiredVars.forEach(varName => {
const value = envVars[varName];
const status = value ? 'SET' : 'MISSING';
const preview = value ? value.substring(0, 20) + '...' : 'N/A';
console.log(` ${varName}: ${status} (${preview})`);
});
} catch (error) {
console.log(' Error reading .env.local:', error.message);
}
console.log('\n2. OAuth App Configuration Requirements:');
console.log('---------------------------------------');
console.log(' Google OAuth App Requirements:');
console.log(' - Authorized redirect URIs must include:');
console.log(' * http://localhost:3000/api/auth/callback/google');
console.log(' - OAuth consent screen must be configured');
console.log(' - Client ID and Secret must be valid');
console.log('\n GitHub OAuth App Requirements:');
console.log(' - Authorization callback URL must be:');
console.log(' * http://localhost:3000/api/auth/callback/github');
console.log(' - App must allow access to user email');
console.log('\n3. Common Issues and Solutions:');
console.log('------------------------------');
console.log(' Issue: "Error 400: redirect_uri_mismatch"');
console.log(' Solution: Check OAuth app callback URLs match exactly');
console.log('\n Issue: "Access denied" or authentication fails');
console.log(' Solution: Verify OAuth app is not restricted to production domains');
console.log('\n Issue: "Configuration error"');
console.log(' Solution: Check environment variables are properly set');
console.log('\n4. Test OAuth Endpoints:');
console.log('------------------------');
const http = require('http');
// Test Google OAuth signin URL
const testGoogleAuth = () => {
return new Promise((resolve) => {
const req = http.request({
hostname: 'localhost',
port: 3000,
path: '/api/auth/signin/google',
method: 'GET',
}, (res) => {
resolve({
statusCode: res.statusCode,
headers: res.headers
});
});
req.on('error', (error) => {
resolve({ error: error.message });
});
req.setTimeout(5000, () => {
req.destroy();
resolve({ error: 'timeout' });
});
req.end();
});
};
// Test GitHub OAuth signin URL
const testGitHubAuth = () => {
return new Promise((resolve) => {
const req = http.request({
hostname: 'localhost',
port: 3000,
path: '/api/auth/signin/github',
method: 'GET',
}, (res) => {
resolve({
statusCode: res.statusCode,
headers: res.headers
});
});
req.on('error', (error) => {
resolve({ error: error.message });
});
req.setTimeout(5000, () => {
req.destroy();
resolve({ error: 'timeout' });
});
req.end();
});
};
(async () => {
const googleTest = await testGoogleAuth();
console.log(' Google signin endpoint:', googleTest.error ? `Error: ${googleTest.error}` : `Status: ${googleTest.statusCode}`);
const githubTest = await testGitHubAuth();
console.log(' GitHub signin endpoint:', githubTest.error ? `Error: ${githubTest.error}` : `Status: ${githubTest.statusCode}`);
console.log('\n5. Manual Testing Steps:');
console.log('------------------------');
console.log(' 1. Go to http://localhost:3000/auth-test');
console.log(' 2. Check browser console for error messages');
console.log(' 3. Test each authentication method');
console.log(' 4. Verify OAuth app callback URLs in provider consoles');
console.log('\n6. OAuth App Console URLs:');
console.log('--------------------------');
console.log(' Google: https://console.cloud.google.com/apis/credentials');
console.log(' GitHub: https://github.com/settings/developers');
})();