Lee SeJin

edit profile update

......@@ -117,6 +117,6 @@ dist
.pnp.*
package-lock.json
uploads
/uploads
static
build
\ No newline at end of file
......
......@@ -22,6 +22,7 @@
"express-session": "^1.17.1",
"mongoose": "^5.12.9",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"nodemon": "^2.0.7",
"passport": "^0.4.1",
"passport-github2": "^0.1.12",
......
......@@ -23,11 +23,48 @@ export const getUserDetail = async (req,res) =>{
res.render("userDetail",{pagetTitle:"User Detail", quote:quote.quote, author:quote.author})
}
export const getEditProfile = (req,res)=> res.render("editProfile",{pageTitle:"Edit Profile"});
export const getEditProfile = async (req,res)=> {
const{
user:{_id:id}
} = req;
try{
const user = await User.findById(id);
if(user.id !== id){
throw Error();
} else{
res.render("editProfile",{pageTitle:"Edit Profile", user});
}
}catch(error){
console.log(error);
}
};
export const postEditProfile = (req,res) =>{
console.log(req.body);
export const postEditProfile = async (req,res) =>{
const {
user:{_id:id},
body: {name, email, school, blogUrl, tech, career, introduction},
file
} = req;
try{
const updatedUser = await User.findByIdAndUpdate(id, {
avatarUrl: file ? file.path : req.session.passport.user.avatarUrl,
name,
email,
school,
blogUrl,
tech: User.formatTech(tech),
career: User.formatCareer(career),
introduction
},
{
new: true
});
req.session.passport.user = updatedUser;
res.redirect("/users/edit-profile");
}catch(error){
console.log(error);
res.redirect("/");
}
};
......@@ -49,7 +86,7 @@ export const githubLoginCallback = async (_, __, profile, done) =>{
const {_json: {id:githubId, login:githubName, avatar_url:avatarUrl, name, email}} = profile;
try{
const user = await User.findOne({email});
const user = await User.findOne({githubId});
if(user){
user.githubId = githubId,
user.githubName = githubName
......
import multer from "multer";
export const localsMiddleware = (req,res,next) => {
res.locals.siteName = "Dev Profile";
res.locals.loggedUser = req.user || null;
......@@ -20,3 +22,10 @@ export const onlyPrivate = (req, res, next) => {
res.redirect("/");
}
};
export const uploadFiles = multer({
dest:"uploads/",
limits: {
fileSize: 3000000
}
});
\ No newline at end of file
......
......@@ -27,7 +27,7 @@ const UserSchema = new mongoose.Schema({
},
tech: [{ type: String, trim: true }],
career: [{ type: String, trim: true }],
introduction: String,
introduction: { type: String, maxLength: 500},
createdAt: {
type: Date,
default: Date.now
......
......@@ -12,9 +12,10 @@ globalRouter.get("/join", onlyPublic, getJoin);
globalRouter.get("/login", onlyPublic, getLogin);
globalRouter.get("/logout", onlyPrivate, logout);
globalRouter.get("/auth/github", githubLogin);
globalRouter.get("/auth/github", onlyPublic, githubLogin);
globalRouter.get(
"/auth/github/callback",
onlyPublic,
passport.authenticate("github",{failureRedirect: "/login"}),
postGithubLogin
);
......
import express from "express";
import { getEditProfile, getUserDetail, handleUsers, postEditProfile } from "../controllers/userController";
import { onlyPrivate } from "../middlewares";
import { onlyPrivate, uploadFiles } from "../middlewares";
const userRouter = express.Router();
......@@ -8,7 +8,7 @@ const userRouter = express.Router();
userRouter.get("/",handleUsers);
userRouter.get("/edit-profile", onlyPrivate, getEditProfile);
userRouter.post("/edit-profile", onlyPrivate, postEditProfile);
userRouter.post("/edit-profile", onlyPrivate, uploadFiles.single("photo"),postEditProfile);
userRouter.get("/:id", getUserDetail);
......
......@@ -32,6 +32,7 @@ app.use(passport.initialize());
app.use(passport.session());
app.use(localsMiddleware);
app.use("/uploads", express.static("uploads"));
app.use("/", globalRouter);
app.use("/users", userRouter);
......
......@@ -2,15 +2,23 @@ extends layouts/main
block content
.form-container
form(action="/users/edit-profile", method="POST", enctype="application/json")
form(action="/users/edit-profile", method="POST", enctype="multipart/form-data")
img(src=`/${loggedUser.avatarUrl}`, width="100", height="120")
.fileUpload
label(for="picture") Picture
input(type="file", id="picture", name="picture", accept="image/*")
input(type="text", placeholder="Name", name="name")
input(type="text", placeholder="Github Id", name="githubId")
input(type="email", placeholder="Email", name="email")
input(type="text", placeholder="School", name="school")
textarea(name="tech", placeholder="Add your Tech")
textarea(name="career", placeholder="Add your Career")
textarea(name="introduction", placeholder="Self introduction")
label(for="photo") Photo
input(type="file", id="photo", name="photo", accept="image/*")
label(for="name") Name
input(type="text", id="name",placeholder="Name", name="name", value=user.name)
label(for="email") Email
input(type="email", id="email", placeholder="Email", name="email", value=user.email)
label(for="school") School
input(type="text", id="school",placeholder="School", name="school", value=user.school)
label(for="blogUrl") Blog Url
input(type="text", id="blogUrl", placeholder="Blog url", name="blogUrl", value=user.blogUrl)
label(for="tech") Enter your tech skills seperated by comma. ex)react,node
textarea(name="tech", id="tech", placeholder="Add Tech you can use")=user.tech
label(for="career") Enter your careers seperated by comma. ex)A company, B competition
textarea(name="career", id="career", placeholder="Add your Career")=user.career
label(for="introduction") Self introduction
textarea(name="introduction", id="introduction", placeholder="Self introduction", maxlength=200)=user.introduction
input(type="submit", value="Update Profile")
\ No newline at end of file
......
......@@ -7,7 +7,11 @@ block content
h1 -Developer Profile-
h2=quote
h3=author
if !loggedUser
.home-link
a(href="/join") Join
|#{' '}
a(href="/login") Login
else
a(href=`/users/${loggedUser._id}`) My profile
\ No newline at end of file
......
......@@ -3,10 +3,9 @@ extends layouts/main
block content
i.fas.fa-location-arrow
h3 Start with GitHub!
button.login-github
a(href="/auth/github")
span
.login-github
i.fab.fa-github
|Join with GitHub
span Join with GitHub
\ No newline at end of file
......
......@@ -3,10 +3,9 @@ extends layouts/main
block content
i.fas.fa-location-arrow
h3 Login with GitHub!
button.login-github
a(href="/auth/github")
span
.login-github
i.fab.fa-github
|Login with GitHub
span Login with GitHub
\ No newline at end of file
......
footer.footer
hr
.footer__icon
i.fas.fa-code-branch
span.footer__text © dev-profile #{new Date().getFullYear()}
\ No newline at end of file
......
......@@ -14,6 +14,8 @@ header.header
li
a(href="/") Home
li
a(href=`/users/${loggedUser._id}`) My Profile
li
a(href="/users/edit-profile") Edit Profile
li
a(href="/logout") Log Out
\ No newline at end of file
......