index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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.get("/nchar/me", async (req, res) => {
  238. await coreClient.NChar.me()
  239. .then((resp) => {
  240. res.status(200).json(resp);
  241. }).catch((err) => {
  242. console.log("err:", err)
  243. res.status(400).json("something-went-wrong");
  244. });
  245. });
  246. /* app.post("/nchar/startChangeMail", async (req, res) => {
  247. await coreClient.NChar.startChangeMail({
  248. mail: req.body.mail
  249. }).then((resp) => {
  250. res.status(200).json(resp);
  251. }).catch((err) => {
  252. console.log("err:", err)
  253. res.status(400).json("something-went-wrong");
  254. });
  255. });
  256. app.post("/nchar/checkChangeMailCode", async (req, res) => {
  257. await coreClient.NChar.checkChangeMailCode({
  258. mail: req.body.mail,
  259. code: req.body.code
  260. }).then((resp) => {
  261. res.status(200).json(resp);
  262. }).catch((err) => {
  263. console.log("err:", err)
  264. res.status(400).json("something-went-wrong");
  265. });
  266. }); */
  267. app.post("/nchar/addPhoneNumber", async (req, res) => {
  268. await coreClient.NChar.addPhoneNumber({
  269. phoneAreaCode: req.body.phoneAreaCode,
  270. phone: req.body.phone
  271. }).then((resp) => {
  272. res.status(200).json(resp);
  273. }).catch((err) => {
  274. console.log("err:", err)
  275. res.status(400).json("something-went-wrong");
  276. });
  277. });
  278. app.post("/nchar/deletePhoneNumber", async (req, res) => {
  279. await coreClient.NChar.deletePhoneNumber({
  280. phoneAreaCode: req.body.phoneAreaCode,
  281. phone: req.body.phone
  282. }).then((resp) => {
  283. res.status(200).json(resp);
  284. }).catch((err) => {
  285. console.log("err:", err)
  286. res.status(400).json("something-went-wrong");
  287. });
  288. });
  289. app.post("/nchar/setPrimaryPhoneNumber", async (req, res) => {
  290. await coreClient.NChar.setPrimaryPhoneNumber({
  291. phoneAreaCode: req.body.phoneAreaCode,
  292. phone: req.body.phone
  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/setPrimaryPhoneNumberCheckCode", async (req, res) => {
  301. await coreClient.NChar.setPrimaryPhoneNumberCheckCode({
  302. phoneAreaCode: req.body.phoneAreaCode,
  303. phone: req.body.phone,
  304. code: req.body.code
  305. }).then((resp) => {
  306. res.status(200).json(resp);
  307. }).catch((err) => {
  308. console.log("err:", err)
  309. res.status(400).json("something-went-wrong");
  310. });
  311. });
  312. app.post("/nchar/changeUserName", async (req, res) => {
  313. await coreClient.NChar.changeUserName({
  314. userName: req.body.userName
  315. }).then((resp) => {
  316. res.status(200).json(resp);
  317. }).catch((err) => {
  318. console.log("err:", err)
  319. res.status(400).json("something-went-wrong");
  320. });
  321. });
  322. app.get("/nchar/getPastUserNameChanges", async (req, res) => {
  323. await coreClient.NChar.getPastUserNameChanges()
  324. .then((resp) => {
  325. res.status(200).json(resp);
  326. }).catch((err) => {
  327. console.log("err:", err)
  328. res.status(400).json("something-went-wrong");
  329. });
  330. });
  331. app.post("/nchar/editUserProfile", async (req, res) => {
  332. let params = {};
  333. if (req.body.firstName) params.firstName = req.body.firstName;
  334. if (req.body.lastName) params.lastName = req.body.lastName;
  335. await coreClient.NChar.editUserProfile(params).then((resp) => {
  336. res.status(200).json(resp);
  337. }).catch((err) => {
  338. console.log("err:", err)
  339. res.status(400).json("something-went-wrong");
  340. });
  341. });
  342. app.post("/nchar/addMail", async (req, res) => {
  343. await coreClient.NChar.addMail({
  344. mail: req.body.mail
  345. }).then((resp) => {
  346. res.status(200).json(resp);
  347. }).catch((err) => {
  348. console.log("err:", err)
  349. res.status(400).json("something-went-wrong");
  350. });
  351. });
  352. app.post("/nchar/deleteMail", async (req, res) => {
  353. await coreClient.NChar.deleteMail({
  354. mail: req.body.mail
  355. }).then((resp) => {
  356. res.status(200).json(resp);
  357. }).catch((err) => {
  358. console.log("err:", err)
  359. res.status(400).json("something-went-wrong");
  360. });
  361. });
  362. app.post("/nchar/setPrimaryMail", async (req, res) => {
  363. await coreClient.NChar.setPrimaryMail({
  364. mail: req.body.mail
  365. }).then((resp) => {
  366. res.status(200).json(resp);
  367. }).catch((err) => {
  368. console.log("err:", err)
  369. res.status(400).json("something-went-wrong");
  370. });
  371. });
  372. app.post("/nchar/setPrimaryMailCheckCode", async (req, res) => {
  373. await coreClient.NChar.setPrimaryMailCheckCode({
  374. mail: req.body.mail,
  375. code: req.body.code
  376. }).then((resp) => {
  377. res.status(200).json(resp);
  378. }).catch((err) => {
  379. console.log("err:", err)
  380. res.status(400).json("something-went-wrong");
  381. });
  382. });
  383. const PORT = process.env.PORT || 5000;
  384. app.listen(PORT, () => {
  385. console.log("Server is running on port " + PORT);
  386. });