user.controller.ts 929 Bytes
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap";
Kim, MinGyu's avatar
Kim, MinGyu committed
3
4
5
6
7
import { Request } from "express";

export interface TypedRequestAuth<T> extends Request {
  auth: T;
}
Yoon, Daeki's avatar
Yoon, Daeki committed
8
9
10
11
12
13
14
15
16
17

export const getUsers = asyncWrap(async (req, res) => {
  const users = await userDb.getUsers();
  return res.json(users);
});

export const createUser = asyncWrap(async (req, res) => {
  const user = req.body;
  console.log("user body", user);
  const newUser = await userDb.createUser(user);
Yoon, Daeki's avatar
Yoon, Daeki committed
18
  return res.json(newUser);
Yoon, Daeki's avatar
Yoon, Daeki committed
19
});
Kim, MinGyu's avatar
Kim, MinGyu committed
20
21
22
23
24
25
26
27

export const getProfile = asyncWrap(async (reqExp, res) => {
  const req = reqExp as TypedRequestAuth<{userId : string}>;  // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다.

  const {userId} = req.auth;
  const profile = await userDb.getProfile(userId);
  res.json(profile)
})