Add temp pw

This commit is contained in:
Skylar Grant 2024-09-12 12:24:02 -04:00
parent 709cc83fec
commit d50cdae095
3 changed files with 130 additions and 0 deletions

View File

@ -24,6 +24,20 @@ app.post('/print-credentials', async (req, res) => {
}
});
// Print temp password
app.post('/print-temp-pw', async (req, res) => {
try {
const { password } = req.body;
if (!password) {
return res.status(400).json({ message: 'Password is required.' });
}
await Thermal.printCredentials(password, req, res);
} catch (error) {
console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message });
}
});
// Print Student MaineCC Info
app.post('/print-mcc', async (req, res) => {
try {

View File

@ -160,6 +160,77 @@ module.exports = {
});
});
// Handle any errors during file writing
writeStream.on('error', (err) => {
console.error('Error writing PDF file:', err);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
},
printTempPassword(password, req, res) {
// Create the output text
const infoArray = [
'We have changed your password to a temporary password, shown below. Next time you log into Microsoft you will be prompted to change your password.\n',
`Password requirements:\n8+ characters\n1+ Uppercase\n1+ Lowercase\n1+ Special (!/_=.,?)\n`,
`Temporary Password:`
`${password}\n`,
receiptFooter
];
const infoString = infoArray.join('\n');
// Define the path for the PDF file
const pdfFilePath = path.join(__dirname, 'output.pdf');
// Create a new PDF document
const doc = new PDFDocument({
size: [204, 400],
margin: 5
});
// Create a writable stream to save the PDF to a file
const writeStream = fs.createWriteStream(pdfFilePath);
doc.pipe(writeStream);
// Add content (image, text) to the PDF
doc.image('./public/itslogo.jpg', {
fit: [196, 100],
align: 'center',
valign: 'top',
});
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.fontSize(12).text(infoString, {
align: 'left',
});
// Finalize the PDF
doc.end();
writeStream.on('finish', () => {
// PDF file has been saved, now print it using lp command
const printer = spawn('lp', ['-d', 'ITThermal', pdfFilePath]);
// Handle error if the printing process fails
printer.on('error', (err) => {
console.error('Failed to start printing process:', err);
res.status(500).json({ message: 'Failed to start printing process', error: err.message });
});
// Handle the process exit event
printer.on('close', (code) => {
if (code === 0) {
res.send('PDF sent to printer successfully.');
} else {
res.status(500).json({ message: 'Failed to print PDF' });
}
});
});
// Handle any errors during file writing
writeStream.on('error', (err) => {
console.error('Error writing PDF file:', err);

View File

@ -43,8 +43,25 @@
</button>
</div>
</form>
<form id="printTempPw" class="space-y-4 mt-12"></form>
<div>
<label for="password" class="block text-sm font-medium text-gray-700">Temp Password:</label>
<input type="text" id="password" name="password" required
class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
</div>
<div class="text-center">
<button type="submit"
class="w-full bg-gray-500 text-white py-2 px-4 rounded-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50 mt-2">
Print Temp Password
</button>
</div>
</form>
</div>
<script>
document.getElementById('print-mcc').addEventListener('click', (event) => {
// Send a POST request with the form data to the server
@ -91,6 +108,34 @@
.then(message => alert(message))
.catch(error => alert('Error: ' + error));
});
document.getElementById('printTempPw').addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting normally
// Get the form data
const formData = new FormData(event.target);
const password = formData.get('password');
// Send a POST request with the form data to the server
fetch('/print-temp-pw', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
password: password
}),
})
.then(response => {
if (response.ok) {
return response.text(); // or .json() if your server returns JSON
} else {
throw new Error('Failed to send print request');
}
})
.then(message => alert(message))
.catch(error => alert('Error: ' + error));
});
</script>
</body>
</html>