node-programmeren4-studentenhuis-master/controllers/authentication.controller.js
/**
     * Register a new user. The user should provide a Voornaam, Achternaam, emailaddress and 
     * password. The emailaddress should be unique when it exists, an error must be thrown.
     * The password will be encrypted by the User class and must never be stored as plain text! 
     * 
     * @param {*} req The incoming request, containing valid properties.
     * @param {*} res The created user on success, or error on invalid properties.
     * @param {*} next ApiError when supplied properties are invalid.
     */
Register a new user. The user should provide a Voornaam, Achternaam, emailaddress and
password. The emailaddress should be unique when it exists, an error must be thrown.
The password will be encrypted by the User class and must never be stored as plain text!

@param {*} req The incoming request, containing valid properties.
@param {*} res The created user on success, or error on invalid properties.
@param {*} next ApiError when supplied properties are invalid.
Register a new user . The user should provide a Voornaam Achternaam emailaddress and password . The emailaddress should be unique when it exists an error must be thrown . The password will be encrypted by the User class and must never be stored as plain text! @param { * } req The incoming request containing valid properties . @param { * } res The created user on success or error on invalid properties . @param { * } next ApiError when supplied properties are invalid .
register(req, res, next) {
        try {
            assert(typeof (req.body.firstname) === 'string', 'firstname must be a string.')
            assert(typeof (req.body.lastname) === 'string', 'lastname must be a string.')
            assert(typeof (req.body.email) === 'string', 'email must be a string.')
            assert(typeof (req.body.password) === 'string', 'password must be a string.')
            assert(req.body.firstname.trim().length > 2, 'firstname must be at least 3 characters')
            assert(req.body.lastname.trim().length > 2, 'lastname must be at least 3 characters')
            assert(validateEmail(req.body.email.trim()), 'email must be a valid emailaddress')
            assert(req.body.password.trim().length > 2, 'password must be at least 3 characters')
        } catch (ex) {
            const error = new ApiError(ex.toString(), 412)
            next(error)
            return
        }
        pool.getConnection((err, connection) => {
            if (err) {
                logger.error('Error getting connection from pool: ' + err.toString())
                const error = new ApiError(err, 500)
                next(error);
                return
            }
            connection.query('SELECT `Email` FROM `user` WHERE `Email` = ?', [req.body.email], (err, rows, fields) => {
                if (err) {
                    const error = new ApiError(err, 412)
                    next(error);
                } else {
                    if (rows.length > 0) {
                        const error = new ApiError('Email already exists', 412)
                        next(error);
                    } else {
                        try {
                            const user = new User(
                                req.body.firstname,
                                req.body.lastname,
                                req.body.email,
                                req.body.password,
                            )
                            logger.info(user)
                            connection.query('INSERT INTO `user` (`Voornaam`, `Achternaam`, `Email`, `Password`) VALUES (?, ?, ?, ? )',
                                [user.name.firstname, user.name.lastname, user.email, user.password],
                                (err, rows, fields) => {
                                    connection.release()
                                    if (err) {
                                        const error = new ApiError(err, 412)
                                        next(error);
                                    } else {
                                        const payload = {
                                            user: user.email,
                                            id: rows.insertId
                                        }
                                        const userinfo = {
                                            token: auth.encodeToken(payload),
                                            username: user.name.firstname + ' ' + user.name.lastname,
                                            email: user.email,
                                        }
                                        res.status(200).json(userinfo).end()
                                    }
                                })
                        } catch (ex) {
                            next(ex)
                        }
                    }
                }
            })
        })
    }
register ( req , res , next ) { try { assert ( typeof ( req . body . firstname ) === 'string' , 'firstname must be a string.' ) assert ( typeof ( req . body . lastname ) === 'string' , 'lastname must be a string.' ) assert ( typeof ( req . body . email ) === 'string' , 'email must be a string.' ) assert ( typeof ( req . body . password ) === 'string' , 'password must be a string.' ) assert ( req . body . firstname . trim ( ) . length > 2 , 'firstname must be at least 3 characters' ) assert ( req . body . lastname . trim ( ) . length > 2 , 'lastname must be at least 3 characters' ) assert ( validateEmail ( req . body . email . trim ( ) ) , 'email must be a valid emailaddress' ) assert ( req . body . password . trim ( ) . length > 2 , 'password must be at least 3 characters' ) } catch ( ex ) { const error = new ApiError ( ex . toString ( ) , 412 ) next ( error ) return } pool . getConnection ( ( err , connection ) => { if ( err ) { logger . error ( 'Error getting connection from pool: ' + err . toString ( ) ) const error = new ApiError ( err , 500 ) next ( error ) ; return } connection . query ( 'SELECT `Email` FROM `user` WHERE `Email` = ?' , [ req . body . email ] , ( err , rows , fields ) => { if ( err ) { const error = new ApiError ( err , 412 ) next ( error ) ; } else { if ( rows . length > 0 ) { const error = new ApiError ( 'Email already exists' , 412 ) next ( error ) ; } else { try { const user = new User ( req . body . firstname , req . body . lastname , req . body . email , req . body . password , ) logger . info ( user ) connection . query ( 'INSERT INTO `user` (`Voornaam`, `Achternaam`, `Email`, `Password`) VALUES (?, ?, ?, ? )' , [ user . name . firstname , user . name . lastname , user . email , user . password ] , ( err , rows , fields ) => { connection . release ( ) if ( err ) { const error = new ApiError ( err , 412 ) next ( error ) ; } else { const payload = { user : user . email , id : rows . insertId } const userinfo = { token : auth . encodeToken ( payload ) , username : user . name . firstname + ' ' + user . name . lastname , email : user . email , } res . status ( 200 ) . json ( userinfo ) . end ( ) } } ) } catch ( ex ) { next ( ex ) } } } } ) } ) }
Register a new user.
Register a new user .
// logger.info('register') // logger.info(req.body) /**
         * Query the database to see if the email of the user to be registered already exists.
         */ // logger.info('found results') // logger.info(rows) /**
                         * User constructor could throw an exception
                         */ // Create an object containing the data we want in the payload. // Userinfo returned to the caller.