Showing
10 changed files
with
80 additions
and
39 deletions
... | @@ -3,14 +3,22 @@ import morgan from "morgan"; | ... | @@ -3,14 +3,22 @@ import morgan from "morgan"; |
3 | import helmet from "helmet"; | 3 | import helmet from "helmet"; |
4 | import cookieParser from "cookie-parser"; | 4 | import cookieParser from "cookie-parser"; |
5 | import bodyParser from "body-parser"; | 5 | import bodyParser from "body-parser"; |
6 | +import passport from "passport"; | ||
7 | +import mongoose from "mongoose"; | ||
8 | +import session from "express-session"; | ||
9 | +import MongoStore from "connect-mongo"; | ||
6 | import { localsMiddleware } from "./middlewares"; | 10 | import { localsMiddleware } from "./middlewares"; |
7 | import routes from "./routes"; | 11 | import routes from "./routes"; |
8 | import userRouter from "./routers/userRouter"; | 12 | import userRouter from "./routers/userRouter"; |
9 | import videoRouter from "./routers/videoRouter"; | 13 | import videoRouter from "./routers/videoRouter"; |
10 | import globalRouter from "./routers/globalRouter"; | 14 | import globalRouter from "./routers/globalRouter"; |
11 | 15 | ||
16 | +import "./passport"; | ||
17 | + | ||
12 | const app = express(); | 18 | const app = express(); |
13 | 19 | ||
20 | +const CokieStore = MongoStore(session); | ||
21 | + | ||
14 | app.use(helmet()); | 22 | app.use(helmet()); |
15 | app.set("view engine", "pug"); | 23 | app.set("view engine", "pug"); |
16 | app.use("/uploads", express.static("uploads")); | 24 | app.use("/uploads", express.static("uploads")); |
... | @@ -20,6 +28,17 @@ app.use(bodyParser.json()); | ... | @@ -20,6 +28,17 @@ app.use(bodyParser.json()); |
20 | app.use(bodyParser.urlencoded({ extended: true })); // json, html, text, urlencoded 할 거 없이 다 parser할 수 있도록 설정해줘야 한다. | 28 | app.use(bodyParser.urlencoded({ extended: true })); // json, html, text, urlencoded 할 거 없이 다 parser할 수 있도록 설정해줘야 한다. |
21 | app.use(morgan("dev")); | 29 | app.use(morgan("dev")); |
22 | 30 | ||
31 | +app.use( | ||
32 | + session({ | ||
33 | + secret: process.env.COOKIE_SECRET, | ||
34 | + resave: true, | ||
35 | + saveUninitialized: false, | ||
36 | + store: new CokieStore({ mongooseConnection: mongoose.connection }), | ||
37 | + }) | ||
38 | +); | ||
39 | +app.use(passport.initialize()); | ||
40 | +app.use(passport.session()); | ||
41 | + | ||
23 | app.use(localsMiddleware); | 42 | app.use(localsMiddleware); |
24 | app.use(routes.home, globalRouter); | 43 | app.use(routes.home, globalRouter); |
25 | app.use(routes.users, userRouter); | 44 | app.use(routes.users, userRouter); | ... | ... |
1 | +import passport from "passport"; | ||
1 | import routes from "../routes"; | 2 | import routes from "../routes"; |
2 | import User from "../models/User"; | 3 | import User from "../models/User"; |
3 | 4 | ||
... | @@ -5,7 +6,7 @@ import User from "../models/User"; | ... | @@ -5,7 +6,7 @@ import User from "../models/User"; |
5 | export const getJoin = (req, res) => { | 6 | export const getJoin = (req, res) => { |
6 | res.render("join", { pageTitle: "Join" }); | 7 | res.render("join", { pageTitle: "Join" }); |
7 | }; | 8 | }; |
8 | -export const postJoin = async (req, res) => { | 9 | +export const postJoin = async (req, res, next) => { |
9 | const { | 10 | const { |
10 | body: { name, email, password, password2 }, | 11 | body: { name, email, password, password2 }, |
11 | } = req; | 12 | } = req; |
... | @@ -19,20 +20,23 @@ export const postJoin = async (req, res) => { | ... | @@ -19,20 +20,23 @@ export const postJoin = async (req, res) => { |
19 | email, | 20 | email, |
20 | }); | 21 | }); |
21 | await User.register(user, password); | 22 | await User.register(user, password); |
23 | + next(); | ||
22 | } catch (error) { | 24 | } catch (error) { |
25 | + // eslint-disable-next-line no-console | ||
23 | console.log(error); | 26 | console.log(error); |
27 | + res.redirect(routes.home); | ||
24 | } | 28 | } |
25 | // To Do: Register User | 29 | // To Do: Register User |
26 | // To Do: Log user in | 30 | // To Do: Log user in |
27 | - res.redirect(routes.home); | ||
28 | } | 31 | } |
29 | }; | 32 | }; |
30 | 33 | ||
31 | export const getLogin = (req, res) => | 34 | export const getLogin = (req, res) => |
32 | res.render("login", { pageTitle: "Login" }); | 35 | res.render("login", { pageTitle: "Login" }); |
33 | -export const postLogin = (req, res) => { | 36 | +export const postLogin = passport.authenticate("local", { |
34 | - res.redirect(routes.home); | 37 | + failureRedirect: routes.login, |
35 | -}; | 38 | + successRedirect: routes.home, |
39 | +}); | ||
36 | 40 | ||
37 | // 로그아웃을 클릭하면 LogOut페이지로 가는 것 대신에, 로그아웃을 처리한 후 | 41 | // 로그아웃을 클릭하면 LogOut페이지로 가는 것 대신에, 로그아웃을 처리한 후 |
38 | // home 페이지로 Redirect로 표현할 것이다. | 42 | // home 페이지로 Redirect로 표현할 것이다. |
... | @@ -42,7 +46,7 @@ export const logout = (req, res) => { | ... | @@ -42,7 +46,7 @@ export const logout = (req, res) => { |
42 | res.redirect(routes.home); | 46 | res.redirect(routes.home); |
43 | }; | 47 | }; |
44 | 48 | ||
45 | -export const users = (req, res) => res.render("users", { pageTitle: "Users" }); | 49 | +// export const users = (req, res) => res.render("users", { pageTitle: "Users" }); |
46 | export const userDetail = (req, res) => | 50 | export const userDetail = (req, res) => |
47 | res.render("userDetail", { pageTitle: "User Detail" }); | 51 | res.render("userDetail", { pageTitle: "User Detail" }); |
48 | export const editProfile = (req, res) => | 52 | export const editProfile = (req, res) => | ... | ... |
... | @@ -6,12 +6,23 @@ const multerVideo = multer({ dest: "uploads/videos/" }); | ... | @@ -6,12 +6,23 @@ const multerVideo = multer({ dest: "uploads/videos/" }); |
6 | export const localsMiddleware = (req, res, next) => { | 6 | export const localsMiddleware = (req, res, next) => { |
7 | res.locals.siteName = "my Youtube"; | 7 | res.locals.siteName = "my Youtube"; |
8 | res.locals.routes = routes; | 8 | res.locals.routes = routes; |
9 | - res.locals.user = { | 9 | + res.locals.user = req.user || null; |
10 | - isAuthenticated: false, | ||
11 | - id: 1, | ||
12 | - }; | ||
13 | next(); | 10 | next(); |
14 | }; | 11 | }; |
15 | 12 | ||
13 | +export const onlyPublic = (req, res, next) => { | ||
14 | + if (req.user) { | ||
15 | + res.redirect(routes.home); | ||
16 | + } else { | ||
17 | + next(); | ||
18 | + } | ||
19 | +}; | ||
20 | +export const onlyPrivate = (req, res, next) => { | ||
21 | + if (req.user) { | ||
22 | + next(); | ||
23 | + } else { | ||
24 | + res.redirect(routes.home); | ||
25 | + } | ||
26 | +}; | ||
16 | export const uploadVideo = multerVideo.single("videoFile"); | 27 | export const uploadVideo = multerVideo.single("videoFile"); |
17 | // single에 들어간 videoFile은 upload.pug의 file 부분 input name | 28 | // single에 들어간 videoFile은 upload.pug의 file 부분 input name | ... | ... |
... | @@ -22,10 +22,12 @@ | ... | @@ -22,10 +22,12 @@ |
22 | "autoprefixer": "^9.8.0", | 22 | "autoprefixer": "^9.8.0", |
23 | "babel-loader": "^8.1.0", | 23 | "babel-loader": "^8.1.0", |
24 | "body-parser": "^1.19.0", | 24 | "body-parser": "^1.19.0", |
25 | + "connect-mongo": "^3.2.0", | ||
25 | "cookie-parser": "^1.4.5", | 26 | "cookie-parser": "^1.4.5", |
26 | "css-loader": "^3.5.3", | 27 | "css-loader": "^3.5.3", |
27 | "dotenv": "^8.2.0", | 28 | "dotenv": "^8.2.0", |
28 | "express": "^4.17.1", | 29 | "express": "^4.17.1", |
30 | + "express-session": "^1.17.1", | ||
29 | "extract-text-webpack-plugin": "^4.0.0-beta.0", | 31 | "extract-text-webpack-plugin": "^4.0.0-beta.0", |
30 | "helmet": "^3.22.0", | 32 | "helmet": "^3.22.0", |
31 | "mongoose": "^5.9.15", | 33 | "mongoose": "^5.9.15", | ... | ... |
... | @@ -2,23 +2,23 @@ import express from "express"; | ... | @@ -2,23 +2,23 @@ import express from "express"; |
2 | import routes from "../routes"; | 2 | import routes from "../routes"; |
3 | import { home, search } from "../controllers/videoController"; | 3 | import { home, search } from "../controllers/videoController"; |
4 | import { | 4 | import { |
5 | - logout, | 5 | + logout, |
6 | - getJoin, | 6 | + getJoin, |
7 | - postJoin, | 7 | + postJoin, |
8 | - getLogin, | 8 | + getLogin, |
9 | - postLogin, | 9 | + postLogin, |
10 | } from "../controllers/userController"; | 10 | } from "../controllers/userController"; |
11 | +import { onlyPublic } from "../middlewares"; | ||
11 | 12 | ||
12 | const globalRouter = express.Router(); | 13 | const globalRouter = express.Router(); |
13 | 14 | ||
14 | -globalRouter.get(routes.home, home); | 15 | +globalRouter.get(routes.join, onlyPublic, getJoin); |
15 | - | 16 | +globalRouter.post(routes.join, onlyPublic, postJoin, postLogin); |
16 | -globalRouter.get(routes.join, getJoin); | ||
17 | -globalRouter.post(routes.join, postJoin); | ||
18 | 17 | ||
19 | -globalRouter.get(routes.login, getLogin); | 18 | +globalRouter.get(routes.login, onlyPublic, getLogin); |
20 | -globalRouter.post(routes.login, postLogin); | 19 | +globalRouter.post(routes.login, onlyPublic, postLogin); |
21 | 20 | ||
22 | -globalRouter.get(routes.logout, logout); | 21 | +globalRouter.get(routes.home, home); |
23 | globalRouter.get(routes.search, search); | 22 | globalRouter.get(routes.search, search); |
24 | -export default globalRouter; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
23 | +globalRouter.get(routes.logout, logout); | ||
24 | +export default globalRouter; | ... | ... |
1 | import express from "express"; | 1 | import express from "express"; |
2 | import routes from "../routes"; | 2 | import routes from "../routes"; |
3 | import { | 3 | import { |
4 | - userDetail, | 4 | + userDetail, |
5 | - editProfile, | 5 | + editProfile, |
6 | - changePassword, | 6 | + changePassword, |
7 | } from "../controllers/userController"; | 7 | } from "../controllers/userController"; |
8 | +import { onlyPrivate } from "../middlewares"; | ||
9 | + | ||
8 | const userRouter = express.Router(); | 10 | const userRouter = express.Router(); |
9 | 11 | ||
10 | -userRouter.get(routes.editProfile, editProfile); | 12 | +userRouter.get(routes.editProfile, onlyPrivate, editProfile); |
11 | -userRouter.get(routes.changePassword, changePassword); | 13 | +userRouter.get(routes.changePassword, onlyPrivate, changePassword); |
12 | userRouter.get(routes.userDetail(), userDetail); | 14 | userRouter.get(routes.userDetail(), userDetail); |
13 | 15 | ||
14 | export default userRouter; | 16 | export default userRouter; |
15 | 17 | ||
16 | - | ||
17 | - | ||
18 | /* | 18 | /* |
19 | userRouter.get("/", (req, res) => res.send("user index")); | 19 | userRouter.get("/", (req, res) => res.send("user index")); |
20 | userRouter.get("/edit", (req, res) => res.send("user edit")); | 20 | userRouter.get("/edit", (req, res) => res.send("user edit")); |
... | @@ -25,4 +25,4 @@ app.use("/user", userRouter) | ... | @@ -25,4 +25,4 @@ app.use("/user", userRouter) |
25 | 이런식으로 사용하면 app.js에서 하나하나 라우터를 만드는 방법과 달리 | 25 | 이런식으로 사용하면 app.js에서 하나하나 라우터를 만드는 방법과 달리 |
26 | /user라 필요한 라우터에 대한 라우터들을 모두 import할 수 있게 된다.. | 26 | /user라 필요한 라우터에 대한 라우터들을 모두 import할 수 있게 된다.. |
27 | (ex, /user, /user/edit, /user/password ...) | 27 | (ex, /user, /user/edit, /user/password ...) |
28 | -*/ | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
28 | +*/ | ... | ... |
... | @@ -8,22 +8,22 @@ import { | ... | @@ -8,22 +8,22 @@ import { |
8 | postEditVideo, | 8 | postEditVideo, |
9 | deleteVideo, | 9 | deleteVideo, |
10 | } from "../controllers/videoController"; | 10 | } from "../controllers/videoController"; |
11 | -import { uploadVideo } from "../middlewares"; | 11 | +import { uploadVideo, onlyPrivate } from "../middlewares"; |
12 | // export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다. | 12 | // export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다. |
13 | const videoRouter = express.Router(); | 13 | const videoRouter = express.Router(); |
14 | 14 | ||
15 | // Upload | 15 | // Upload |
16 | -videoRouter.get(routes.upload, getUpload); | 16 | +videoRouter.get(routes.upload, onlyPrivate, getUpload); |
17 | -videoRouter.post(routes.upload, uploadVideo, postUpload); | 17 | +videoRouter.post(routes.upload, onlyPrivate, uploadVideo, postUpload); |
18 | 18 | ||
19 | // Video Detail | 19 | // Video Detail |
20 | videoRouter.get(routes.videoDetail(), videoDetail); | 20 | videoRouter.get(routes.videoDetail(), videoDetail); |
21 | 21 | ||
22 | // Video Edit | 22 | // Video Edit |
23 | -videoRouter.get(routes.editVideo(), getEditVideo); | 23 | +videoRouter.get(routes.editVideo(), onlyPrivate, getEditVideo); |
24 | -videoRouter.post(routes.editVideo(), postEditVideo); | 24 | +videoRouter.post(routes.editVideo(), onlyPrivate, postEditVideo); |
25 | 25 | ||
26 | // Video Delete | 26 | // Video Delete |
27 | -videoRouter.get(routes.deleteVideo(), deleteVideo); | 27 | +videoRouter.get(routes.deleteVideo(), onlyPrivate, deleteVideo); |
28 | 28 | ||
29 | export default videoRouter; | 29 | export default videoRouter; | ... | ... |
text/study.txt
0 → 100644
... | @@ -8,7 +8,7 @@ header.header | ... | @@ -8,7 +8,7 @@ header.header |
8 | input(type="text", placeholder="Search by term...", name="term") | 8 | input(type="text", placeholder="Search by term...", name="term") |
9 | .header__column | 9 | .header__column |
10 | ul | 10 | ul |
11 | - if !user.isAuthenticated | 11 | + if !user |
12 | li | 12 | li |
13 | a(href=routes.join) Join | 13 | a(href=routes.join) Join |
14 | li | 14 | li | ... | ... |
-
Please register or login to post a comment