서주원

implement signup and some of login

const express=require('exress')
const express=require('express')
const router=express.Router()
router.use('/user',require('./user'))
......
......@@ -4,3 +4,7 @@ const router=express.Router()
const signUp=require('./signUp')
const login=require('./login')
router.post('/signup',signUp.SignUp)
router.post('/login',login.Login)
module.exports=router
\ No newline at end of file
......
const findById=require('../../database/user/findById')
const mysql=require('../../mysql')
const bcrypt=require('bcrypt-nodejs')
exports.Login=(req,res)=>{
const userId=req.body.userId
const password=req.body.password
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
console.log('1')
if(!userId || !password){
console.log('1 err')
return reject({
code: 'request_body_error',
message: 'request body is not defined'
})
}
else resolve()
})
}
const IdCheck=()=>{
console.log(2)
resolve(findById.findById(userId))
}
const PwCheck=(user)=>{
console.log(user)
if (!user){
console.log('2 err')
return reject({
code:'id_wrong',
message:'id wrong'
})
}
console.log('3')
if(bcrypt.compareSync(password,user.password)){
console.log(`Login : ${userId}`)
resolve()
}
else{
console.log('3 err')
return reject({
code:'pw_wrong',
message:'pw wrong'
})
}
}
DataCheck()
.then(IdCheck)
.then(PwCheck)
.then(()=>{
req.session.sid=userId
res.status(200).json({userId:userId})
})
.catch((err)=>{
res.status(500).json(err.message|err)
})
}
\ No newline at end of file
'use strict'
const express=require('express')
const session=require('express-session')
const findById=require('../../database/user/findById')
const mysql=require('mysql')
const mysql=require('../../mysql')
const bcrypt=require('bcrypt-nodejs')
const dbconfig=require('../../dbconfig')
const connection=mysql.createConnection(dbconfig)
connection.connect()
exports.SignUp=(req,res)=>{
const userId=req.body.userId
......@@ -21,28 +20,66 @@ exports.SignUp=(req,res)=>{
else resolve()
})
}
const SignUp=()=>{
const user=findById(userId)
if(!user){
const hash=bcrypt.hashSync(password,bcrypt.genSaltSync(10),null)
connection.query(`insert into user (userId,password) values (${userId},${hash});`,(err,results,fields)=>{
const UserCheck=()=>{
let user={}
const findUser=async ()=>{
try{
user= await findById.findById(userId)
console.log(`user in UserCheck : ${JSON.stringify(user)}`)
return user
}
catch(err){
return Promise.reject(err)
}
}
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){
throw err
connection.release()
return reject({
code:'select_db_error',
message:'select db error'
})
}
else{
console.log(results)
connection.release()
console.log('1 result in findById ',result)
return result
}
})
}
else{
return reject({
code:'User_already_exists',
message:'User already exists'
})
*/
}
const SignUp=(user)=>{
if(user[0]!=null){
return Promise.reject({
code:'User_Already_Exists',
message:'User Already Exists'
})
}
const hash=bcrypt.hashSync(password,bcrypt.genSaltSync(10),null)
mysql.getConnection((err,connection)=>{
if(err)
throw err
connection.query(`insert into user (userId,password) values (\'${userId}\',\'${hash}\');`,(err,results,fields)=>{
if(err)
throw err
connection.release()
})
})
}
DataCheck()
.then(UserCheck)
.then(SignUp)
.then(()=>{
return res.status(200).json({userId:userId})
......
......@@ -7,15 +7,12 @@ const bcrypt=require('bcrypt-nodejs')
const rp=require('request-promise')
const morgan=require('morgan')
const cheerio=require('cheerio')
const mysql=require('mysql')
require('dotenv').config()
app.use('/api',require('./api'))
const dbconfig=require('dbconfig')
const app=express()
app.use(morgan('[:date[iso]] :method :status :url :response-time(ms) :user-agent'))
app.use(express.static(path.join(__dirname, '/static')))
app.use('/static',express.static(path.join(__dirname, '/static')))
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.use(function (req, res, next) {
......@@ -25,7 +22,7 @@ app.use(function (req, res, next) {
next()
})
const connection=mysql.createConnection(dbconfig)
app.use('/api',require('./api'))
let allCards=[]
fs.readFile('cardskoKR.json',(err,data)=>{
......@@ -46,13 +43,27 @@ app.get('/main',(req,res)=>{
if(!req.session.sid)
res.redirect('/login')
else {
fs.readFile('./views/main',(err,data)=>{
fs.readFile('./views/html/main.html',(err,data)=>{
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(data)
})
}
})
app.get('/signup',(req,res)=>{
fs.readFile('./views/html/signup.html',(err,data)=>{
res.writeHead(200,{'Content-Type':'text/html'})
res.end(data)
})
})
app.get('/login',(req,res)=>{
fs.readFile('./views/html/login.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
......
const mysql=require('mysql')
const dbconfig=require('../../dbconfig')
const mysql=require('../../mysql')
exports.findById=(userId)=>{
const connection=mysql.createConnection(dbconfig)
connection.connect()
connection.query(`select * from user where userId=${userId}`,(err,result,fields)=>{
if(err){
throw err
}
else{
return result
}
return new Promise((resolve,reject)=>{
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)
resolve(result)
}
})
})
})
}
......
require('dotenv').config()
const mysql=require('mysql')
module.exports={
host:process.env.DB_HOST,
user:process.env.DB_USER,
password:process.env.DB_PASSWORD,
database:'WhoAreYou',
port:process.env.DB_PORT
}
\ No newline at end of file
require('dotenv').conf
\ No newline at end of file
......
const mysql=require('mysql')
require('dotenv').config()
const pool=mysql.createPool({
host:process.env.DB_HOST,
user:process.env.DB_USER,
password:process.env.DB_PASSWORD,
database:'WhoAreYou',
port:process.env.DB_PORT
})
module.exports=pool
\ No newline at end of file
......@@ -5,9 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Who Are You? - 하스스톤 멀리건 도우미</title>
<!-- 부트스트랩 -->
<link href="../../static/bootstrap-3.3.2-dist/css/bootstrap.min.css?ver=1" rel="stylesheet">
<link href="../../static/bootstrap-3.3.2-dist/css/bootstrap.min.css" rel="stylesheet">
<link href="../../static/main.css" rel="stylesheet">
<!-- IE8 에서 HTML5 요소와 미디어 쿼리를 위한 HTML5 shim 와 Respond.js -->
<!-- WARNING: Respond.js 는 당신이 file:// 을 통해 페이지를 볼 때는 동작하지 않습니다. -->
......@@ -15,6 +14,39 @@
<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(){
$('#loginButton').click(function(){
var data=new Object()
data.userId=$('#userId').val()
data.password=$('#password').val()
const stringData=JSON.stringify(data)
$.ajax({
type:'POST',
url:'api/user/login',
data:stringData,
dataType:'JSON',
contentType:'application/json; charset=utf-8',
traditional:true,
processdata:false,
success:function(result){
alert('로그인 성공!')
window.location.href='/main'
},
error:function(result){
alert(`로그인 실패!\nmessage:${result.message}`)
return false
}
})
})
$('#goSignup').click(function(){
window.location.href='/signup'
})
})
</script>
</head>
<body>
<nav class="navbar-default navbar-fixed-top">
......
......@@ -7,7 +7,7 @@
<title>Who Are You? - 하스스톤 멀리건 도우미</title>
<!-- 부트스트랩 -->
<link href="../../static/bootstrap-3.3.2-dist/css/bootstrap.min.css?ver=1" rel="stylesheet">
<link href="../../static/bootstrap-3.3.2-dist/css/bootstrap.min.css" rel="stylesheet">
<link href="../../static/main.css?after" rel="stylesheet">
<!-- IE8 에서 HTML5 요소와 미디어 쿼리를 위한 HTML5 shim 와 Respond.js -->
<!-- WARNING: Respond.js 는 당신이 file:// 을 통해 페이지를 볼 때는 동작하지 않습니다. -->
......@@ -15,6 +15,42 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js?after"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js?after"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#signupButton').click(function(){
if($('#password').val()!==$('#repeatPassword').val()) {
alert('두 비밀번호가 일치하지 않습니다!')
return false
}
else{
var data=new Object()
data.userId=$('#userId').val()
data.password=$('#password').val()
const stringData=JSON.stringify(data)
console.log(stringData)
$.ajax({
type:'POST',
url:'api/user/signup',
data:stringData,
dataType:'JSON',
contentType:'application/json; charset=utf-8',
traditional:true,
processdata:false,
success:function(result){
alert('회원가입 성공!')
window.location.href='/login'
},
error:function(result){
alert(`회원가입 실패!\nmessage:${result.message}`)
return false
}
})
}
})
})
</script>
</head>
<body>
<nav class="navbar-default navbar-fixed-top">
......