Wednesday, 3 May 2017

NodeJS User login without using Jade or Handlebars

I am currently trying to create a user registration and login system. I am using the full MEAN stack but I cannot find examples where you use it without JADE or Handlebars.

My main use is that none of those examples actually show how you connect the separate collections to NODE and Express. I've literally have browsed the first two google pages on available examples but still can't find nothing.

I am trying to use passport for verification. This is the code that I am trying to build:

Node Script:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/loginapp');
var express = require('express');
var app = express();
var db = mongoose.connection;
var path = require('path');
var fs = require('fs');
app.use('/', express.static(path.join(__dirname, 'static')));


app.post("/",function(req,res){
    res.end("Registration Succesfully Completed!");

    var db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error:'));
        db.once('open', function (callback) {
        console.log("connected.")
    });

    // Schema
    var RegSchema = mongoose.Schema({
        Name: String,
        Pass: String
        });

    // Model
    var UserReg = mongoose.model('UserReg', RegSchema);

    // Add in collection
    var UserAdd = new UserReg({
        Name: req.body.name,
        Pass: req.body.pass
    });

    // Save
    UserAdd.save(function (err, fluffy) {
        if (err) return console.error(err);
    });
});


function isLoggedIn (req, res, next) {
  if (!(req.session && req.session.user)) {
    return res.send('Not logged in!');
  }
  next();
}

app.get("/",  function (req, res) {
   //Something private
   res.sendFile(__dirname + 'index.html');


})



app.get("/api", isLoggedIn, function (req, res) {
   //Something private
   res.sendFile( __dirname +  "secondPage.html" );

})


app.listen(8000, function() {
  console.log("Server is running!");
});

HTML code

<body>
        <h1> HELLO HTTP TEST </h1>

        <div class="container">
            <div class="wrapper">
                <div class="row">

            <form ="form-group">
    <input type="text" class="form-control" id="name" placeholder="name"><br>

    <input type="password" class="form-control" id="pass" placeholder="Password"><br>

    <button type="submit" class="btn btn-primary" id="reg-form-btn">Registration!</button>
</form>

            </div>
        </div>
    </div>

P.S. I am ok with using Angular as well. My main issues is connecting the DB schema to the NODE back end script and creating verification without using JADE or Handlebars



via coderJoe

No comments:

Post a Comment