index.js 2.2 KB

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