Toggle navigation
Toggle navigation
This project
Loading...
Sign in
정승호
/
Hexa_for_you_renewal
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
1
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
정승호
2020-06-10 01:51:41 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
fe29d1725ce6bb11a02db1005408628aafc9ed78
fe29d172
1 parent
f549a28a
로그인 토큰 생성 기능 추가
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
163 additions
and
0 deletions
index.js
models/Users.js
package-lock.json
package.json
index.js
View file @
fe29d17
...
...
@@ -2,6 +2,7 @@
const
express
=
require
(
'express'
)
const
app
=
express
()
const
bodyParser
=
require
(
'body-parser'
);
const
cookieParser
=
require
(
'cookie-parser'
);
const
port
=
5000
const
{
User
}
=
require
(
'./models/Users'
);
...
...
@@ -11,6 +12,7 @@ const config = require('./config/dev')
app
.
use
(
bodyParser
.
urlencoded
({
extended
:
true
}));
app
.
use
(
bodyParser
.
json
());
app
.
use
(
cookieParser
());
const
mongoose
=
require
(
'mongoose'
)
mongoose
.
connect
(
config
.
mongoURI
,{
...
...
@@ -35,5 +37,35 @@ app.post('/register', (req, res) => {
})
// MongoDb에 저징
})
app
.
post
(
'/login'
,
(
req
,
res
)
=>
{
// 요청된 이메일이 데이터 베이스에 있는지 확인
User
.
findOne
({
email
:
req
.
body
.
email
},
(
err
,
user
)
=>
{
if
(
!
user
){
return
res
.
json
({
loginSuccess
:
false
,
message
:
"이메일에 해당하는 유저가 없습니다."
})
}
// 요청된 이메일이 데이터 베이스에 있다면, 비밀번호가 맞는디 확인
user
.
comparePassword
(
req
.
body
.
password
,(
err
,
isMatch
)
=>
{
if
(
!
isMatch
)
return
res
.
json
({
loginSuccess
:
false
,
message
:
"비밀번호 오류"
})
// 비밀번호까지 맞다면 그 유저에 대한 토큰을 생성한다.
user
.
generateToken
((
err
,
user
)
=>
{
if
(
err
)
return
res
.
status
(
400
).
send
(
err
);
//토큰을 저장한다.
res
.
cookie
(
"x_auth"
,
user
.
token
)
.
status
(
200
)
.
json
({
loginSuccess
:
true
,
userId
:
user
.
_id
})
})
})
})
})
app
.
listen
(
port
,
()
=>
console
.
log
(
'example app listen on port %s!'
,
port
))
...
...
models/Users.js
View file @
fe29d17
const
mongoose
=
require
(
'mongoose'
);
const
bcrypt
=
require
(
'bcrypt'
);
const
saltRounds
=
10
;
const
jwt
=
require
(
'jsonwebtoken'
);
const
userSchema
=
mongoose
.
Schema
({
name
:
{
type
:
String
,
...
...
@@ -49,10 +50,33 @@ userSchema.pre('save', function(next){
next
()
})
})
}
else
{
next
()
// 비밀번호를 바꾸는 것이 아니라면, 넘어감
}
})
userSchema
.
methods
.
comparePassword
=
function
(
plainPassword
,
cb
){
bcrypt
.
compare
(
plainPassword
,
this
.
password
,
function
(
err
,
isMatch
){
if
(
err
)
return
cb
(
err
),
cb
(
null
,
isMatch
)
})
}
userSchema
.
methods
.
generateToken
=
function
(
cb
){
// token생성
var
user
=
this
;
var
token
=
jwt
.
sign
(
user
.
_id
,
'secretToken'
)
user
.
token
=
token
user
.
save
(
function
(
err
,
user
){
if
(
err
)
return
cb
(
err
)
cb
(
null
,
user
)
})
}
const
User
=
mongoose
.
model
(
'Users'
,
userSchema
)
...
...
package-lock.json
View file @
fe29d17
...
...
@@ -196,6 +196,11 @@
"resolved"
:
"https://registry.npmjs.org/bson/-/bson-1.1.4.tgz"
,
"integrity"
:
"sha512-S/yKGU1syOMzO86+dGpg2qGoDL0zvzcb262G+gqEy6TgP6rt6z6qxSFX/8X6vLC91P7G7C3nLs0+bvDzmvBA3Q=="
},
"buffer-equal-constant-time"
:
{
"version"
:
"1.0.1"
,
"resolved"
:
"https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz"
,
"integrity"
:
"sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
},
"bytes"
:
{
"version"
:
"3.1.0"
,
"resolved"
:
"https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz"
,
...
...
@@ -370,11 +375,25 @@
"resolved"
:
"https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz"
,
"integrity"
:
"sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
},
"cookie-parser"
:
{
"version"
:
"1.4.5"
,
"resolved"
:
"https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.5.tgz"
,
"integrity"
:
"sha512-f13bPUj/gG/5mDr+xLmSxxDsB9DQiTIfhJS/sqjrmfAWiAN+x2O4i/XguTL9yDZ+/IFDanJ+5x7hC4CXT9Tdzw=="
,
"requires"
:
{
"cookie"
:
"0.4.0"
,
"cookie-signature"
:
"1.0.6"
}
},
"cookie-signature"
:
{
"version"
:
"1.0.6"
,
"resolved"
:
"https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz"
,
"integrity"
:
"sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"cookieparser"
:
{
"version"
:
"0.1.0"
,
"resolved"
:
"https://registry.npmjs.org/cookieparser/-/cookieparser-0.1.0.tgz"
,
"integrity"
:
"sha1-6hLLEIXBdPMWf66veYX3mr5nHQ4="
},
"core-util-is"
:
{
"version"
:
"1.0.2"
,
"resolved"
:
"https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz"
,
...
...
@@ -454,6 +473,14 @@
"integrity"
:
"sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI="
,
"dev"
:
true
},
"ecdsa-sig-formatter"
:
{
"version"
:
"1.0.11"
,
"resolved"
:
"https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz"
,
"integrity"
:
"sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ=="
,
"requires"
:
{
"safe-buffer"
:
"^5.0.1"
}
},
"ee-first"
:
{
"version"
:
"1.1.1"
,
"resolved"
:
"https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz"
,
...
...
@@ -886,6 +913,49 @@
"integrity"
:
"sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg="
,
"dev"
:
true
},
"jsonwebtoken"
:
{
"version"
:
"8.5.1"
,
"resolved"
:
"https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz"
,
"integrity"
:
"sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w=="
,
"requires"
:
{
"jws"
:
"^3.2.2"
,
"lodash.includes"
:
"^4.3.0"
,
"lodash.isboolean"
:
"^3.0.3"
,
"lodash.isinteger"
:
"^4.0.4"
,
"lodash.isnumber"
:
"^3.0.3"
,
"lodash.isplainobject"
:
"^4.0.6"
,
"lodash.isstring"
:
"^4.0.1"
,
"lodash.once"
:
"^4.0.0"
,
"ms"
:
"^2.1.1"
,
"semver"
:
"^5.6.0"
},
"dependencies"
:
{
"ms"
:
{
"version"
:
"2.1.2"
,
"resolved"
:
"https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
,
"integrity"
:
"sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"jwa"
:
{
"version"
:
"1.4.1"
,
"resolved"
:
"https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz"
,
"integrity"
:
"sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA=="
,
"requires"
:
{
"buffer-equal-constant-time"
:
"1.0.1"
,
"ecdsa-sig-formatter"
:
"1.0.11"
,
"safe-buffer"
:
"^5.0.1"
}
},
"jws"
:
{
"version"
:
"3.2.2"
,
"resolved"
:
"https://registry.npmjs.org/jws/-/jws-3.2.2.tgz"
,
"integrity"
:
"sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA=="
,
"requires"
:
{
"jwa"
:
"^1.4.1"
,
"safe-buffer"
:
"^5.0.1"
}
},
"kareem"
:
{
"version"
:
"2.3.1"
,
"resolved"
:
"https://registry.npmjs.org/kareem/-/kareem-2.3.1.tgz"
,
...
...
@@ -909,6 +979,41 @@
"package-json"
:
"^6.3.0"
}
},
"lodash.includes"
:
{
"version"
:
"4.3.0"
,
"resolved"
:
"https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz"
,
"integrity"
:
"sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
},
"lodash.isboolean"
:
{
"version"
:
"3.0.3"
,
"resolved"
:
"https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz"
,
"integrity"
:
"sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
},
"lodash.isinteger"
:
{
"version"
:
"4.0.4"
,
"resolved"
:
"https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz"
,
"integrity"
:
"sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
},
"lodash.isnumber"
:
{
"version"
:
"3.0.3"
,
"resolved"
:
"https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz"
,
"integrity"
:
"sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
},
"lodash.isplainobject"
:
{
"version"
:
"4.0.6"
,
"resolved"
:
"https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz"
,
"integrity"
:
"sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
},
"lodash.isstring"
:
{
"version"
:
"4.0.1"
,
"resolved"
:
"https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz"
,
"integrity"
:
"sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
},
"lodash.once"
:
{
"version"
:
"4.1.1"
,
"resolved"
:
"https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz"
,
"integrity"
:
"sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
},
"lowercase-keys"
:
{
"version"
:
"1.0.1"
,
"resolved"
:
"https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz"
,
...
...
package.json
View file @
fe29d17
...
...
@@ -17,7 +17,9 @@
"dependencies"
:
{
"bcrypt"
:
"^4.0.1"
,
"body-parser"
:
"^1.19.0"
,
"cookie-parser"
:
"^1.4.5"
,
"express"
:
"^4.17.1"
,
"jsonwebtoken"
:
"^8.5.1"
,
"mongoose"
:
"^5.9.15"
},
"devDependencies"
:
{
...
...
Please
register
or
login
to post a comment