Skip to content

Commit

Permalink
feat: ✨ Adding the feature who will add a verification for the email (#5
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nduboi authored Apr 10, 2024
1 parent 366c97b commit ff27168
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
40 changes: 31 additions & 9 deletions api_server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ var transporter = nodemailer.createTransport({
}
});

async function send_verif_mail(email_adress, token) {
async function send_verif_mail(email_adress, token, url) {
const info = await transporter.sendMail({
from: '"Avnduboi" <[email protected]>',
to: email_adress,
subject: "Check your account ✔",
html: `<h1>Hello, to check your account click <a href="http://localhost/check_account&${token}">here</a></h1>`,
from: '"Avnduboi" <[email protected]>',
to: email_adress,
subject: "Check your account ✔",
html: `<h1>Hello, to check your account click <a href="http://${url}/check_account?token=${token}">here</a></h1>`,
});
}

Expand Down Expand Up @@ -58,7 +58,7 @@ app.get('/get_user_log', async (req, res) => {
if (pseudo != undefined) {
try {
const connection = await pool.getConnection();
const [rows, fields] = await connection.execute('SELECT pseudo, email FROM users WHERE pseudo = ?', [pseudo]);
const [rows, fields] = await connection.execute('SELECT pseudo, email, id, status_verif FROM users WHERE pseudo = ?', [pseudo]);
connection.release();
res.json(rows);
} catch (error) {
Expand All @@ -69,7 +69,7 @@ app.get('/get_user_log', async (req, res) => {
if (user_token != undefined) {
try {
const connection = await pool.getConnection();
const [rows, fields] = await connection.execute('SELECT pseudo, email FROM users WHERE token = ?', [user_token]);
const [rows, fields] = await connection.execute('SELECT pseudo, email, id, status_verif FROM users WHERE token = ?', [user_token]);
connection.release();
res.json(rows);
} catch (error) {
Expand All @@ -83,11 +83,12 @@ app.get('/get_user_log', async (req, res) => {

app.get('/send_verif_mail', async (req, res) => {
const token = req.query.token;
const url = req.query.server;
const api_token = req.query.api_token

if (api_token != getenv('API_TOKEN'))
return res.status(700).json({status: 'Wrong api token' });
if (token === undefined) {
if (token === undefined || url === undefined) {
return res.status(400).json({status: 'Check the urls parameters' });
}
try {
Expand All @@ -96,7 +97,7 @@ app.get('/send_verif_mail', async (req, res) => {
connection.release();
if (Object.keys(rows).length == 1) {
if (rows[0].status_verif == 0) {
send_verif_mail(rows[0].email, token);
send_verif_mail(rows[0].email, token, url);
res.json({status: 'Success'});
} else {
res.json({status: 'Already check'});
Expand Down Expand Up @@ -181,6 +182,27 @@ app.get('/set_challenge', async (req, res) => {
}
});

app.get('/set_verif_email_completed', async (req, res) => {
const token = req.query.token;
const id = req.query.id;
const api_token = req.query.api_token
if (api_token != getenv('API_TOKEN'))
return res.status(700).json({ error: 'Wrong api token' });
if ((token === undefined || id === undefined)) {
return res.status(400).json({ error: 'Check the urls parameters' });
}

try {
const connection = await pool.getConnection();
const [rows, fields] = await connection.execute('UPDATE `users` SET `status_verif` = ? WHERE `users`.`id` = ?', [1, id]);
connection.release();
res.json(rows);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Internal server error' });
}
});

const server = http.createServer(app);
server.listen(port, '0.0.0.0', () => {
console.log(`API server is running on port ${port}`);
Expand Down
40 changes: 36 additions & 4 deletions app_server/server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,43 @@ app.get("/account", (req, res) => {
res.sendFile(__dirname + "/src/account.html");
})


app.get("/build/css/main.css", (req, res) => {
res.sendFile(__dirname + "/src/build/css/main.css");
})

app.get("/check_account", (req, res) => {
var token = req.query.token

if (token === undefined) {
res.redirect('/');
return;
} else {
get_pseudo_from_api_with_token(token)
.then(info => {
if (Object.keys(info).length == 1) {
set_challenge_completed(token, info[0].id);
}
})
.catch(error => {
console.log(error);
});
res.redirect('/');
}
})

async function set_challenge_completed(token, id) {
try {
const response = await fetch('http://api_js:8080/set_verif_email_completed?token='+token+'&id='+id+'&api_token='+getenv('API_TOKEN'));
if (!response.ok) {
throw new Error('Failed to fetch data');
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error:', error);
}
}

async function get_user_from_api(mail) {
try {
const response = await fetch('http://api_js:8080/get_user_log?mail='+mail+'&api_token='+getenv('API_TOKEN'));
Expand All @@ -73,9 +105,9 @@ async function get_pseudo_from_api_with_token(token) {
}
}

async function send_mail_verif(token) {
async function send_mail_verif(token, url) {
try {
const response = await fetch('http://api_js:8080/send_verif_mail?token='+token+'&api_token='+getenv('API_TOKEN'));
const response = await fetch('http://api_js:8080/send_verif_mail?token='+token+'&server='+url+'&api_token='+getenv('API_TOKEN'));
if (!response.ok) {
throw new Error('Failed to fetch data');
}
Expand Down Expand Up @@ -178,7 +210,7 @@ io.on("connection", (socket) => {
var hash_token = bcrypt.hashSync(data.token, salt);
set_new_user_api(data.email, data.pseudo, hash_pass, hash_token)
.then (info => {
send_mail_verif(hash_token)
send_mail_verif(hash_token, data.server)
.then(into => {
io.emit("anwser_bdd_account :"+data.token, {status : "success", token : hash_token});
}).catch(error => {
Expand Down
2 changes: 1 addition & 1 deletion app_server/src/inscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <H1 class="text-center text-black font-bold w-full m-2">Inscription</H1>
var password = document.getElementById('pass').value;
var pseudo = document.getElementById('pseudo').value;
if (email != "" && password != "" && pseudo != "") {
socket.emit("create_user", {token : token, email : email, password : password, pseudo : pseudo});
socket.emit("create_user", {token : token, email : email, password : password, pseudo : pseudo, server : window.location.hostname});
} else {
document.getElementById('error').innerHTML = "Fill all inputs";
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
build: ./app_server
container_name: app_js
ports:
- "8083:80"
- "80:80"
volumes:
- ./app_server:/app_app
env_file:
Expand Down

0 comments on commit ff27168

Please sign in to comment.