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

export const createUser = async (user: IUser) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
5
6
7
  // 비밀번호 암호화
  const hash = await bcrypt.hash(user.password, 10);
  const newUser = await User.create({ email: user.email, password: hash });
8
9
10
  return newUser;
};

Yoon, Daeki's avatar
Yoon, Daeki committed
11
12
13
14
15
16
17
18
19
20
export const deleteUserById = async (userId: string) => {
  const deletedUser = await User.findByIdAndDelete(userId);
  return deletedUser;
};

export const findUserById = async (id: string) => {
  const user = await User.findById(id);
  return user;
};

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
};

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;
  }
};