index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var express = require('express');
  2. var router = express.Router();
  3. var request = require('request');
  4. var config = require('../config');
  5. router.get('/', function(req, res) {
  6. res.setLocale(config.locale);
  7. res.render('index', { community: config.community,
  8. tokenRequired: !!config.inviteToken,
  9. recaptchaSiteKey: config.recaptchaSiteKey });
  10. });
  11. router.post('/invite', function(req, res) {
  12. if (req.body.email && (!config.inviteToken || (!!config.inviteToken && req.body.token === config.inviteToken))) {
  13. function doInvite() {
  14. request.post({
  15. url: 'https://'+ config.slackUrl + '/api/users.admin.invite',
  16. form: {
  17. email: req.body.email,
  18. token: config.slacktoken,
  19. set_active: true
  20. }
  21. }, function(err, httpResponse, body) {
  22. // body looks like:
  23. // {"ok":true}
  24. // or
  25. // {"ok":false,"error":"already_invited"}
  26. if (err) { return res.send('Error:' + err); }
  27. body = JSON.parse(body);
  28. if (body.ok) {
  29. res.render('result', {
  30. community: config.community,
  31. message: 'Success! Check “'+ req.body.email +'” for an invite from Slack.'
  32. });
  33. } else {
  34. var error = body.error;
  35. if (error === 'already_invited' || error === 'already_in_team') {
  36. res.render('result', {
  37. community: config.community,
  38. message: 'Success! You were already invited.<br>' +
  39. 'Visit <a href="https://'+ config.slackUrl +'">'+ config.community +'</a>'
  40. });
  41. return;
  42. } else if (error === 'invalid_email') {
  43. error = 'The email you entered is an invalid email.';
  44. } else if (error === 'invalid_auth') {
  45. error = 'Something has gone wrong. Please contact a system administrator.';
  46. }
  47. res.render('result', {
  48. community: config.community,
  49. message: 'Failed! ' + error,
  50. isFailed: true
  51. });
  52. }
  53. });
  54. }
  55. if (!!config.recaptchaSiteKey && !!config.recaptchaSecretKey) {
  56. request.post({
  57. url: 'https://www.google.com/recaptcha/api/siteverify',
  58. form: {
  59. response: req.body['g-recaptcha-response'],
  60. secret: config.recaptchaSecretKey
  61. }
  62. }, function(err, httpResponse, body) {
  63. if (typeof body === "string") {
  64. body = JSON.parse(body);
  65. }
  66. if (body.success) {
  67. doInvite();
  68. } else {
  69. error = 'Invalid captcha.';
  70. res.render('result', {
  71. community: config.community,
  72. message: 'Failed! ' + error,
  73. isFailed: true
  74. });
  75. }
  76. });
  77. } else {
  78. doInvite();
  79. }
  80. } else {
  81. var errMsg = [];
  82. if (!req.body.email) {
  83. errMsg.push('your email is required');
  84. }
  85. if (!!config.inviteToken) {
  86. if (!req.body.token) {
  87. errMsg.push('valid token is required');
  88. }
  89. if (req.body.token && req.body.token !== config.inviteToken) {
  90. errMsg.push('the token you entered is wrong');
  91. }
  92. }
  93. res.render('result', {
  94. community: config.community,
  95. message: 'Failed! ' + errMsg.join(' and ') + '.',
  96. isFailed: true
  97. });
  98. }
  99. });
  100. module.exports = router;