import bodyParser from "body-parser"; import express from "express"; import dotenv from "dotenv"; import cors from "cors"; import { setServerURL, NAuthClient, NCharClient } from "core-complex-client"; dotenv.config(); const app = express(); app.use(bodyParser.json({ limit: "30mb", extended: true })); app.use(bodyParser.urlencoded({ limit: "30mb", extended: true })); app.use(cors()); setServerURL(process.env.SERVER_URL); const nauthClient = new NAuthClient({ rsaPublicKey: process.env.PUBLIC_KEY, privateKeyID: process.env.PRIVATE_KEY_ID, privateKey: process.env.PRIVATE_KEY, appID: process.env.APP_ID }); const ncharClient = new NCharClient({ nauth: nauthClient }); app.get("/healthCheck", (req, res) => { res.status(200).json("It works!"); }); //NAuth app.post("/changePassword", async (req, res) => { await nauthClient.changePassword({ newPassword: req.body.newPassword, oldPassword: req.body.oldPassword }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/login", async (req, res) => { await nauthClient.login({ mail: req.body.mail, password: req.body.password }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/mailVerify", async (req, res) => { await nauthClient.mailVerify({ hash: req.body.hash, code: req.body.code }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.get("/me", async (req, res) => { await nauthClient.me() .then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/refreshToken", async (req, res) => { await nauthClient.refreshToken({ token: req.body.token }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/register", async (req, res) => { await nauthClient.register({ isApproveContracts: req.body.isApproveContracts, phoneAreaCode: req.body.phoneAreaCode, firstName: req.body.firstName, password: req.body.password, lastName: req.body.lastName, userName: req.body.userName, language: req.body.language, phone: req.body.phone, mail: req.body.mail }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/reSendCodeSMSOTP", async (req, res) => { await nauthClient.reSendCodeSMSOTP({ mail: req.body.mail }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/resetNewPassword", async (req, res) => { await nauthClient.resetNewPassword({ newPassword: req.body.newPassword, mail: req.body.mail, code: req.body.code }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/resetPasswordCheckCode", async (req, res) => { await nauthClient.resetPasswordCheckCode({ mail: req.body.mail, code: req.body.code }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/smsOTP", async (req, res) => { await nauthClient.smsOTP({ mail: req.body.mail, code: req.body.code }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/startMailVerify", async (req, res) => { await nauthClient.startMailVerify({ mail: req.body.mail }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/startResetPassword", async (req, res) => { await nauthClient.startResetPassword({ mail: req.body.mail }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); //NChar app.post("/addPhoneNumber", async (req, res) => { await ncharClient.addPhoneNumber({ phoneAreaCode: req.body.phoneAreaCode, phone: req.body.phone }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/checkChangeMailCode", async (req, res) => { await ncharClient.checkChangeMailCode({ mail: req.body.mail, code: req.body.code }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/deletePhoneNumber", async (req, res) => { await ncharClient.deletePhoneNumber({ phoneAreaCode: req.body.phoneAreaCode, phone: req.body.phone }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.get("/me", async (req, res) => { await ncharClient.me() .then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/setPrimaryPhoneNumber", async (req, res) => { await ncharClient.setPrimaryPhoneNumber({ phoneAreaCode: req.body.phoneAreaCode, phone: req.body.phone }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); app.post("/startChangeMail", async (req, res) => { await ncharClient.startChangeMail({ mail: req.body.mail }).then((resp) => { res.status(200).json(resp); }).catch((err) => { console.log("err:", err); }); }); const PORT = process.env.PORT || 5000; app.listen(PORT, () => { console.log("Server is running on port " + PORT); });