index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.render('index', { community: config.community,
  7. tokenRequired: !!config.inviteToken });
  8. });
  9. router.post('/invite', function(req, res) {
  10. if (req.body.email && (!config.inviteToken || (!!config.inviteToken && req.body.token === config.inviteToken))) {
  11. request.post({
  12. url: 'https://'+ config.slackUrl + '/api/users.admin.invite',
  13. form: {
  14. email: req.body.email,
  15. token: config.slacktoken,
  16. set_active: true
  17. }
  18. }, function(err, httpResponse, body) {
  19. // body looks like:
  20. // {"ok":true}
  21. // or
  22. // {"ok":false,"error":"already_invited"}
  23. if (err) { return res.send('Error:' + err); }
  24. body = JSON.parse(body);
  25. if (body.ok) {
  26. res.render('result', {
  27. community: config.community,
  28. message: 'Success! Check "'+ req.body.email +'" for an invite from Slack.'
  29. });
  30. } else {
  31. var error = body.error;
  32. if (error === 'already_invited' || error === 'already_in_team') {
  33. res.render('result', {
  34. community: config.community,
  35. message: 'Success! You were already invited.<br>' +
  36. 'Visit <a href="https://'+ config.slackUrl +'">'+ config.community +'</a>'
  37. });
  38. return;
  39. } else if (error === 'invalid_email') {
  40. error = 'The email you entered is an invalid email.'
  41. } else if (error === 'invalid_auth') {
  42. error = 'Something has gone wrong. Please contact a system administrator.'
  43. }
  44. res.render('result', {
  45. community: config.community,
  46. message: 'Failed! ' + error,
  47. isFailed: true
  48. });
  49. }
  50. });
  51. } else {
  52. var errMsg = [];
  53. if (!req.body.email) {
  54. errMsg.push('your email is required');
  55. }
  56. if (!!config.inviteToken) {
  57. if (!req.body.token) {
  58. errMsg.push('valid token is required');
  59. }
  60. if (req.body.token && req.body.token !== config.inviteToken) {
  61. errMsg.push('the token you entered is wrong');
  62. }
  63. }
  64. res.render('result', {
  65. community: config.community,
  66. message: 'Failed! ' + errMsg.join(' and ') + '.',
  67. isFailed: true
  68. });
  69. }
  70. });
  71. module.exports = router;