Advertisement
MaksNew

Untitled

Apr 30th, 2023
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import express from 'express'
  2. import path from "path";
  3. import {authMiddleware} from "./middlewares/middlewares.js";
  4. import cookieParser from "cookie-parser";
  5. import jwt from "jsonwebtoken";
  6. import bcrypt from "bcryptjs";
  7. import { create, getAll, remove, update, getUpdateModel } from "./controllers/articleController.js";
  8. import { register, getByEmail } from "./controllers/userController.js";
  9. import {secretKey} from "./config.js";
  10.  
  11. const __dirname = path.resolve()
  12. const PORT = 3000
  13. const app = express()
  14. let val = []
  15.  
  16. app.use(express.json({limit: '1mb'}))
  17. app.use(express.urlencoded({extended: false}))
  18. app.set('view engine', 'ejs')
  19. app.set('views',path.resolve(__dirname, 'templates'))
  20. app.use(express.static('res'));
  21. app.use(cookieParser())
  22.  
  23. function getData(data) {
  24.     val = data
  25. }
  26.  
  27. app.get('/getArticlesByName/:name?', async (req, res)=>{
  28.     let articles = []
  29.     await getAll(getData)
  30.     articles = val
  31.     if (req.params.name === 'undefined') {
  32.         return res.status(200).send(articles)
  33.     } else {
  34.         return res.status(200).send(articles.filter(x => x.name.includes(req.params.name)))
  35.     }
  36. })
  37.  
  38. app.get('/', authMiddleware, async (req, res)=>{
  39.     res.render('index', {authorId: req.cookies.current_user_id})
  40. })
  41.  
  42. app.get('/dfy', authMiddleware, async (req, res)=>{
  43.     res.render('dfy', {authorId: req.cookies.current_user_id})
  44. })
  45.  
  46. app.get('/edit/:id', authMiddleware, async (req, res)=>{
  47.     let article = []
  48.     await getUpdateModel(req.params.id, getData)
  49.     article = val
  50.     if (typeof article === 'undefined') return res.status(500).send("Article not found!");
  51.     if (req.cookies.current_user_id != article.author_id) return res.status(500).send("This article is not your!");
  52.     res.render('edit', {article: article})
  53. })
  54.  
  55. app.get('/login', async (req, res)=>{
  56.     res.render('login')
  57. })
  58.  
  59. app.get('/register', async (req, res)=>{
  60.     res.render('register')
  61. })
  62.  
  63. app.post('/add', authMiddleware, async (req, res) => {
  64.     await create(req.body);
  65.     res.redirect('/');
  66. })
  67.  
  68. app.post('/delete/:id', authMiddleware, async (req, res) => {
  69.     console.log(req.params.id)
  70.     await remove(req.params.id)
  71.     return res.redirect("/");
  72. })
  73.  
  74. app.post('/edit', authMiddleware, async (req, res) => {
  75.     if (req.cookies.current_user_id != req.body.author_id) return res.status(500).send("This article is not your!");
  76.     await update(req.body)
  77.     return res.redirect("/");
  78. })
  79.  
  80. app.post('/login', async(req, res) => {
  81.     try {
  82.         const {email, password} = req.body
  83.         await getByEmail(email, getData)
  84.         if (val === []) return res.status(500).send("Incorrect email!");
  85.         const isValidPassword = await bcrypt.compare(password, val[0].hash)
  86.         if (!(isValidPassword)) return res.status(500).send("Incorrect password!");
  87.         const token = jwt.sign({email, password}, secretKey, {expiresIn: "1h"})
  88.         res.cookie("token", token, {httpOnly:true})
  89.         res.cookie("current_user_id", val[0].id, {httpOnly:true})
  90.         return res.redirect("/");
  91.     }
  92.     catch {
  93.         return res.status(500).send("Server error!");
  94.     }
  95.  
  96. })
  97.  
  98. app.post('/exit', async(req, res) => {
  99.     res.clearCookie("token")
  100.     res.clearCookie("current_user_id")
  101.     return res.redirect("/login")
  102. })
  103.  
  104. app.post('/register', async(req, res) => {
  105.     try {
  106.         const {email, password} = req.body
  107.         await getByEmail(email, getData)
  108.         if (val === []) return res.status(500).send("Email already register");
  109.         const hash = await bcrypt.hash(password, 10);
  110.         await register(email, hash);
  111.         return res.redirect("/");
  112.     }
  113.     catch {
  114.         return res.status(500).send("Server error!");
  115.     }
  116. })
  117.  
  118. app.listen(PORT, ()=>{
  119.     console.log("workin")
  120. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement