서주원

implement login, route other pages

1 const findById=require('../../database/user/findById') 1 const findById=require('../../database/user/findById')
2 const mysql=require('../../mysql') 2 const mysql=require('../../mysql')
3 const bcrypt=require('bcrypt-nodejs') 3 const bcrypt=require('bcrypt-nodejs')
4 +const session=require('express-session')
5 +const app=require('express')()
6 +
7 +app.use(session({
8 + secret:'ambc@!vsmkv#!&*!#EDNAnsv#!$()_*#@',
9 + resave:false,
10 + saveUninitialized:true
11 +}))
12 +
4 13
5 exports.Login=(req,res)=>{ 14 exports.Login=(req,res)=>{
6 const userId=req.body.userId 15 const userId=req.body.userId
...@@ -21,27 +30,38 @@ exports.Login=(req,res)=>{ ...@@ -21,27 +30,38 @@ exports.Login=(req,res)=>{
21 } 30 }
22 31
23 const IdCheck=()=>{ 32 const IdCheck=()=>{
24 - console.log(2) 33 + let user={}
25 - resolve(findById.findById(userId)) 34 + const findUser= async ()=>{
35 + try{
36 + user =await findById.findById(userId)
37 + return user
38 + }
39 + catch (err) {
40 + return Promise.reject(err)
41 + }
42 + }
43 + return findUser()
26 } 44 }
27 45
28 const PwCheck=(user)=>{ 46 const PwCheck=(user)=>{
29 - console.log(user) 47 + if (user[0]==null){
30 - if (!user){
31 console.log('2 err') 48 console.log('2 err')
32 - return reject({ 49 + return Promise.reject({
33 code:'id_wrong', 50 code:'id_wrong',
34 message:'id wrong' 51 message:'id wrong'
35 }) 52 })
36 } 53 }
37 console.log('3') 54 console.log('3')
38 - if(bcrypt.compareSync(password,user.password)){ 55 + if(bcrypt.compareSync(password,user[0].password)){
39 - console.log(`Login : ${userId}`) 56 + console.log(`3 success\nLogin : ${userId}`)
40 - resolve() 57 + req.session.sid=userId
58 + req.session.save(()=>{
59 + res.status(200).json({userId:userId})
60 + })
41 } 61 }
42 else{ 62 else{
43 console.log('3 err') 63 console.log('3 err')
44 - return reject({ 64 + return Promise.reject({
45 code:'pw_wrong', 65 code:'pw_wrong',
46 message:'pw wrong' 66 message:'pw wrong'
47 }) 67 })
...@@ -51,11 +71,8 @@ exports.Login=(req,res)=>{ ...@@ -51,11 +71,8 @@ exports.Login=(req,res)=>{
51 DataCheck() 71 DataCheck()
52 .then(IdCheck) 72 .then(IdCheck)
53 .then(PwCheck) 73 .then(PwCheck)
54 - .then(()=>{
55 - req.session.sid=userId
56 - res.status(200).json({userId:userId})
57 - })
58 .catch((err)=>{ 74 .catch((err)=>{
75 + console.log(err)
59 res.status(500).json(err.message|err) 76 res.status(500).json(err.message|err)
60 }) 77 })
61 } 78 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -33,29 +33,6 @@ exports.SignUp=(req,res)=>{ ...@@ -33,29 +33,6 @@ exports.SignUp=(req,res)=>{
33 } 33 }
34 } 34 }
35 return findUser() 35 return findUser()
36 - /*
37 - mysql.getConnection((err,connection)=>{
38 - if(err)
39 - return reject({
40 - code: 'connect_db_error',
41 - message: 'connect_db_error'
42 - })
43 - connection.query(`select * from user where userId=\'${userId}\'`,(err,result,fields)=>{
44 - if(err){
45 - connection.release()
46 - return reject({
47 - code:'select_db_error',
48 - message:'select db error'
49 - })
50 - }
51 - else{
52 - connection.release()
53 - console.log('1 result in findById ',result)
54 - return result
55 - }
56 - })
57 - })
58 - */
59 } 36 }
60 37
61 const SignUp=(user)=>{ 38 const SignUp=(user)=>{
......
...@@ -21,6 +21,11 @@ app.use(function (req, res, next) { ...@@ -21,6 +21,11 @@ app.use(function (req, res, next) {
21 res.header('Access-Control-Allow-Headers', 'content-type, x-access-token') 21 res.header('Access-Control-Allow-Headers', 'content-type, x-access-token')
22 next() 22 next()
23 }) 23 })
24 +app.use(session({
25 + secret:'ambc@!vsmkv#!&*!#EDNAnsv#!$()_*#@',
26 + resave:false,
27 + saveUninitialized:true
28 +}))
24 29
25 app.use('/api',require('./api')) 30 app.use('/api',require('./api'))
26 31
...@@ -40,6 +45,7 @@ app.get('/',(req,res)=>{ ...@@ -40,6 +45,7 @@ app.get('/',(req,res)=>{
40 }) 45 })
41 46
42 app.get('/main',(req,res)=>{ 47 app.get('/main',(req,res)=>{
48 + console.log(`GET /main\ncurrent sid : ${req.session.sid}`)
43 if(!req.session.sid) 49 if(!req.session.sid)
44 res.redirect('/login') 50 res.redirect('/login')
45 else { 51 else {
...@@ -64,6 +70,33 @@ app.get('/login',(req,res)=>{ ...@@ -64,6 +70,33 @@ app.get('/login',(req,res)=>{
64 }) 70 })
65 }) 71 })
66 72
73 +app.post('/logout',(req,res)=>{
74 + console.log('logout')
75 + delete req.session.sid
76 + res.status(200).json({result:'Logout Successful'})
77 +})
78 +
79 +app.get('/decklist',(req,res)=>{
80 + fs.readFile('./views/html/decklist.html',(err,data)=>{
81 + res.writeHead(200,{'Content-Type':'text/html'})
82 + res.end(data)
83 + })
84 +})
85 +
86 +app.get('/ingame',(req,res)=>{
87 + fs.readFile('./views/html/ingame.html',(err,data)=>{
88 + res.writeHead(200,{'Content-Type':'text/html'})
89 + res.end(data)
90 + })
91 +})
92 +
93 +app.get('/newdeck',(req,res)=>{
94 + fs.readFile('./views/html/newdeck.html',(err,data)=>{
95 + res.writeHead(200,{'Content-Type':'text/html'})
96 + res.end(data)
97 + })
98 +})
99 +
67 app.listen(process.env.SERVER_PORT || 3000,()=>{ 100 app.listen(process.env.SERVER_PORT || 3000,()=>{
68 console.log('sample server is listening to port ' + process.env.SERVER_PORT) 101 console.log('sample server is listening to port ' + process.env.SERVER_PORT)
69 }) 102 })
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -18,7 +18,7 @@ exports.findById=(userId)=>{ ...@@ -18,7 +18,7 @@ exports.findById=(userId)=>{
18 } 18 }
19 else{ 19 else{
20 connection.release() 20 connection.release()
21 - console.log('1 result in findById ',result) 21 + console.log('result in findById ',result)
22 resolve(result) 22 resolve(result)
23 } 23 }
24 }) 24 })
......
...@@ -15,6 +15,25 @@ ...@@ -15,6 +15,25 @@
15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
17 <![endif]--> 17 <![endif]-->
18 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
19 + <script>
20 + $(document).ready(function(){
21 + $('#logoutButton').click(function(){
22 + $.ajax({
23 + type:'POST',
24 + url:'/logout',
25 + success:function(result){
26 + alert('로그아웃 성공!')
27 + window.location.href='/main'
28 + },
29 + error:function(result){
30 + alert('로그아웃 실패!')
31 + return false
32 + }
33 + })
34 + })
35 + })
36 + </script>
18 </head> 37 </head>
19 <body> 38 <body>
20 <nav class="navbar-default navbar-fixed-top"> 39 <nav class="navbar-default navbar-fixed-top">
...@@ -32,7 +51,7 @@ ...@@ -32,7 +51,7 @@
32 <ul class="nav navbar-nav"> 51 <ul class="nav navbar-nav">
33 </ul> 52 </ul>
34 <div class="navbar-right"> 53 <div class="navbar-right">
35 - <a href="#"><button class="btn navbar-btn" style="color:#000;">로그아웃</button></a> 54 + <input type="button" class="btn navbar-btn" id="logoutButton" style="color:#000;" value="로그아웃" />
36 </div> 55 </div>
37 </div> 56 </div>
38 </div> 57 </div>
...@@ -43,7 +62,7 @@ ...@@ -43,7 +62,7 @@
43 <div class="container" style="width:50%;"> 62 <div class="container" style="width:50%;">
44 <div class="row"> 63 <div class="row">
45 <h3 class="form-signin-heading col-md-10" style="text-align: left;">내 덱 리스트</h3> 64 <h3 class="form-signin-heading col-md-10" style="text-align: left;">내 덱 리스트</h3>
46 - <input type="button" class="btn col-md-2" value="+" style="margin-top: 18px;" /> 65 + <a href="/newdeck"><input type="button" class="btn col-md-2" value="+" style="margin-top: 18px;" /></a>
47 </div> 66 </div>
48 <br> 67 <br>
49 <table class="table"> 68 <table class="table">
......
...@@ -15,6 +15,25 @@ ...@@ -15,6 +15,25 @@
15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
17 <![endif]--> 17 <![endif]-->
18 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
19 + <script>
20 + $(document).ready(function(){
21 + $('#logoutButton').click(function(){
22 + $.ajax({
23 + type:'POST',
24 + url:'/logout',
25 + success:function(result){
26 + alert('로그아웃 성공!')
27 + window.location.href='/main'
28 + },
29 + error:function(result){
30 + alert('로그아웃 실패!')
31 + return false
32 + }
33 + })
34 + })
35 + })
36 + </script>
18 </head> 37 </head>
19 <body> 38 <body>
20 <nav class="navbar-default navbar-fixed-top"> 39 <nav class="navbar-default navbar-fixed-top">
...@@ -32,7 +51,7 @@ ...@@ -32,7 +51,7 @@
32 <ul class="nav navbar-nav"> 51 <ul class="nav navbar-nav">
33 </ul> 52 </ul>
34 <div class="navbar-right"> 53 <div class="navbar-right">
35 - <a href="#"><button class="btn navbar-btn" style="color:#000;">로그아웃</button></a> 54 + <input type="button" class="btn navbar-btn" id="logoutButton" style="color:#000;" value="로그아웃" />
36 </div> 55 </div>
37 </div> 56 </div>
38 </div> 57 </div>
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
36 window.location.href='/main' 36 window.location.href='/main'
37 }, 37 },
38 error:function(result){ 38 error:function(result){
39 - alert(`로그인 실패!\nmessage:${result.message}`) 39 + alert(`로그인 실패!\nmessage:${result['message']}`)
40 return false 40 return false
41 } 41 }
42 }) 42 })
......
...@@ -15,6 +15,26 @@ ...@@ -15,6 +15,26 @@
15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
17 <![endif]--> 17 <![endif]-->
18 +
19 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
20 + <script>
21 + $(document).ready(function(){
22 + $('#logoutButton').click(function(){
23 + $.ajax({
24 + type:'POST',
25 + url:'/logout',
26 + success:function(result){
27 + alert('로그아웃 성공!')
28 + window.location.href='/main'
29 + },
30 + error:function(result){
31 + alert('로그아웃 실패!')
32 + return false
33 + }
34 + })
35 + })
36 + })
37 + </script>
18 </head> 38 </head>
19 <body> 39 <body>
20 <nav class="navbar-default navbar-fixed-top"> 40 <nav class="navbar-default navbar-fixed-top">
...@@ -32,7 +52,7 @@ ...@@ -32,7 +52,7 @@
32 <ul class="nav navbar-nav"> 52 <ul class="nav navbar-nav">
33 </ul> 53 </ul>
34 <div class="navbar-right"> 54 <div class="navbar-right">
35 - <a href="#"><button class="btn navbar-btn" style="color:#000;">로그인</button></a> 55 + <input type="button" class="btn navbar-btn" id="logoutButton" style="color:#000;" value="로그아웃" />
36 </div> 56 </div>
37 </div> 57 </div>
38 </div> 58 </div>
...@@ -44,14 +64,14 @@ ...@@ -44,14 +64,14 @@
44 <div class="col-md-6 "> 64 <div class="col-md-6 ">
45 <br> 65 <br>
46 <br> 66 <br>
47 - <input class="btn btn-lg btn-primary btn-block" type="button" id="goMyDecks" value="덱 확인"/> 67 + <a href="/decklist"><input class="btn btn-lg btn-primary btn-block" type="button" id="goMyDecks" value="덱 확인"/></a>
48 <br> 68 <br>
49 <br> 69 <br>
50 </div> 70 </div>
51 <div class="col-md-6 "> 71 <div class="col-md-6 ">
52 <br> 72 <br>
53 <br> 73 <br>
54 - <input class="btn btn-lg btn-primary btn-block" type="button" id="goGame" value="게임 시작"/> 74 + <a href="/ingame"><input class="btn btn-lg btn-primary btn-block" type="button" id="goGame" value="게임 시작"/></a>
55 <br> 75 <br>
56 <br> 76 <br>
57 </div> 77 </div>
......
...@@ -15,6 +15,25 @@ ...@@ -15,6 +15,25 @@
15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 15 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 16 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
17 <![endif]--> 17 <![endif]-->
18 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
19 + <script>
20 + $(document).ready(function(){
21 + $('#logoutButton').click(function(){
22 + $.ajax({
23 + type:'POST',
24 + url:'/logout',
25 + success:function(result){
26 + alert('로그아웃 성공!')
27 + window.location.href='/main'
28 + },
29 + error:function(result){
30 + alert('로그아웃 실패!')
31 + return false
32 + }
33 + })
34 + })
35 + })
36 + </script>
18 </head> 37 </head>
19 <body> 38 <body>
20 <nav class="navbar-default navbar-fixed-top"> 39 <nav class="navbar-default navbar-fixed-top">
...@@ -32,7 +51,7 @@ ...@@ -32,7 +51,7 @@
32 <ul class="nav navbar-nav"> 51 <ul class="nav navbar-nav">
33 </ul> 52 </ul>
34 <div class="navbar-right"> 53 <div class="navbar-right">
35 - <a href="#"><button class="btn navbar-btn" style="color:#000;">로그아웃</button></a> 54 + <input type="button" class="btn navbar-btn" id="logoutButton" style="color:#000;" value="로그아웃" />
36 </div> 55 </div>
37 </div> 56 </div>
38 </div> 57 </div>
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
42 window.location.href='/login' 42 window.location.href='/login'
43 }, 43 },
44 error:function(result){ 44 error:function(result){
45 - alert(`회원가입 실패!\nmessage:${result.message}`) 45 + alert(`회원가입 실패!\nmessage:${result['message']}`)
46 return false 46 return false
47 } 47 }
48 }) 48 })
......