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