서주원

implement login, route other pages

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