index.js 6.0 KB

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