index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/startResetPassword", async (req, res) => {
  106. let params = {
  107. };
  108. if (req.body.mail) {
  109. params.mail = req.body.mail;
  110. }
  111. if (req.body.userName) {
  112. params.userName = req.body.userName;
  113. }
  114. if (req.body.phone) {
  115. const phone = {
  116. phoneAreaCode: req.body.phoneAreaCode,
  117. phone: req.body.phone
  118. }
  119. params.phone = phone;
  120. }
  121. await coreClient.NAuth.startResetPassword(params)
  122. .then((resp) => {
  123. res.status(200).json(resp);
  124. }).catch((err) => {
  125. console.log("err:", err)
  126. res.status(400).json("something-went-wrong");
  127. });
  128. });
  129. app.post("/nauth/confirmResetPasswordMethod", async (req, res) => {
  130. let params = {
  131. method: req.body.method
  132. };
  133. if (req.body.userName) {
  134. params.userName = req.body.userName;
  135. }
  136. if (req.body.mail) {
  137. params.mail = req.body.mail;
  138. }
  139. if (req.body.phone) {
  140. const phone = {
  141. phoneAreaCode: req.body.phoneAreaCode,
  142. phone: req.body.phone
  143. }
  144. params.phone = phone;
  145. }
  146. if (req.body.targetMail) {
  147. params.targetMail = req.body.targetMail;
  148. }
  149. if (req.body.targetPhone) {
  150. const target = {
  151. phoneAreaCode: req.body.targetPhoneAreaCode,
  152. phone: req.body.targetPhone
  153. }
  154. params.targetPhone = target
  155. }
  156. await coreClient.NAuth.confirmResetPasswordMethod(params)
  157. .then((resp) => {
  158. res.status(200).json(resp);
  159. }).catch((err) => {
  160. console.log("err:", err)
  161. res.status(400).json("something-went-wrong");
  162. });
  163. });
  164. app.post("/nauth/resetPasswordCheckCode", async (req, res) => {
  165. let params = {
  166. code: req.body.code
  167. };
  168. if (req.body.mail) {
  169. params.mail = req.body.mail;
  170. }
  171. if (req.body.userName) {
  172. params.userName = req.body.userName;
  173. }
  174. if (req.body.phone) {
  175. const phone = {
  176. phoneAreaCode: req.body.phoneAreaCode,
  177. phone: req.body.phone
  178. }
  179. params.phone = phone;
  180. }
  181. await coreClient.NAuth.resetPasswordCheckCode(params)
  182. .then((resp) => {
  183. res.status(200).json(resp);
  184. }).catch((err) => {
  185. console.log("err:", err)
  186. res.status(400).json("something-went-wrong");
  187. });
  188. });
  189. app.post("/nauth/resetNewPassword", async (req, res) => {
  190. let params = {
  191. newPassword: req.body.newPassword,
  192. code: req.body.code
  193. };
  194. if (req.body.mail) {
  195. params.mail = req.body.mail;
  196. }
  197. if (req.body.userName) {
  198. params.userName = req.body.userName;
  199. }
  200. if (req.body.phone) {
  201. const phone = {
  202. phoneAreaCode: req.body.phoneAreaCode,
  203. phone: req.body.phone
  204. }
  205. params.phone = phone;
  206. }
  207. await coreClient.NAuth.resetNewPassword(params)
  208. .then((resp) => {
  209. res.status(200).json(resp);
  210. }).catch((err) => {
  211. console.log("err:", err)
  212. res.status(400).json("something-went-wrong");
  213. });
  214. });
  215. app.post("/nauth/smsOTP", async (req, res) => {
  216. await coreClient.NAuth.smsOTP({
  217. mail: req.body.mail,
  218. code: req.body.code
  219. }).then((resp) => {
  220. res.status(200).json(resp);
  221. }).catch((err) => {
  222. console.log("err:", err)
  223. res.status(400).json("something-went-wrong");
  224. });
  225. });
  226. app.post("/nauth/startMailVerify", async (req, res) => {
  227. await coreClient.NAuth.startMailVerify({
  228. mail: req.body.mail
  229. }).then((resp) => {
  230. res.status(200).json(resp);
  231. }).catch((err) => {
  232. console.log("err:", err)
  233. res.status(400).json("something-went-wrong");
  234. });
  235. });
  236. //NChar
  237. app.post("/nchar/addPhoneNumber", async (req, res) => {
  238. await coreClient.NChar.addPhoneNumber({
  239. phoneAreaCode: req.body.phoneAreaCode,
  240. phone: req.body.phone
  241. }).then((resp) => {
  242. res.status(200).json(resp);
  243. }).catch((err) => {
  244. console.log("err:", err)
  245. res.status(400).json("something-went-wrong");
  246. });
  247. });
  248. app.post("/nchar/checkChangeMailCode", async (req, res) => {
  249. await coreClient.NChar.checkChangeMailCode({
  250. mail: req.body.mail,
  251. code: req.body.code
  252. }).then((resp) => {
  253. res.status(200).json(resp);
  254. }).catch((err) => {
  255. console.log("err:", err)
  256. res.status(400).json("something-went-wrong");
  257. });
  258. });
  259. app.post("/nchar/deletePhoneNumber", async (req, res) => {
  260. await coreClient.NChar.deletePhoneNumber({
  261. phoneAreaCode: req.body.phoneAreaCode,
  262. phone: req.body.phone
  263. }).then((resp) => {
  264. res.status(200).json(resp);
  265. }).catch((err) => {
  266. console.log("err:", err)
  267. res.status(400).json("something-went-wrong");
  268. });
  269. });
  270. app.get("/nchar/me", async (req, res) => {
  271. await coreClient.NChar.me()
  272. .then((resp) => {
  273. res.status(200).json(resp);
  274. }).catch((err) => {
  275. console.log("err:", err)
  276. res.status(400).json("something-went-wrong");
  277. });
  278. });
  279. app.post("/nchar/setPrimaryPhoneNumber", async (req, res) => {
  280. await coreClient.NChar.setPrimaryPhoneNumber({
  281. phoneAreaCode: req.body.phoneAreaCode,
  282. phone: req.body.phone
  283. }).then((resp) => {
  284. res.status(200).json(resp);
  285. }).catch((err) => {
  286. console.log("err:", err)
  287. res.status(400).json("something-went-wrong");
  288. });
  289. });
  290. app.post("/nchar/startChangeMail", async (req, res) => {
  291. await coreClient.NChar.startChangeMail({
  292. mail: req.body.mail
  293. }).then((resp) => {
  294. res.status(200).json(resp);
  295. }).catch((err) => {
  296. console.log("err:", err)
  297. res.status(400).json("something-went-wrong");
  298. });
  299. });
  300. app.post("/nchar/changeUserName", async (req, res) => {
  301. await coreClient.NChar.changeUserName({
  302. userName: req.body.userName
  303. }).then((resp) => {
  304. res.status(200).json(resp);
  305. }).catch((err) => {
  306. console.log("err:", err)
  307. res.status(400).json("something-went-wrong");
  308. });
  309. });
  310. app.get("/nchar/getPastUserNameChanges", async (req, res) => {
  311. await coreClient.NChar.getPastUserNameChanges()
  312. .then((resp) => {
  313. res.status(200).json(resp);
  314. }).catch((err) => {
  315. console.log("err:", err)
  316. res.status(400).json("something-went-wrong");
  317. });
  318. });
  319. app.post("/nchar/editUserProfile", async (req, res) => {
  320. let params = {};
  321. if (req.body.firstName) params.firstName = req.body.firstName;
  322. if (req.body.lastName) params.lastName = req.body.lastName;
  323. await coreClient.NChar.editUserProfile(params).then((resp) => {
  324. res.status(200).json(resp);
  325. }).catch((err) => {
  326. console.log("err:", err)
  327. res.status(400).json("something-went-wrong");
  328. });
  329. });
  330. app.post("/nchar/addMail", async (req, res) => {
  331. await coreClient.NChar.addMail({
  332. mail: req.body.mail
  333. }).then((resp) => {
  334. res.status(200).json(resp);
  335. }).catch((err) => {
  336. console.log("err:", err)
  337. res.status(400).json("something-went-wrong");
  338. });
  339. });
  340. app.post("/nchar/deleteMail", async (req, res) => {
  341. await coreClient.NChar.deleteMail({
  342. mail: req.body.mail
  343. }).then((resp) => {
  344. res.status(200).json(resp);
  345. }).catch((err) => {
  346. console.log("err:", err)
  347. res.status(400).json("something-went-wrong");
  348. });
  349. });
  350. const PORT = process.env.PORT || 5000;
  351. app.listen(PORT, () => {
  352. console.log("Server is running on port " + PORT);
  353. });