Commit ed7b303c authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

user role 추가

parent 5d59e60d
......@@ -5,7 +5,7 @@ import { useAuth } from "../auth/auth.context";
import "tailwindcss/tailwind.css";
export default function Header() {
const { logout } = useAuth();
const { logout, user } = useAuth();
const [search, setSearch] = useState("");
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
......@@ -33,7 +33,7 @@ export default function Header() {
</div>
</div>
<div className="flex justify-end ">
{useAuth().user.isLoggedIn ? (
{user.isLoggedIn ? (
<div className="flex text-xs">
<Link to="/profile" className="mr-2 ">
프로필
......
......@@ -70,6 +70,7 @@ export const login = asyncWrap(async (req, res) => {
res.json({
isLoggedIn: true,
email: user.email,
role: user.role?.name,
});
});
......
import bcrypt from "bcryptjs";
import { ObjectId } from "mongoose";
import { IUser, Role, Post, User, FileInfo } from "../models";
import { IUser, Role, Post, User, FileInfo, IRole } from "../models";
import fs from "fs/promises";
export const createUser = async (user: IUser) => {
// 비밀번호 암호화
const hash = await bcrypt.hash(user.password, 10);
const newFileInfo = await FileInfo.create({});
// const newFileInfo = await FileInfo.create({});
// 사용자 역할 추가: 기본값은 "user"
let userRole = null;
if (user.role) {
......@@ -19,7 +19,7 @@ export const createUser = async (user: IUser) => {
password: hash,
role: userRole,
isNew: true,
fileInfo: newFileInfo._id,
// fileInfo: newFileInfo._id,
});
const retUser = await newUser.save();
return retUser;
......@@ -31,9 +31,11 @@ export const findUserByEmail = async (
) => {
let user;
if (includePassword) {
user = await User.findOne({ email }).select("+password");
user = await User.findOne({ email })
.select("+password")
.populate<{ role: IRole }>("role");
} else {
user = await User.findOne({ email });
user = await User.findOne({ email }).populate<{ role: IRole }>("role");
}
return user;
};
......
export { default as User, IUser } from "./user.model";
export { default as Post, PostType } from "./post.model";
export { default as Role } from "./role.model";
export { default as Role, IRole } from "./role.model";
export { default as FileInfo, IFileInfo } from "./fileinfo.model";
export { default as Mainimg, MainimgType } from "./mainimg.model";
import { model, Schema } from "mongoose";
interface IRole {
export interface IRole {
name: string;
priority: number;
}
......
import { model, Schema, Types, version } from "mongoose";
import { model, Schema, Types } from "mongoose";
export interface IUser {
email: string;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment