user.db.ts 1.14 KB
Newer Older
1
import bcrypt from "bcryptjs";
Lee Soobeom's avatar
Lee Soobeom committed
2
import { IUser, Post, User } from "../models";
Yoon, Daeki's avatar
Yoon, Daeki committed
3
4

export const createUser = async (user: IUser) => {
5
6
  // 비밀번호 암호화
  const hash = await bcrypt.hash(user.password, 10);
Lee Soobeom's avatar
Lee Soobeom committed
7
8
9
10
11
  const newUser = await User.create({
    email: user.email,
    password: hash,
    name: user.name,
  });
Yoon, Daeki's avatar
Yoon, Daeki committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  return newUser;
};

export const findUserByEmail = async (
  email: string,
  includePassword: boolean = false
) => {
  let user;
  if (includePassword) {
    user = await User.findOne({ email }).select("+password");
  } else {
    user = await User.findOne({ email });
  }
  return user;
};

Lee Soobeom's avatar
Lee Soobeom committed
28
29
30
31
32
export const findUserByPostId = async (postId: string) => {
  const post = await Post.findOne({ _id: postId }).populate("user");
  return post?.user;
};

Yoon, Daeki's avatar
Yoon, Daeki committed
33
34
35
36
37
38
39
40
41
42
43
44
45
export const getUsers = async () => {
  const users = await User.find({});
  return users;
};

export const isUser = async (email: string) => {
  const user = await User.findOne({ email });
  if (user) {
    return true;
  } else {
    return false;
  }
};
Kim, MinGyu's avatar
Kim, MinGyu committed
46

Lee Soobeom's avatar
Lee Soobeom committed
47
48
49
50
export const getProfile = async (userId: string) => {
  const profile = await User.findById(userId);
  return profile; //이름 수정
};