middlewares.js 669 Bytes
Newer Older
1
import routes from "./routes";
KangMin An's avatar
KangMin An committed
2
import jwt from "jsonwebtoken";
3

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

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

  try {
KangMin An's avatar
KangMin An committed
23
    const acs_decode = jwt.verify(acs_token, process.env.AUTH_ACCESS_SECRETKEY);
KangMin An's avatar
KangMin An committed
24
25
26
27
28
    next();
  } catch (error) {
    console.log(error);
    res.redirect("/api/login");
  }
29
};