user.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. const UserModel = require('../model/user');
  2. const fs = require('fs').promises;
  3. const path = require('path');
  4. const Jimp = require('jimp');
  5. const jwt = require('jsonwebtoken');
  6. const createFolderIsExist = require('../helpers/create-directory');
  7. require('dotenv').config();
  8. const client = require('../helpers/twilio');
  9. const phoneToken = require('generate-sms-verification-code');
  10. const SECRET_KEY = process.env.JWT_SECRET;
  11. const saveAvatarForStatic = async (req, res, next) => {
  12. try {
  13. const userId = req.user.id;
  14. const DIR_IMAGES = process.env.DIR_IMAGES;
  15. const pathToFile = req.file.path;
  16. const newNameAvatar = req.file.originalname;
  17. const img = await Jimp.read(pathToFile);
  18. await img
  19. .autocrop()
  20. .cover(
  21. 250,
  22. 250,
  23. Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE
  24. )
  25. .writeAsync(pathToFile);
  26. await createFolderIsExist(path.join(DIR_IMAGES, userId));
  27. await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameAvatar));
  28. const newUrlAvatar = path.normalize(path.join(userId, newNameAvatar));
  29. const newUrl = `http://localhost:3000/${userId}/${newNameAvatar}`;
  30. await UserModel.updateAvatar(userId, newUrlAvatar);
  31. return res.status(200).json({
  32. status: 'success',
  33. code: 200,
  34. data: {
  35. newUrl,
  36. },
  37. });
  38. } catch (e) {
  39. next(e);
  40. }
  41. };
  42. const createNewUser = async (req, res, next) => {
  43. try {
  44. const code = phoneToken(4, { type: 'number' });
  45. const { number, country } = req.body;
  46. const isExist = await UserModel.findByNumber(number);
  47. if (isExist) {
  48. const id = isExist._id;
  49. await UserModel.updateCode(id, code);
  50. } else {
  51. await UserModel.createUser({ number, country, code });
  52. }
  53. client.messages.create({
  54. body: `${code}`,
  55. to: `${number}`,
  56. from: '+18305875860',
  57. });
  58. return res.status(201).json({
  59. status: 'success',
  60. code: 201,
  61. data: String(code),
  62. });
  63. } catch (e) {
  64. next(e);
  65. }
  66. };
  67. const logIn = async (req, res, next) => {
  68. try {
  69. const { number, code } = req.body;
  70. const user = await UserModel.findByNumber(number);
  71. if (!user || user.code !== code)
  72. return res.status(401).json({
  73. status: 'error',
  74. code: 401,
  75. data: 'UNAUTHORIZED',
  76. message: 'Invalid credentials',
  77. });
  78. const id = user._id;
  79. const payload = { id };
  80. const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
  81. const registration = user.name? false: true
  82. await UserModel.updateToken(id, token);
  83. await UserModel.updateCode(id, '');
  84. return res.status(200).json({
  85. status: 'success',
  86. code: 200,
  87. data: {
  88. token,
  89. registration
  90. },
  91. });
  92. } catch (e) {
  93. next(e);
  94. }
  95. };
  96. const logOut = async (req, res, next) => {
  97. try {
  98. const id = req.user.id;
  99. const user = await UserModel.findById(id);
  100. if (!user)
  101. return res.status(401).json({
  102. status: 'error',
  103. code: 401,
  104. data: 'UNAUTHORIZED',
  105. message: 'Invalid credentials',
  106. });
  107. await UserModel.updateToken(id, null);
  108. return res.status(204).json({});
  109. } catch (e) {
  110. next(e);
  111. }
  112. };
  113. const getCurrent = async (req, res, next) => {
  114. try {
  115. const user = req.user;
  116. if (!user)
  117. return res.status(401).json({
  118. status: 'error',
  119. code: 401,
  120. data: 'UNAUTHORIZED',
  121. message: 'Invalid credentials',
  122. });
  123. return res.status(200).json({
  124. status: 'success',
  125. code: 200,
  126. data: {
  127. user,
  128. },
  129. });
  130. } catch (e) {
  131. next(e);
  132. }
  133. };
  134. const updateCredentials = async (req, res, next) => {
  135. try {
  136. const id = req.user.id;
  137. const user = await UserModel.updateCredentials(id, req.body);
  138. return res.status(200).json({
  139. data: {
  140. user},
  141. });
  142. } catch (e) {
  143. next(e);
  144. }
  145. };
  146. module.exports = {
  147. saveAvatarForStatic,
  148. createNewUser,
  149. logIn,
  150. logOut,
  151. getCurrent,
  152. updateCredentials,
  153. };