-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-token-flow.mjs
More file actions
58 lines (48 loc) · 2.06 KB
/
test-token-flow.mjs
File metadata and controls
58 lines (48 loc) · 2.06 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
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log('\n🔐 Testing JWT Token Flow\n');
console.log('=' . repeat(80));
// Load auth service environment
console.log('\n📦 Loading Auth Service Environment...');
dotenv.config({ path: join(__dirname, 'auth-service', '.env') });
const authSecret = process.env.JWT_ACCESS_SECRET;
console.log('Auth JWT_ACCESS_SECRET:', authSecret);
// Generate token like auth service does
const testPayload = {
userId: 'test-user-123',
email: 'test@example.com',
role: 'STUDENT'
};
const authOptions = {
expiresIn: '15m',
issuer: 'codexa-auth-service',
subject: testPayload.userId,
};
const token = jwt.sign(testPayload, authSecret, authOptions);
console.log('\n✅ Token Generated by Auth Service:');
console.log('Token (first 50 chars):', token.substring(0, 50) + '...');
console.log('Full token length:', token.length);
// Now verify with classroom service logic
console.log('\n📦 Loading Classroom Service Environment...');
dotenv.config({ path: join(__dirname, 'classroom-service', '.env'), override: true });
const classroomSecret = process.env.JWT_ACCESS_SECRET || process.env.JWT_SECRET;
console.log('Classroom JWT_ACCESS_SECRET:', classroomSecret);
console.log('\n🔍 Attempting to verify token with Classroom Service logic...');
try {
const decoded = jwt.verify(token, classroomSecret);
console.log('\n✅ SUCCESS! Token verified by Classroom Service');
console.log('Decoded payload:', JSON.stringify(decoded, null, 2));
} catch (error) {
console.log('\n❌ FAILED! Token verification error:');
console.log('Error:', error.message);
console.log('Error name:', error.name);
}
console.log('\n🔍 Comparing Secrets:');
console.log('Auth Secret: ', authSecret);
console.log('Classroom Secret: ', classroomSecret);
console.log('Secrets Match: ', authSecret === classroomSecret ? '✅ YES' : '❌ NO');
console.log('\n' + '='.repeat(80) + '\n');