공태현

Implement page move using LoginSession

...@@ -12,10 +12,38 @@ app.set('port', port) ...@@ -12,10 +12,38 @@ app.set('port', port)
12 app.set('view engine', 'ejs') 12 app.set('view engine', 'ejs')
13 app.engine('html',ejs.renderFile ) 13 app.engine('html',ejs.renderFile )
14 14
15 +// model/user.js
16 +const { User } = require('./model/User');
17 +const mongoose = require('mongoose');
18 +// db 연결을 위한 키 값 , 보안을 위해 최종 마스터 브런치에는 포함하지 않을 예정.
19 +mongoose.connect('mongodb+srv://kongtae:ksas9825!%40@squartusers.e2ddc.mongodb.net/?retryWrites=true&w=majority')
20 +.then(() => console.log('MongoDB connect!'))
21 +.catch(err => console.log(err))
22 +
23 +// 로그인 세션 : 로그인 정보 유지.
24 +const express_session = require('express-session')
25 +app.use(express_session({
26 + secret : "@secret@number", // 암호화 키
27 + resave : false,
28 + saveUninitialized : false,
29 + store:require('mongoose-session')(mongoose),
30 + cookie : {maxAge : 60*60*24}
31 +}))
32 +
15 33
16 app.get('/', (req,res) => { 34 app.get('/', (req,res) => {
17 - app.set('views', __dirname + '/views/mainPage') 35 + console.log(req.session)
18 - res.render('main.html') 36 + if (req.session.user)
37 + {
38 + app.set('views', __dirname + '/views/squartPage')
39 + res.render('squart.html')
40 + }
41 + else
42 + {
43 + app.set('views', __dirname + '/views/mainPage')
44 + res.render('main.html')
45 + }
46 +
19 }) 47 })
20 48
21 49
...@@ -25,33 +53,20 @@ app.get('/main.js', (req,res) => { ...@@ -25,33 +53,20 @@ app.get('/main.js', (req,res) => {
25 }) 53 })
26 54
27 app.get('/squart', (req,res) => { 55 app.get('/squart', (req,res) => {
28 - app.set('views', __dirname + '/views/squartPage') 56 +
29 - res.render('squart.html') 57 + app.set('views', __dirname + '/views/squartPage')
58 + res.render('squart.html')
30 }) 59 })
31 60
32 app.listen(port, () => { 61 app.listen(port, () => {
33 console.log(`Listening on ${port} port`); 62 console.log(`Listening on ${port} port`);
34 }) 63 })
35 64
36 -// model/user.js 65 +
37 -const { User } = require('./model/User');
38 -const mongoose = require('mongoose');
39 -// db 연결을 위한 키 값 , 보안을 위해 최종 마스터 브런치에는 포함하지 않을 예정.
40 -mongoose.connect('mongodb+srv://kongtae:ksas9825!%40@squartusers.e2ddc.mongodb.net/?retryWrites=true&w=majority')
41 -.then(() => console.log('MongoDB connect!'))
42 -.catch(err => console.log(err))
43 66
44 // 유저 등록 및 로그인 API 67 // 유저 등록 및 로그인 API
45 68
46 -// 로그인 세션 : 로그인 정보 유지. 69 +
47 -const express_session = require('express-session')
48 -app.use(express_session({
49 - secret : "@secret@number", // 암호화 키
50 - resave : false,
51 - saveUninitialized : false,
52 - store:require('mongoose-session')(mongoose),
53 - cookie : {maxAge : 60*60*24}
54 -}))
55 70
56 71
57 // 등록 . 72 // 등록 .
...@@ -81,6 +96,8 @@ app.post('/api/users/login', (req ,res) => { ...@@ -81,6 +96,8 @@ app.post('/api/users/login', (req ,res) => {
81 user_name : req.body.name, 96 user_name : req.body.name,
82 user_password : req.body.password, 97 user_password : req.body.password,
83 } 98 }
99 + req.session.save()
100 +
84 console.log(req.session.user) 101 console.log(req.session.user)
85 return res.json({ 102 return res.json({
86 loginSuccess : true, 103 loginSuccess : true,
...@@ -127,6 +144,10 @@ app.get('/api/users/logout', (req,res) => { ...@@ -127,6 +144,10 @@ app.get('/api/users/logout', (req,res) => {
127 144
128 }) 145 })
129 146
147 +
148 +
149 +
150 +
130 // 세션 저장 확인 151 // 세션 저장 확인
131 app.get('/api/session', (req,res) => { 152 app.get('/api/session', (req,res) => {
132 console.log(req.session.user) 153 console.log(req.session.user)
......
...@@ -5,9 +5,7 @@ function moveSquartPage() ...@@ -5,9 +5,7 @@ function moveSquartPage()
5 location.href = "/squart"; 5 location.href = "/squart";
6 } 6 }
7 7
8 -const movePage = document.getElementById("moveSquart")
9 8
10 -movePage.addEventListener('click', moveSquartPage);
11 9
12 10
13 $(document).ready(function(){ 11 $(document).ready(function(){
...@@ -39,4 +37,4 @@ $(document).ready(function(){ ...@@ -39,4 +37,4 @@ $(document).ready(function(){
39 } 37 }
40 }) 38 })
41 }) 39 })
42 -})
...\ No newline at end of file ...\ No newline at end of file
40 +})
......