Guest User

Untitled

a guest
Oct 19th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. /**
  2. * @author Alexi Jamal
  3. */
  4.  
  5.  
  6.  
  7. //const app = require('express')();
  8.  
  9. //Packages requires
  10. const express = require('express');
  11. const bodyPar = require('body-parser');
  12. const session = require('express-session');
  13. const cms = require('connect-mongodb-session');
  14. const mongoose = require('mongoose');
  15. const routerActions = require('./routes/router');
  16.  
  17. //Creating packages objects
  18. const app = express();
  19. const mongoStore = cms(session);
  20. const urlEncodedBodyParser = bodyPar.urlencoded({ extended: false });
  21.  
  22. mongoose.set('useNewUrlParser', true);
  23. mongoose.set('useCreateIndex', true);
  24. mongoose.set('useFindAndModify', false);
  25.  
  26. DATABASE_NAME = "midterm"
  27. mongoose.connect(`mongodb://localhost:27017/${DATABASE_NAME}`);
  28.  
  29. const mongoConnection = mongoose.connection;
  30.  
  31. module.exports = mongoConnection
  32.  
  33. app.use(session({
  34. secret: 'testing',
  35. cookie: {
  36. maxAge: 1000 * 60 * 60 * 24 * 7
  37. },
  38. resave: true,
  39. saveUninitialized: false,
  40. store: new mongoStore({
  41. uri: `mongodb://localhost:27017/${DATABASE_NAME}`,
  42. collection: 'sessions'
  43. })
  44. }));
  45.  
  46. app.use("/", express.static("public"));
  47.  
  48. app.use('/user', routerActions);
  49.  
  50. const showProfile = async (req, res) => {
  51.  
  52. const user = await User.findById(req.session.userId);
  53.  
  54. if (user === null) {
  55. res.status(403).send('Forbidden');
  56. return;
  57. }
  58.  
  59. var html = "Welcome " + user.name + " to your profile!!\nClick <a href='logout'>here</a> to logout."
  60. res.status(200).send(html);
  61. return;
  62. }
  63.  
  64. const logout = async (req, res) => {
  65. if (req.session) await req.session.destroy();
  66. res.redirect('/');
  67. return;
  68. }
  69.  
  70. const register = (req, res) => {
  71.  
  72. if (req.body.username && req.body.password && req.body.name) {
  73.  
  74. (async () => {
  75.  
  76. const password = await bcrypt.hash(req.body.password, 10);
  77.  
  78. const user = new User({
  79. _id: mongoose.Types.ObjectId(),
  80. name: req.body.name,
  81. username: req.body.username,
  82. password: password
  83. });
  84.  
  85. await user.validate();
  86. await user.save();
  87. req.session.userId = user._id;
  88. res.redirect('/profile');
  89. return;
  90.  
  91. })().catch((error) => { res.status(400).send('Bad request.'); });
  92. }
  93. else { res.status(400).send('Bad request.'); }
  94. }
  95.  
  96. const login = (req, res) => {
  97.  
  98. if (req.body.username && req.body.password) {
  99.  
  100. console.log(req.body.username)
  101.  
  102. User.authenticate(req.body.username, req.body.password).then((user) => {
  103. if (user !== null) {
  104. req.session.userId = user._id;
  105. res.redirect('/profile');
  106. return;
  107. }
  108. else { res.status(401).send('Login failed.'); }
  109. });
  110. }
  111. else { res.status(401).send('Login failed.'); }
  112.  
  113. return;
  114. }
  115.  
  116. app.post('/register', urlEncodedBodyParser, [register]);
  117.  
  118. app.post('/login', urlEncodedBodyParser, [login]);
  119.  
  120. app.get('/profile', [showProfile]);
  121.  
  122. app.get('/logout', [logout]);
  123.  
  124. let port = process.env.PORT || 3000;
  125. app.listen(port, () => {
  126. console.log("Listening on port "+port);
  127. })
Add Comment
Please, Sign In to add comment