Files
pos-system/apps/client-example/scripts/developers/login-and-set-token.js

82 lines
2.9 KiB
JavaScript

// Script để login và set token
// Chạy trong browser console tại http://localhost:3001
// KHÔNG chạy với node command!
// Check if running in browser
if (typeof window === 'undefined') {
console.error('❌ Script này phải chạy trong browser console, không phải Node.js!');
console.log('💡 Hướng dẫn:');
console.log('1. Mở browser tại http://localhost:3001');
console.log('2. Mở Developer Console (F12)');
console.log('3. Copy và paste script này vào console');
process.exit(1);
}
async function loginAndSetToken() {
try {
console.log('🔐 Logging in...');
// Login với admin account
const response = await fetch('http://localhost:7001/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: 'hongochai10@icloud.com',
password: 'Hai@2025'
})
});
const data = await response.json();
console.log('📋 Login response:', data);
if (data.success && data.data && data.data.tokens) {
// Set tokens vào localStorage
localStorage.setItem('auth_token', data.data.tokens.accessToken);
localStorage.setItem('refresh_token', data.data.tokens.refreshToken);
console.log('✅ Login successful!');
console.log('👤 User:', data.data.user.email);
console.log('🎫 Access Token:', data.data.tokens.accessToken.substring(0, 50) + '...');
console.log('🔄 Refresh Token:', data.data.tokens.refreshToken.substring(0, 50) + '...');
// Verify tokens are stored
console.log('🔍 Verifying token storage:');
console.log('- auth_token:', localStorage.getItem('auth_token') ? '✅ Stored' : '❌ Missing');
console.log('- refresh_token:', localStorage.getItem('refresh_token') ? '✅ Stored' : '❌ Missing');
// Test ppoint service
console.log('🧪 Testing PPoint Service...');
try {
// Import từ đúng path
const { PPointService } = await import('/src/lib/ppoint.service.js');
const ppointService = new PPointService();
const balance = await ppointService.getUserBalance();
console.log('✅ PPoint Service working!');
console.log('💰 Balance:', balance);
// Test other endpoints
const dailyStatus = await ppointService.getDailyPointsStatus();
console.log('📅 Daily Points Status:', dailyStatus);
} catch (ppointError) {
console.error('❌ PPoint Service error:', ppointError);
console.log('🔍 Error details:', ppointError.message);
}
} else {
console.error('❌ Login failed:', data.error?.message || 'Unknown error');
console.log('📋 Full response:', data);
}
} catch (error) {
console.error('❌ Login error:', error);
}
}
// Run the function
loginAndSetToken();