Giter Site home page Giter Site logo

registeruser's Introduction

GitHub: RegisterUser

http://localhost:4000/user http://localhost:4000/create-user using Robo3T GUI

added db connection in app.js (mongoose):

const mongoose = require('mongoose');

// connect db mongoose.connect("mongodb://localhost:27017/auth", { useUnifiedTopology: true, useCreateIndex: true, useNewUrlParser: true });

mongoose.connection.on("error", console.error); mongoose.connection.on("open", function(){ console.log("Database connected ..."); })

prepare DB


new file: user.js in new folder models/:

const mongoose = require("mongoose");

const UserSchema = mongoose.Schema({ name: { type: String, required: true }, email: { type: String, required: true }, password: { type: String, required: true } })

module.exports = mongoose.model("User", UserSchema);


New file welcome.hbs

Welcome ...

add in routes.js

const bcrypt = require("bcrypt"); const User = require("./models/User");

in routes error handling:

switch

    if(errors){
        req.session.errors = errors;        
        res.redirect('/user');
    } else {
        res.redirect('/create-user')
    }
});

to

    if (errors.length) {
        req.session.errors = errors;
        res.redirect('/user');
    } else {
        // res.redirect('/create-user')
        bcrypt.hash(req.body.password, 10, (err, hash) => {
            if (err) {
                res.json({
                    error: err
                })
            } else {
                const user = new User({
                    name: req.body.name,
                    email: req.body.email,
                    password: hash
                });
                user.save().then(result => {
                    const userName = result.name;
                    res.render("welcome", {userName})
                }).catch(err => {
                    res.json({
                        error: err
                    })
                })
            }
        })
    }
});

in welcome.hbs change stuff to:

Welcome {{userName}}

// debugging works with no .vscode file // mark some stop points in app.js and press F5 -> type node.js // debug output in "debug console"

avoid doubled emails:

in routes: add User.find.... and move bcrypt stuff into else plus change the catch error

    User.find({email: req.body.email}).exec().then(user => {
                if(user.length >= 1) {
                    return res.status(409).json({  // test without return
                        message: "Mail exists" // {"message":"Mail exists"} if user mail is already in database
                    })
                } 
                .
                .
                .
                    .catch(err => {
                            // res.json({
                                // error: err
                                res.status(500).json({error:err})
                            // })
                        }

complete code to avoid doubled emails:

{
        // res.redirect('/create-user')
        // part to avoid doubled email registrations
        User.find({email: req.body.email}).exec().then(user => {
            if(user.length >= 1) {
                return res.status(409).json({  // test without return
                    message: "Mail exists" // {"message":"Mail exists"} if user mail is already in database
                })
            } else {
                bcrypt.hash(req.body.password, 10, (err, hash) => {
                    if (err) {
                        res.json({
                            error: err
                        })
                    } else {
                        const user = new User({
                            name: req.body.name,
                            email: req.body.email,
                            password: hash
                        });
                        user.save().then(result => {
                            const userName = result.name;
                            res.render("welcome", {userName})
                        }).catch(err => {
                            // res.json({
                                // error: err
                                res.status(500).json({error:err})
                            // })
                        })
                    }
                })
            }
        })
    }

Test output on page: {"message":"Mail exists"} -> because user mail is already in database

registeruser's People

Contributors

itsanytime avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.