You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.8 KiB
85 lines
2.8 KiB
/*
|
|
Modifier votre front (code fait à la section 2) pour qu'il interroge
|
|
l'API et indique à l'utilisateur si il existe en base de données
|
|
|
|
Elle contiendra au minimum :
|
|
- un fichier README indiquant comment installer votre projet et les routes disponibles
|
|
- un fichier package.json
|
|
- un fichier index.js contenant votre code Node
|
|
*/
|
|
|
|
import http from 'http';
|
|
import * as url from "url";
|
|
// npm i node-fetch
|
|
// https://www.npmjs.com/package/node-fetch
|
|
import fetch from 'node-fetch';
|
|
|
|
function outputHTML5(status, response, content, title)
|
|
{
|
|
response.writeHead(status, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
response.write(
|
|
`
|
|
<!DOCTYPE html>
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>${title}</title>
|
|
<style type="text/css">*{ font-family: sans-serif }</style>
|
|
</head>
|
|
<body>
|
|
${content}
|
|
</body>
|
|
</html>
|
|
`
|
|
);
|
|
response.end();
|
|
}
|
|
|
|
var server = http.createServer(function (request, response) {
|
|
if(request.url === "/" && request.method==="GET") {
|
|
let formHtml = "<form method='post' action='/'>";
|
|
formHtml += "<label for='email'>Email</label><input id='email' type='email' value='' name='email' />";
|
|
formHtml += "<br/><label for='password'>Mot de passe</label><input id='password' type='password' value='' name='password' />";
|
|
formHtml += "<br/><button type='submit'>aller !</button>"
|
|
formHtml += "</form>";
|
|
outputHTML5(200,response,formHtml,"login");
|
|
}
|
|
else if(request.url === "/" && request.method==="POST") {
|
|
let responseString = "/?";
|
|
console.log("In post");
|
|
request.on('data', function (varposted) {
|
|
responseString += varposted;
|
|
}).on('end', async function () {
|
|
let parsedData = url.parse(responseString, true);
|
|
|
|
if(parsedData.query['email']) {
|
|
console.log('got' + parsedData.query['email'])
|
|
fetch('http://localhost:8088/check',
|
|
{
|
|
method:'POST',
|
|
body:responseString
|
|
}
|
|
)
|
|
.then(apiresponse =>
|
|
apiresponse.json()
|
|
)
|
|
.then( json => {
|
|
if(json.emailValid) {
|
|
outputHTML5(200,response,'Ok ! Connexion possible :)',"Cool!");
|
|
}
|
|
else {
|
|
outputHTML5(200,response,"Cet mail n'existe pas ou le mot de passe ne correspond pas à l'email","Zut!");
|
|
}
|
|
})
|
|
.catch(err => {
|
|
console.log(err);
|
|
outputHTML5(500,response,'Y\'a eu un souci',"Oups!");
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
outputHTML5(404,response,'Y\'a rien ici',"Oups");
|
|
}
|
|
});
|
|
|
|
server.listen(80); |