Toggle navigation
Toggle navigation
This project
Loading...
Sign in
박민정
/
We-Shop
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
박민정
2021-06-03 02:40:46 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
03d6239b6f09114f8d4e8eebd24b742330c24cc5
03d6239b
1 parent
5dd9c59e
[feat] Generate token
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
166 additions
and
3 deletions
boiler-plate/index.js
boiler-plate/models/User.js
boiler-plate/package-lock.json
boiler-plate/package.json
boiler-plate/index.js
View file @
03d6239
...
...
@@ -13,6 +13,9 @@ app.use(bodyParser.json()) // application/json 타입으로 된 데이터를
const
config
=
require
(
'./config/key'
)
const
cookieParser
=
require
(
'cookie-parser'
)
app
.
use
(
cookieParser
())
const
mongoose
=
require
(
'mongoose'
)
//이 정보는 비밀임..! 몽고DB아이디랑 비밀번호를 감춰야해..!
...
...
@@ -48,6 +51,34 @@ app.post('/register', (req, res) => {
})
// 로그인 구현
app
.
post
(
'/login'
,
(
req
,
res
)
=>
{
// 1. 요청된 이메일이 데이터베이스에 있는지 찾기
User
.
findOne
({
email
:
req
.
body
.
email
},
(
err
,
user
)
=>
{
if
(
!
user
)
{
return
res
.
json
({
loginSuccess
:
false
,
message
:
"There is no user with that email."
})
}
// 2. email과 비밀번호가 맞는지 확인 (User.js에 comparePassword 함수 정의되어 있음)
user
.
comparePassword
(
req
.
body
.
password
,
(
err
,
isMatch
)
=>
{
if
(
!
isMatch
)
return
res
.
json
({
loginSuccess
:
false
,
message
:
"Password is not match."
})
// 3. 비밀번호까지 맞다면 유저를 위한 토큰 생성 (User.js에 generateToken 함수 정의)
user
.
generateToken
((
err
,
user
)
=>
{
// err가 없으면 user에 정보 받아옴
if
(
err
)
return
res
.
status
(
400
).
send
(
err
);
// 4. 생성한 토큰을 저장함 -> 쿠키나 로컬 스토리지 등에 저장할 수 있는데 여기선 쿠키에 저장
res
.
cookie
(
"loginCookie"
,
user
.
token
)
.
status
(
200
)
//성공했다는 표시
.
json
({
loginSuccess
:
true
,
userId
:
user
.
_id
})
})
})
})
})
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`Example app listening at http://localhost:
${
port
}
`
)
})
...
...
boiler-plate/models/User.js
View file @
03d6239
...
...
@@ -5,6 +5,9 @@ const bcrypt = require('bcrypt')
// bcrypt 사용하기 위해 salt를 생성하고 그걸 이용해 암호화 시킴
const
saltRounds
=
10
// salt를 몇글자 할 건지
//
const
jwt
=
require
(
'jsonwebtoken'
)
const
userSchema
=
mongoose
.
Schema
({
name
:{
type
:
String
,
...
...
@@ -54,10 +57,38 @@ userSchema.pre('save', function( next ){
});
});
}
else
{
next
()
}
})
userSchema
.
methods
.
comparePassword
=
function
(
plainPassword
,
cb
){
// 1. plainPassword가 1234567 암호화된 비밀번호 가 같은지 체크해야함
// 그러면 plainPassword도 암호화해서 비교해야함. (복호화 할 수 없기 때문에)
bcrypt
.
compare
(
plainPassword
,
this
.
password
,
function
(
err
,
isMatch
)
{
// 에러가 나면 err callback, 아니면 isMatch
if
(
err
)
return
cb
(
err
);
cb
(
null
,
isMatch
);
})
}
userSchema
.
methods
.
generateToken
=
function
(
cb
)
{
var
user
=
this
;
// jsonwebtoken을 이용해서 token 생성
var
token
=
jwt
.
sign
(
user
.
_id
.
toHexString
(),
'secretToken'
)
//database에 있는 id라서 _id
user
.
token
=
token
user
.
save
(
function
(
err
,
user
){
if
(
err
)
return
cb
(
err
)
// 에러가 있다면 callback으로 에러 전달
cb
(
null
,
user
)
// 에러가 없다면 err는 없고 user정보만 전달
})
}
// 만든 스키마를 모델로 감싸줌
const
User
=
mongoose
.
model
(
'User'
,
userSchema
)
...
...
boiler-plate/package-lock.json
View file @
03d6239
...
...
@@ -268,6 +268,11 @@
"resolved"
:
"https://registry.npmjs.org/bson/-/bson-1.1.6.tgz"
,
"integrity"
:
"sha512-EvVNVeGo4tHxwi8L6bPj3y3itEvStdwvvlojVxxbyYfoaxJ6keLgrTuKdyfEAszFK+H3olzBuafE0yoh0D1gdg=="
},
"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"
,
...
...
@@ -442,6 +447,15 @@
"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"
,
...
...
@@ -527,6 +541,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"
,
...
...
@@ -976,6 +998,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.3"
,
"resolved"
:
"https://registry.npmjs.org/ms/-/ms-2.1.3.tgz"
,
"integrity"
:
"sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
}
}
},
"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.2"
,
"resolved"
:
"https://registry.npmjs.org/kareem/-/kareem-2.3.2.tgz"
,
...
...
@@ -999,6 +1064,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"
,
...
...
@@ -1513,8 +1613,7 @@
"semver"
:
{
"version"
:
"5.7.1"
,
"resolved"
:
"https://registry.npmjs.org/semver/-/semver-5.7.1.tgz"
,
"integrity"
:
"sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ=="
,
"dev"
:
true
"integrity"
:
"sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ=="
},
"semver-diff"
:
{
"version"
:
"3.1.1"
,
...
...
boiler-plate/package.json
View file @
03d6239
...
...
@@ -13,7 +13,9 @@
"dependencies"
:
{
"bcrypt"
:
"^5.0.1"
,
"body-parser"
:
"^1.19.0"
,
"cookie-parser"
:
"^1.4.5"
,
"express"
:
"^4.17.1"
,
"jsonwebtoken"
:
"^8.5.1"
,
"mongoose"
:
"^5.12.12"
},
"devDependencies"
:
{
...
...
Please
register
or
login
to post a comment