auth.controller (1).js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // ./api/auth/auth.controller.js
  2. import _ from "lodash";
  3. import slugify from "slugify";
  4. import AccountService from "../accounts/account.service.js";
  5. import AccountValidator from "../accounts/account.validator.js";
  6. import UserValidator from "../users/user.validator.js";
  7. import AuthService from "./auth.service.js";
  8. class Controller {
  9. async signup(req, res, next) {
  10. const accountData = _.pick(req.body, [
  11. "subdomain",
  12. "privacyAccepted",
  13. "marketingAccepted",
  14. ]);
  15. accountData.subdomain = slugify(accountData.subdomain, {
  16. replacement: "-",
  17. lower: true,
  18. strict: true,
  19. trim: true,
  20. });
  21. const accountErrors = await AccountValidator.onSignup(accountData);
  22. if (accountErrors) {
  23. return res.status(422).json({
  24. success: false,
  25. errors: accountErrors.details,
  26. });
  27. }
  28. const userData = _.pick(req.body, ["email", "password"]);
  29. userData.language = req.body.language || process.env.DEFAULT_LOCALE;
  30. const userErrors = await UserValidator.onSignup(userData);
  31. if (userErrors) {
  32. return res.status(422).json({
  33. success: false,
  34. errors: userErrors.details,
  35. });
  36. }
  37. console.log(
  38. "SIGNUP_WITH_ACTIVATE ->",
  39. process.env.SIGNUP_WITH_ACTIVATE,
  40. process.env.SIGNUP_WITH_ACTIVATE === "true",
  41. process.env.SIGNUP_WITH_ACTIVATE === "false"
  42. );
  43. if (process.env.SIGNUP_WITH_ACTIVATE === "true") {
  44. const signupResponse = await AuthService.signupWithActivate(
  45. accountData,
  46. userData
  47. );
  48. return res.json(signupResponse);
  49. } else {
  50. const signupResponse = await AuthService.signup(accountData, userData);
  51. return res.json(signupResponse);
  52. }
  53. }
  54. async manualSignup(req, res, next) {
  55. const accountData = _.pick(req.body, ["subdomain"]);
  56. accountData.subdomain = slugify(accountData.subdomain);
  57. const userData = _.pick(req.body, ["email", "password"]);
  58. userData.active = true;
  59. const { account, user } = await AuthService.signup(accountData, userData);
  60. const accountCompanyData = _.pick(req.body, [
  61. "companyName",
  62. "companyVat",
  63. "companyBillingAddress",
  64. "companySdi",
  65. "companyPhone",
  66. "companyEmail",
  67. "companyPec",
  68. ]);
  69. await AccountService.update(account.id, accountCompanyData);
  70. await AccountService.activate(user);
  71. return res.json({
  72. success: true,
  73. message: "created",
  74. });
  75. }
  76. async activateAccount(req, res) {
  77. const userErrors = await UserValidator.onActivate(req.body);
  78. if (userErrors) {
  79. return res.status(422).json({
  80. success: false,
  81. errors: userErrors.details,
  82. });
  83. }
  84. const user = await AuthService.activate(req.body.token, req.body.email);
  85. if (!user) {
  86. return res.status(404).json({
  87. success: false,
  88. message: "Failed to activate account - No account found",
  89. });
  90. }
  91. return res.json({
  92. success: true,
  93. });
  94. }
  95. async resendActivation(req, res) {
  96. const errors = await UserValidator.onResendActivation(req.body);
  97. if (errors) {
  98. return res.status(422).json({
  99. success: false,
  100. errors: errors.details,
  101. });
  102. }
  103. const user = await AuthService.resendActivation(req.body.email);
  104. if (!user) {
  105. return res.status(404).json({
  106. success: false,
  107. message: "Account not found or already activated",
  108. });
  109. }
  110. return res.json({
  111. success: true,
  112. });
  113. }
  114. async login(req, res) {
  115. const error = await UserValidator.onLogin(req.body);
  116. if (error) {
  117. return res.status(422).json(error.details);
  118. }
  119. const token = await AuthService.login(req.body.email, req.body.password);
  120. if (token) {
  121. return res.json({
  122. success: true,
  123. message: "Enjoy your tokens!",
  124. token: token,
  125. });
  126. } else {
  127. return res.status(401).json({ message: "Email or password invalid" });
  128. }
  129. }
  130. async forgotPassword(req, res, next) {
  131. const errors = await UserValidator.forgotPassword(req.body);
  132. if (errors) {
  133. return res.status(422).json({
  134. success: false,
  135. message: "Please use a valid e-mail address!",
  136. errrors: errors.details,
  137. });
  138. }
  139. const user = await AuthService.forgotPassword(req.body.email);
  140. if (!user) {
  141. return res.status(404).json({ message: "Email not found" });
  142. }
  143. return res.json({
  144. success: true,
  145. message: "We sent You an email with link to change Your password.",
  146. });
  147. }
  148. async resetPassword(req, res, next) {
  149. const errors = await UserValidator.onResetPassword(req.body);
  150. if (errors) {
  151. return res.status(422).json({
  152. success: false,
  153. message: "Failed to update password",
  154. errors: errors.details,
  155. });
  156. }
  157. const done = await AuthService.resetPassword(
  158. req.body.passwordResetToken,
  159. req.body.password,
  160. req.body.email
  161. );
  162. if (done) {
  163. return res.json({
  164. success: true,
  165. message: "Successfully changed password!",
  166. });
  167. } else {
  168. return res.status(422).json({
  169. success: false,
  170. message: "Failed to find user with the provided reset password token.",
  171. });
  172. }
  173. }
  174. async refreshToken(req, res) {
  175. const token = await AuthService.login(req.user.email, null, true);
  176. if (token) {
  177. return res.json({
  178. success: true,
  179. message: "Enjoy your tokens!",
  180. token: token,
  181. });
  182. }
  183. }
  184. async deleteRefreshToken(req, res) {
  185. if (
  186. await AuthService.checkRefreshToken(req.body.email, req.body.refreshToken)
  187. ) {
  188. await AuthService.deleteToken(req.body.email);
  189. return res.json({
  190. success: true,
  191. message: "Refresh token deleted.",
  192. });
  193. }
  194. return res.status(401).json({ message: "Email or refresh token invalid" });
  195. }
  196. async ssoLogin(req, res) {
  197. const error = await UserValidator.onSso(req.body);
  198. if (error) {
  199. return res.status(422).json(error.details);
  200. }
  201. const tokens = await AuthService.ssoLogin(req.body.sso);
  202. if (tokens) {
  203. return res.json({
  204. success: true,
  205. message: "Enjoy your tokens!",
  206. token: tokens.token,
  207. refreshToken: tokens.refreshToken,
  208. });
  209. } else {
  210. return res.status(401).json({ message: "Email or sso invalid" });
  211. }
  212. }
  213. }
  214. export default new Controller();