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