Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function(app, request, MongoClient, bcrypt){
- app.post('/api/inscription', function(req,res){ //page connexion / inscription
- var ip = req.headers['x-forwarded-for'];
- var pseudo = req.body.pseudo;
- var password = req.body.password; //récupération des champs du formulaire
- var verifPassword = req.body.verifPassword;
- var mail = req.body.mail;
- var recaptcha = req.body.recaptcha;
- var errorInscription = []; //tableau stock toute les erreur d'inscriptions
- var regEmail = new RegExp('^[0-9A-Za-z._-]+@{1}[0-9A-Za-z.-]{2,}[.]{1}[A-Za-z]{2,5}$'); //reg format email
- var regPseudo = new RegExp('^[a-zA-Z]+[0-9]{0,4}[_-]{0,1}[A-Za-z0-9]+$'); //start par une lettre, puis peut mettre 0 à 4 chiffre puis 1 tirets puis doit finir par chiffres ou lettres/
- MongoClient.connect("mongodb://localhost", function(err,client){
- if(err) throw err;
- var db = client.db('viewmc');
- db.collection('account').find({user: { $regex: "^"+pseudo+"$", $options: '-i' }}).count(function(err, results){
- if (err) throw err;
- if (pseudo != undefined && password != undefined && verifPassword != undefined && pseudo != '' && password != '' && verifPassword != '' && mail != undefined && mail != '') {
- }else{
- errorInscription.champs = "Veuillez remplir tous les champs !";
- }
- if (results != 0){
- if(errorInscription.champs == undefined){
- errorInscription.pseudoCheck = "Le pseudo est déjà utilisé"; //requete => regarde si pseudo déjà use
- }
- }
- if(pseudo != undefined){
- if(pseudo.length >= 4 && pseudo.length <=30){ //check taille pseudo
- }else{
- if(errorInscription.champs == undefined){
- errorInscription.pseudoLength = "Votre pseudo doit être compris entre 4 et 30 carcatères !";
- }
- }
- }
- if(regPseudo.test(pseudo)){ //check format pseudo
- }else{
- if(errorInscription.champs == undefined){
- errorInscription.pseudoVerif = "Pseudo invalide !"
- }
- }
- if(password != undefined){
- if(password.length >= 5 && password.length <=255){ //check taille mdp
- }else{
- if(errorInscription.champs == undefined){
- errorInscription.passwordLength = "Votre mot de passe doit contenir plus de 5 caractères !";
- }
- }
- }
- if(password == verifPassword && password != '' && verifPassword != '' && password !== 'undefined' && verifPassword !== 'undefined'){ //check saisie mdp si identique
- }else{
- if(errorInscription.champs == undefined){
- errorInscription.passwordConfirm = "Les deux mots de passe entrées ne sont pas identiques.";
- }
- }
- if(regEmail.test(mail)){ //test format email
- }else{
- if(errorInscription.champs == undefined){
- errorInscription.mail = "Le format de l'email n'est pas valide !";
- }
- }
- /**********************************RECAPTCHA******************************************/
- if(recaptcha === undefined || recaptcha === '' || recaptcha === null){
- if(errorInscription.champs == undefined){
- errorInscription.captchaVide = "Captcha non remplis (si vous ne voyez pas le captcha, CTRL + F5)";
- }
- }
- const secretKey = "6LdIuEEUAAAAAGYz77p9ExTlGDFV_lBK7gktAQYY";
- const verificationURL = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + recaptcha;
- request(verificationURL,function(error,response,body) {
- body = JSON.parse(body);
- if(body.success !== undefined && !body.success) {
- errorInscription.errorCaptcha = "Avez-vous bien remplis le captcha :) ?"; //C'est probablement un bot
- }
- });
- /**********************************RECAPTCHA******************************************/
- var nbr_error = 0;
- for(var msgError in errorInscription){ //compte le nombre d'error
- nbr_error+=1;
- }
- if (nbr_error == 0){
- var start = new Date(); //DATE
- var inscriptionDate = 'Le ' + start.getDate() + '/' + start.getMonth() + parseInt(1) + '/' + start.getFullYear() + ' à ' + start.getHours() + 'h:' + start.getMinutes() + 'm:' + start.getSeconds() + 's';
- bcrypt.hash(password, 10, function(err, hash) {
- if (err) throw err;
- MongoClient.connect("mongodb://localhost", function(err, client){ //connection à la bdd
- if (err) throw err;
- var data = {user: pseudo, password: hash, mail: mail, inscriptionDate: inscriptionDate};
- var db = client.db('viewmc')
- db.collection('account').insert(data, null, function(err,results){
- if (err) throw err;
- req.session.user = pseudo;
- req.session.mail = mail;
- res.json({successInscription: "Bravo, vous êtes maintenant inscrit !", user: req.session.user, mail: req.session.mail, etat: 1});
- });
- });
- });
- }else{
- var inscription = {
- pseudoCheck: errorInscription.pseudoCheck,
- champs: errorInscription.champs,
- mail: errorInscription.mail,
- passwordConfirm: errorInscription.passwordConfirm,
- passwordLength: errorInscription.passwordLength,
- pseudoVerif: errorInscription.pseudoVerif, //reponse json client
- pseudoLength: errorInscription.pseudoLength,
- captchaVide: errorInscription.captchaVide,
- errorCaptcha: errorInscription.errorCaptcha,
- etat: 0
- }
- res.json(inscription);
- }
- });
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement