index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import bodyParser from "body-parser";
  2. import express from "express";
  3. import dotenv from "dotenv";
  4. import cors from "cors";
  5. import {
  6. CoreComplexClient,
  7. setServerURL
  8. } from "core-complex-client";
  9. dotenv.config();
  10. const app = express();
  11. app.use(bodyParser.json({ limit: "30mb", extended: true }));
  12. app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
  13. app.use(cors());
  14. setServerURL(process.env.SERVER_URL);
  15. const coreClient = new CoreComplexClient({
  16. rsaPublicKey: process.env.PUBLIC_KEY,
  17. privateKeyID: process.env.PRIVATE_KEY_ID,
  18. privateKey: process.env.PRIVATE_KEY,
  19. appID: process.env.APP_ID
  20. });
  21. app.get("/healthCheck", (req, res) => {
  22. res.status(200).json("It works!");
  23. });
  24. //NAuth
  25. app.post("/nauth/changePassword", async (req, res) => {
  26. await coreClient.NAuth.changePassword({
  27. newPassword: req.body.newPassword,
  28. oldPassword: req.body.oldPassword
  29. }).then((resp) => {
  30. res.status(200).json(resp);
  31. }).catch((err) => {
  32. console.log("err:", err)
  33. res.status(400).json("something-went-wrong");
  34. });
  35. });
  36. app.post("/nauth/login", async (req, res) => {
  37. await coreClient.NAuth.login({
  38. mail: req.body.mail,
  39. password: req.body.password
  40. }).then((resp) => {
  41. res.status(200).json(resp);
  42. }).catch((err) => {
  43. console.log("err:", err)
  44. res.status(400).json("something-went-wrong");
  45. });
  46. });
  47. app.post("/nauth/mailVerify", async (req, res) => {
  48. await coreClient.NAuth.mailVerify({
  49. hash: req.body.hash,
  50. code: req.body.code
  51. }).then((resp) => {
  52. res.status(200).json(resp);
  53. }).catch((err) => {
  54. console.log("err:", err)
  55. res.status(400).json("something-went-wrong");
  56. });
  57. });
  58. app.get("/nauth/me", async (req, res) => {
  59. await coreClient.NAuth.me()
  60. .then((resp) => {
  61. res.status(200).json(resp);
  62. }).catch((err) => {
  63. console.log("err:", err)
  64. res.status(400).json("something-went-wrong");
  65. });
  66. });
  67. app.post("/nauth/refreshToken", async (req, res) => {
  68. await coreClient.NAuth.refreshToken({
  69. token: req.body.token
  70. }).then((resp) => {
  71. res.status(200).json(resp);
  72. }).catch((err) => {
  73. console.log("err:", err)
  74. res.status(400).json("something-went-wrong");
  75. });
  76. });
  77. app.post("/nauth/register", async (req, res) => {
  78. await coreClient.NAuth.register({
  79. isApproveContracts: req.body.isApproveContracts,
  80. phoneAreaCode: req.body.phoneAreaCode,
  81. firstName: req.body.firstName,
  82. password: req.body.password,
  83. lastName: req.body.lastName,
  84. userName: req.body.userName,
  85. language: req.body.language,
  86. phone: req.body.phone,
  87. mail: req.body.mail
  88. }).then((resp) => {
  89. res.status(200).json(resp);
  90. }).catch((err) => {
  91. console.log("err:", err)
  92. res.status(400).json("something-went-wrong");
  93. });
  94. });
  95. app.post("/nauth/reSendCodeSMSOTP", async (req, res) => {
  96. await coreClient.NAuth.reSendCodeSMSOTP({
  97. mail: req.body.mail
  98. }).then((resp) => {
  99. res.status(200).json(resp);
  100. }).catch((err) => {
  101. console.log("err:", err)
  102. res.status(400).json("something-went-wrong");
  103. });
  104. });
  105. app.post("/nauth/resetNewPassword", async (req, res) => {
  106. await coreClient.NAuth.resetNewPassword({
  107. newPassword: req.body.newPassword,
  108. mail: req.body.mail,
  109. code: req.body.code
  110. }).then((resp) => {
  111. res.status(200).json(resp);
  112. }).catch((err) => {
  113. console.log("err:", err)
  114. res.status(400).json("something-went-wrong");
  115. });
  116. });
  117. app.post("/nauth/resetPasswordCheckCode", async (req, res) => {
  118. await coreClient.NAuth.resetPasswordCheckCode({
  119. mail: req.body.mail,
  120. code: req.body.code
  121. }).then((resp) => {
  122. res.status(200).json(resp);
  123. }).catch((err) => {
  124. console.log("err:", err)
  125. res.status(400).json("something-went-wrong");
  126. });
  127. });
  128. app.post("/nauth/smsOTP", async (req, res) => {
  129. await coreClient.NAuth.smsOTP({
  130. mail: req.body.mail,
  131. code: req.body.code
  132. }).then((resp) => {
  133. res.status(200).json(resp);
  134. }).catch((err) => {
  135. console.log("err:", err)
  136. res.status(400).json("something-went-wrong");
  137. });
  138. });
  139. app.post("/nauth/startMailVerify", async (req, res) => {
  140. await coreClient.NAuth.startMailVerify({
  141. mail: req.body.mail
  142. }).then((resp) => {
  143. res.status(200).json(resp);
  144. }).catch((err) => {
  145. console.log("err:", err)
  146. res.status(400).json("something-went-wrong");
  147. });
  148. });
  149. app.post("/nauth/startResetPassword", async (req, res) => {
  150. await coreClient.NAuth.startResetPassword({
  151. mail: req.body.mail
  152. }).then((resp) => {
  153. res.status(200).json(resp);
  154. }).catch((err) => {
  155. console.log("err:", err)
  156. res.status(400).json("something-went-wrong");
  157. });
  158. });
  159. //NChar
  160. app.post("/nchar/addPhoneNumber", async (req, res) => {
  161. await coreClient.NChar.addPhoneNumber({
  162. phoneAreaCode: req.body.phoneAreaCode,
  163. phone: req.body.phone
  164. }).then((resp) => {
  165. res.status(200).json(resp);
  166. }).catch((err) => {
  167. console.log("err:", err)
  168. res.status(400).json("something-went-wrong");
  169. });
  170. });
  171. app.post("/nchar/checkChangeMailCode", async (req, res) => {
  172. await coreClient.NChar.checkChangeMailCode({
  173. mail: req.body.mail,
  174. code: req.body.code
  175. }).then((resp) => {
  176. res.status(200).json(resp);
  177. }).catch((err) => {
  178. console.log("err:", err)
  179. res.status(400).json("something-went-wrong");
  180. });
  181. });
  182. app.post("/nchar/deletePhoneNumber", async (req, res) => {
  183. await coreClient.NChar.deletePhoneNumber({
  184. phoneAreaCode: req.body.phoneAreaCode,
  185. phone: req.body.phone
  186. }).then((resp) => {
  187. res.status(200).json(resp);
  188. }).catch((err) => {
  189. console.log("err:", err)
  190. res.status(400).json("something-went-wrong");
  191. });
  192. });
  193. app.get("/nchar/me", async (req, res) => {
  194. await coreClient.NChar.me()
  195. .then((resp) => {
  196. res.status(200).json(resp);
  197. }).catch((err) => {
  198. console.log("err:", err)
  199. res.status(400).json("something-went-wrong");
  200. });
  201. });
  202. app.post("/nchar/setPrimaryPhoneNumber", async (req, res) => {
  203. await coreClient.NChar.setPrimaryPhoneNumber({
  204. phoneAreaCode: req.body.phoneAreaCode,
  205. phone: req.body.phone
  206. }).then((resp) => {
  207. res.status(200).json(resp);
  208. }).catch((err) => {
  209. console.log("err:", err)
  210. res.status(400).json("something-went-wrong");
  211. });
  212. });
  213. app.post("/nchar/startChangeMail", async (req, res) => {
  214. await coreClient.NChar.startChangeMail({
  215. mail: req.body.mail
  216. }).then((resp) => {
  217. res.status(200).json(resp);
  218. }).catch((err) => {
  219. console.log("err:", err)
  220. res.status(400).json("something-went-wrong");
  221. });
  222. });
  223. const PORT = process.env.PORT || 5000;
  224. app.listen(PORT, () => {
  225. console.log("Server is running on port " + PORT);
  226. });