Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var path = require('path');
- var mongo = require('mongodb');
- var bodyParser = require('body-parser');
- var expressValidator = require('express-validator');
- var passport = require('passport')
- var LocalStrategy = require('passport-local').Strategy;
- var app = express();
- app.use(bodyParser.urlencoded({
- extended: false
- }));
- app.use(expressValidator());
- //enter the name of the database in the end
- var new_db = "mongodb://localhost:27017/detail";
- app.get('/', function (req, res) {
- res.set({
- 'Access-Control-Allow-Origin': '*'
- });
- return res.redirect('/public/index.html');
- }).listen(3000);
- console.log("Server listening at : 3000");
- app.use('/public', express.static(__dirname + '/public'));
- app.use(bodyParser.json());
- // Sign-up function starts here. . .
- app.post('/sign_up', function (req, res) {
- var email = req.body.email;
- var password = req.body.password;
- var cpasssword = req.body.cpassword;
- req.checkBody('email', 'email is required').notEmpty();
- req.checkBody('email', 'email is not valid').isEmail();
- req.checkBody('password', 'password is required').notEmpty();
- req.checkBody('cpassword', 'confirm password is required').notEmpty();
- req.checkBody('cpassword', 'password do not match').equals(req.body.password);
- var data = {
- "email": email,
- "password": password,
- "cpassword": cpasssword
- }
- mongo.connect(new_db, function (error, db) {
- if (error) {
- throw error;
- }
- console.log("connected to database successfully");
- //CREATING A COLLECTION IN MONGODB USING NODE.JS
- var dbase = db.db("detail");
- dbase.createCollection("details", (err, collection) => {
- if (err) throw err;
- console.log("Details collection created successfully");
- });
- dbase.collection("details").insertOne(data, (err, collection) => {
- if (err) throw err;
- console.log("Record inserted successfully");
- console.log(collection);
- });
- });
- console.log("DATA is " + JSON.stringify(data));
- res.set({
- 'Access-Control-Allow-Origin': '*'
- });
- return res.redirect('/public/success.html');
- });
- app.post('/login',
- passport.authenticate('local'),
- function (req, res) {
- // If this function gets called, authentication was successful.
- // `req.user` contains the authenticated user.
- res.redirect('/users/' + req.user.username);
- });
- passport.use(new LocalStrategy(
- function (username, password, done) {
- User.findOne({
- username: username
- }, function (err, user) {
- if (err) {
- return done(err);
- }
- if (!user) {
- return done(null, false, {
- message: 'Incorrect username.'
- });
- }
- if (!user.validPassword(password)) {
- return done(null, false, {
- message: 'Incorrect password.'
- });
- }
- return done(null, user);
- });
- }
- ));
Add Comment
Please, Sign In to add comment