index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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') {
  33. error = 'You are already invited.'
  34. } else if (error === 'invalid_email') {
  35. error = 'The email you entered is an invalid email.'
  36. }
  37. res.render('result', {
  38. community: config.community,
  39. message: 'Failed! ' + error
  40. });
  41. }
  42. });
  43. } else {
  44. var errMsg = [];
  45. if (!req.body.email) {
  46. errMsg.push('email is required');
  47. }
  48. if (!!config.inviteToken) {
  49. if (!req.body.token) {
  50. errMsg.push('token is required');
  51. }
  52. if (req.body.token && req.body.token !== config.inviteToken) {
  53. errMsg.push('token is wrong');
  54. }
  55. }
  56. res.render('result', {
  57. community: config.community,
  58. message: 'Failed! ' + errMsg.join(' and ') + '.'
  59. });
  60. }
  61. });
  62. module.exports = router;