Guest User

Untitled

a guest
Nov 30th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const PORT = process.env.PORT || 3001;
  4. const express = require('express');
  5. const app = express();
  6. const path = require('path');
  7. const passport = require('passport')
  8. const initPassport = require('./passport/init');
  9. const session = require('express-session')
  10. const mongoose = require('mongoose');
  11. const bodyParser = require('body-parser');
  12. const cookieParser = require('cookie-parser')
  13. const configDB = require('./config/database.js');
  14. const MongoStore = require('connect-mongo')(session);
  15. var flash = require('connect-flash');
  16.  
  17. //Connect to mongo
  18. mongoose.connect(configDB.url, {
  19. useMongoClient: true
  20. }).then(() => console.log('connection with database succeeded'))
  21. .catch(err => console.error(err));
  22.  
  23.  
  24. // Initialize Passport
  25. initPassport(passport);
  26.  
  27. // Serve static assets
  28. app.use(express.static(path.resolve(__dirname, '..', 'build')));
  29.  
  30.  
  31. //use cookie parser to store data
  32. app.use(cookieParser());
  33. app.use(session({
  34. secret: 'mysecret',
  35. store : new MongoStore ({
  36. db : mongoose.connection.db,
  37. host : '127.0.0.1',
  38. port : '27017',
  39. url : configDB.url
  40. }),
  41. maxAge : 2 * 60 * 60 * 1000,
  42. resave: false,
  43. saveUninitialized: false
  44. })); // session secret
  45.  
  46. app.use(bodyParser.urlencoded({ extended: false }));
  47. app.use(passport.initialize());
  48. app.use(passport.session()); // persistent login sessions
  49. app.use(flash()); // use connect-flash for flash messages stored in session
  50.  
  51. app.post('/signup', passport.authenticate('signup', {
  52. successRedirect: '/',
  53. failureRedirect: '/signup'
  54. }));
  55.  
  56. app.listen(PORT, () => {
  57. console.log(`App listening on port ${PORT}!`);
  58. });
  59.  
  60. import React, { Component } from 'react';
  61. import constants from 'constants/AuthPageConstants';
  62.  
  63. class RegisterForm extends Component {
  64. render() {
  65. return (
  66. <div className="tab-pane fade in" id="basic-tab2">
  67. <form action="/test" method="POST">
  68. <div className="text-center">
  69. <div className="icon-object border-success text-success"><i className="icon-plus3"></i></div>
  70. <h5 className="content-group">{constants.register_form_title} <small className="display-block">{constants.register_form_subtitle}</small></h5>
  71. </div>
  72.  
  73. <div className="form-group has-feedback has-feedback-left">
  74. <input type="text" name="username" className="form-control" placeholder={constants.register_username_placeholder} />
  75. <div className="form-control-feedback">
  76. <i className="icon-user-check text-muted"></i>
  77. </div>
  78. </div>
  79.  
  80. <div className="form-group has-feedback has-feedback-left">
  81. <input type="password" name="password" className="form-control" placeholder={constants.register_password_placeholder} />
  82. <div className="form-control-feedback">
  83. <i className="icon-user-lock text-muted"></i>
  84. </div>
  85. </div>
  86.  
  87. <div className="form-group has-feedback has-feedback-left">
  88. <input type="text" name="email" className="form-control" placeholder={constants.register_email_placeholder} />
  89. <div className="form-control-feedback">
  90. <i className="icon-mention text-muted"></i>
  91. </div>
  92. </div>
  93.  
  94. <div className="content-divider text-muted form-group"><span>Additions</span></div>
  95.  
  96. <div className="form-group">
  97. <div className="checkbox">
  98. <label>
  99. <input type="checkbox" className="styled" />
  100. {constants.tos_txt.substring(0, constants.tos_txt.indexOf(" "))} <a href="#">{constants.tos_txt.substr(constants.tos_txt.indexOf(" ") + 1)}</a>
  101. </label>
  102. </div>
  103. </div>
  104. <button type="submit" className="btn bg-indigo-400 btn-block">Register <i className="icon-circle-right2 position-right"></i></button>
  105. </form>
  106. </div>
  107. )
  108. }
  109. }
  110. export default RegisterForm
  111.  
  112. var LocalStrategy = require('passport-local').Strategy;
  113. var User = require('../models/user');
  114. var bCrypt = require('bcrypt');
  115.  
  116. module.exports = function(passport){
  117.  
  118. passport.use('signup', new LocalStrategy({
  119. passReqToCallback : true // allows us to pass back the entire request to the callback
  120. },
  121. function(req, username, password, done) {
  122. console.log("In Signup");
  123. findOrCreateUser = function(){
  124. // find a user in Mongo with provided username
  125. User.findOne({ 'username' : username }, function(err, user) {
  126. // In case of any error, return using the done method
  127. if (err){
  128. console.log('Error in SignUp: '+err);
  129. return done(err);
  130. }
  131. // already exists
  132. if (user) {
  133. console.log('User already exists with username: '+username);
  134. return done(null, false, req.flash('message','User Already Exists'));
  135. } else {
  136. // if there is no user with that email
  137. // create the user
  138. var newUser = new User();
  139.  
  140. // set the user's local credentials
  141. newUser.username = username;
  142. newUser.password = createHash(password);
  143. newUser.email = req.param('email');
  144. newUser.firstName = "firstName";
  145. newUser.lastName = "lastName";
  146.  
  147. // save the user
  148. newUser.save(function(err) {
  149. if (err){
  150. console.log('Error in Saving user: '+err);
  151. throw err;
  152. }
  153. console.log('User Registration succesful');
  154. return done(null, newUser);
  155. });
  156. }
  157. });
  158. };
  159. // Delay the execution of findOrCreateUser and execute the method
  160. // in the next tick of the event loop
  161. process.nextTick(findOrCreateUser);
  162. })
  163. );
  164.  
  165. // Generates hash using bCrypt
  166. var createHash = function(password){
  167. return bCrypt.hashSync(password, bCrypt.genSaltSync(10));
  168. }
  169.  
  170. }
Add Comment
Please, Sign In to add comment