server.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const app = require('./app')
  2. const db = require('./db')
  3. const log = require('./log')
  4. const { hashPassword, genSeed, genKey, authorize, createToken } = require('./security')
  5. const { sendmail } = require('./mail')
  6. app.post('/signup', async (req, res) => {
  7. if (!req.body.name) throw new Error('Missing name')
  8. if (!req.body.password) throw new Error('Missing password')
  9. if (!req.body.email) throw new Error('Missing email')
  10. const email = req.body.email.toLowerCase().trim()
  11. const existing = await db.users.get(email)
  12. if (existing && existing.confirmed) throw new Error('User already exists.')
  13. const seed = genSeed()
  14. const password = hashPassword({password: req.body.password, email, seed})
  15. const confirmKey = genKey()
  16. const user = {
  17. name: req.body.name,
  18. email: req.body.email,
  19. password,
  20. seed,
  21. ip: req.ip,
  22. confirmed: false,
  23. confirmKey,
  24. keyCreated: Date.now(),
  25. failCount: 0,
  26. created: Date.now()
  27. }
  28. const mailResult = await sendmail({
  29. to: `${user.name} <${user.email}>`,
  30. from: `RSS Unlimited <noreply@rssunlimited.com>`,
  31. subject: `Please verify your email address.`,
  32. text: `Enter the following code to confirm your email address on RSSUnlimited.com:\n\n${confirmKey}`
  33. })
  34. await db.users.put(email, user)
  35. log({
  36. type: 'user-created',
  37. user
  38. })
  39. res.status(200).send({})
  40. })
  41. app.post('/confirm', async (req, res) => {
  42. if (!req.body.confirmKey) throw new Error('Missing confirmKey')
  43. if (!req.body.email) throw new Error('Missing email')
  44. const email = req.body.email.toLowerCase().trim()
  45. const user = await db.users.get(email)
  46. if (user && !user.confirmed && user.confirmKey.toUpperCase() === req.body.confirmKey.toUpperCase().trim()) {
  47. user.confirmed = true
  48. db.users.put(email, user)
  49. res.status(200).send({
  50. token: createToken(user)
  51. })
  52. } else {
  53. res.status(400).send({
  54. error: 'Something went wrong :('
  55. })
  56. }
  57. })
  58. app.post('/login', async (req, res) => {
  59. if (!req.body.password) throw new Error('Missing password')
  60. if (!req.body.email) throw new Error('Missing email')
  61. const email = req.body.email.toLowerCase().trim()
  62. const user = await db.users.get(email)
  63. if (user) {
  64. const password = hashPassword({
  65. password: req.body.password,
  66. seed: user.seed,
  67. email
  68. })
  69. if (password === user.password) {
  70. log({ type: 'login', user})
  71. res.status(200).send({
  72. token: createToken(user)
  73. })
  74. return
  75. }
  76. }
  77. res.status(400).send({
  78. error: 'Login failed'
  79. })
  80. })
  81. app.post('/renew', authorize(), async (req, res) => {
  82. log({
  83. type: 'renew',
  84. user: req.identity
  85. })
  86. const identity = {...req.identity}
  87. delete identity.eat
  88. delete identity.iat
  89. res.status(200).send({
  90. token: createToken(identity)
  91. })
  92. })
  93. app.start()