Activation completed

This commit is contained in:
Alessandro Ferro 2024-03-08 09:58:06 +01:00
parent 0626cd02c8
commit 6d79cb04d9
5 changed files with 58 additions and 6 deletions

View File

@ -8,6 +8,7 @@ LIMITER_MAXIMUM_PER_WINDOW = 5000 # Requests for each limiter window
SMTP_USERNAME = blink@ik.me # Fill only if NEEDS_EMAIL_VERIFICATION is true
SMTP_PASSWORD = your_password # Fill only if NEEDS_EMAIL_VERIFICATION is true
SMTP_PORT = 465 # Fill only if NEEDS_EMAIL_VERIFICATION is true
FRONT_END_URL = http://localhost # Primary fron-end's root URL
# Database settings
POSTGRES_SERVER = localhost

View File

@ -76,8 +76,7 @@ async function registerPerson(req, res) {
req.body.qualification);
await personModel.registerPerson(personToInsert, activationLink);
if (process.env.NEEDS_EMAIL_VERIFICATION === 'true') {
// TODO generalize
mailUtils.sendConfirmationLink(req.body.email, 'http://localhost:3000/api/persons/me/activation?q=' + activationLink);
mailUtils.sendConfirmationLink(req.body.email, activationLink);
}
return res.status(200).json({
@ -308,7 +307,7 @@ async function confirmActivation(req, res) {
errors: errors.array()
});
}
const personId = await activationModel.getPersonIdByIdentifier(req.query.q);
const personId = await activationModel.getPersonIdByIdentifier(req.body.code);
if (!personId) {
return res.status(401).json({
error: 'Activation Link either not valid or expired'
@ -330,7 +329,7 @@ const publicRoutes = express.Router(); // Routes not requiring token
publicRoutes.post('/persons', personValidator.registerValidator, registerPerson);
publicRoutes.post('/persons/me/token', personValidator.getTokenValidator, createTokenByEmailAndPassword);
publicRoutes.get('/persons/:id/details', getPerson);
publicRoutes.get('/persons/me/activation', personValidator.confirmActivationValidator, confirmActivation);
publicRoutes.post('/persons/me/activation', personValidator.confirmActivationValidator, confirmActivation);
const protectedRoutes = express.Router(); // Routes requiring token
protectedRoutes.use(jwtUtils.verifyToken);

View File

@ -9,7 +9,8 @@ let transporter = nodemailer.createTransport({
}
});
function sendConfirmationLink(destinationEmail, confirmationLink) {
function sendConfirmationLink(destinationEmail, code) {
const confirmationLink = `${process.env.FRONT_END_URL}/activate-account.html?q=${code}`
let mailOptions = {
from: `"Blink" ${process.env.SMTP_USERNAME}`,
to: destinationEmail,

View File

@ -66,7 +66,7 @@ const updatePersonValidator = [
];
const confirmActivationValidator = [
check('q').trim().escape()
check('code').trim().escape()
]
module.exports = {

View File

@ -0,0 +1,51 @@
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</head>
<body>
<div id="successDialog" class="alert alert-success" role="alert" style="display: none;">
<p>Your account has been activated! Welcome onboard.</p>
<p>Log in <a href="login.html">here</a></p>
</div>
<div id="errorDialog" class="alert alert-danger" role="alert" style="display: none;">
URL either invalid or account already activated.
</div>
<script src="../js/constants.js"></script>
<script>
window.addEventListener("load", async function () {
await activateAccount();
});
async function activateAccount () {
const code = new URLSearchParams(window.location.search).get('q');
if(!code){
document.getElementById('errorDialog').style.display = 'block';
return;
}
const response = await fetch(`${API_URL}/persons/me/activation`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
code
}),
});
if(response.ok) {
document.getElementById('successDialog').style.display = 'block';
}
else {
document.getElementById('errorDialog').style.display = 'block';
}
}
</script>
</body>
</html>