middlewares.js 700 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

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

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

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