Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import express from 'express'
  2. import Sequelize from 'sequelize'
  3. const app = express()
  4. const port = 3000
  5. import passport from 'passport'
  6. import LocalStrategy from 'passport-local'
  7. import session from 'express-session'
  8. import bcrypt from 'bcrypt'
  9. const saltrounds = 10
  10. import bodyParser from 'body-parser'
  11. import flash from 'connect-flash'
  12. app.use(session({
  13. secret: 'keyboard cat',
  14. resave: false,
  15. saveUninitialized: true
  16. }))
  17. app.use(passport.initialize())
  18. app.use(passport.session())
  19. app.use(bodyParser.json());
  20. app.use(bodyParser.urlencoded({
  21. extended: true
  22. }));
  23. app.use(flash())
  24. //
  25. //Removed database connection and other model for brevity
  26. //
  27. const User = sequelize.define('user', {
  28. name: {
  29. type: Sequelize.STRING
  30. },
  31. username: {
  32. type: Sequelize.STRING
  33. },
  34. hash: {
  35. type: Sequelize.STRING
  36. },
  37. id: {
  38. type: Sequelize.UUID,
  39. defaultValue: Sequelize.UUIDV4,
  40. primaryKey: true
  41. },
  42. googleid: {
  43. type: Sequelize.STRING
  44. }
  45. })
  46. User.hashpassword = function (value) {
  47. bcrypt.genSalt(saltrounds, (err, salt) => {
  48. bcrypt.hash(value, salt, (err, hash) => {
  49. return hash;
  50. })
  51. })
  52. }
  53. User.authenticate = function (value, hash) {
  54. if(bcrypt.compare(value, hash)){
  55. console.log(hash)
  56. return true;
  57. }
  58. else
  59. return false;
  60. }
  61. passport.use('local-login', new LocalStrategy((username, password, cb) => {
  62. User.findOne({where:{username:username}}).then((err, result)=> {
  63. if (err) return cb(err)
  64. if (result != undefined) {
  65. if (User.authenticate(password, result.password)) {
  66. cb(null, user)
  67. }
  68. else cb(null, false, { message: 'Incorrect password' })
  69. }
  70. else cb(null, false, { message: 'Incorrect username.' })
  71. })
  72. }))
  73.  
  74. passport.serializeUser((user, done) => {
  75. done(null, user.id)
  76. })
  77. passport.deserializeUser((id, cb) => {
  78. User.findOne({where:{id:id}}.then((err, result) => {
  79. if (err) return cb(err)
  80. else cb(null, result.id)
  81. }))
  82. })
  83. //
  84. //Removed DB sync for brevity
  85. //
  86. function isLoggedIn(req, res, next) {
  87. console.log(req.isAuthenticated())
  88. console.log(req.session)
  89. if (req.isAuthenticated())
  90. return next();
  91. res.status(400).json({
  92. 'message': 'access denied'
  93. });
  94. }
  95. app.post('/login', (req, res, next) => {
  96. console.log(req.body)
  97. passport.authenticate('local-login', {failureFlash: true }, ()=> {
  98. req.session.save(() => {
  99. res.send(req.isAuthenticated()) //false
  100. })
  101.  
  102. })(req,res,next)
  103. })
  104.  
  105. app.get('/', isLoggedIn, (req, res) => {
  106. //
  107. //home logic
  108. //
  109. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement