Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. const userQueries = require("../db/queries.users.js");
  2. const passport = require("passport");
  3. const sgMail = require('@sendgrid/mail');
  4.  
  5. sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  6.  
  7. module.exports = {
  8. signUp(req, res, next){
  9. res.render("users/signup");
  10. },
  11.  
  12. create(req, res, next){
  13. let newUser = {
  14. email: req.body.email,
  15. password: req.body.password,
  16. passwordConfirmation: req.body.passwordConfirmation
  17. };
  18. userQueries.createUser(newUser, (err, user) => {
  19. if(err){
  20. req.flash("error", err);
  21. res.redirect("/users/signup");
  22. } else {
  23. const msg = {
  24. to: req.body.email,
  25. from: 'welcome@blocipedia.com',
  26. subject: 'Welcome to Blocipedia!',
  27. text: 'Get ready to learn and collaborate',
  28. html: 'Get ready to learn and collaborate',
  29. };
  30. sgMail.send(msg);
  31. passport.authenticate("local")(req, res, () => {
  32. req.flash("notice", "You've successfully signed in!");
  33. res.redirect("/");
  34. })
  35.  
  36. }
  37. });
  38. const msg = {
  39. to: req.body.email,
  40. from: 'welcome@blocipedia.com',
  41. subject: 'Welcome to Blocipedia!',
  42. text: 'Get ready to learn and collaborate',
  43. html: 'Get ready to learn and collaborate',
  44. };
  45. sgMail.send(msg);
  46. },
  47.  
  48. signInForm(req, res, next){
  49. res.render("users/signin");
  50. },
  51.  
  52. signIn(req, res, next){
  53. passport.authenticate("local")(req, res, function () {
  54. if(!req.user){
  55. req.flash("notice", "Sign in failed. Please try again.")
  56. res.redirect("/users/signin");
  57. } else {
  58. req.flash("notice", "You've successfully signed in!");
  59. res.redirect("/");
  60. }
  61. })
  62. },
  63.  
  64. signOut(req, res, next){
  65. req.logout();
  66. req.flash("notice", "You've successfully signed out!");
  67. res.redirect("/");
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement