middlewares.js 763 Bytes
Newer Older
1
import routes from "./routes";
KangMin An's avatar
KangMin An committed
2
import jwt from "jsonwebtoken";
3
import envs from "../config/config";
4
import resForm from "./resform";
5

6
7
8
9
/*
  # localmiddleware
  1. Pug Template에서 routes.js에 선언된 경로들을 사용하기 위함.
*/
10
11
12
13
export const localmiddleware = (req, res, next) => {
  res.locals.routes = routes;
  next();
};
14
15
16
17
18
19

/*
  # onlyPrivate
  - 인증된 사용자만 사용할 수 있는 데이터에 접근하기 위한 중간 과정.
*/
export const onlyPrivate = (req, res, next) => {
KangMin An's avatar
KangMin An committed
20
  const {
KangMin An's avatar
KangMin An committed
21
    cookies: { acs_token },
KangMin An's avatar
KangMin An committed
22
23
24
  } = req;

  try {
25
    const acs_decode = jwt.verify(acs_token, envs.secretKey.access_token);
KangMin An's avatar
KangMin An committed
26
    next();
27
28
29
  } catch (err) {
    console.log(err);
    res.json({ msg: resForm.msg.err, contents: { error: err } });
KangMin An's avatar
KangMin An committed
30
  }
31
};