정승호

로그인 토큰 생성 기능 추가

...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
2 const express = require('express') 2 const express = require('express')
3 const app = express() 3 const app = express()
4 const bodyParser = require('body-parser'); 4 const bodyParser = require('body-parser');
5 +const cookieParser = require('cookie-parser');
5 const port = 5000 6 const port = 5000
6 7
7 const {User} = require('./models/Users'); 8 const {User} = require('./models/Users');
...@@ -11,6 +12,7 @@ const config = require('./config/dev') ...@@ -11,6 +12,7 @@ const config = require('./config/dev')
11 app.use(bodyParser.urlencoded({extended : true})); 12 app.use(bodyParser.urlencoded({extended : true}));
12 app.use(bodyParser.json()); 13 app.use(bodyParser.json());
13 14
15 +app.use(cookieParser());
14 16
15 const mongoose = require('mongoose') 17 const mongoose = require('mongoose')
16 mongoose.connect(config.mongoURI,{ 18 mongoose.connect(config.mongoURI,{
...@@ -35,5 +37,35 @@ app.post('/register', (req, res) => { ...@@ -35,5 +37,35 @@ app.post('/register', (req, res) => {
35 }) // MongoDb에 저징 37 }) // MongoDb에 저징
36 }) 38 })
37 39
40 +app.post('/login', (req, res) => {
41 +
42 + // 요청된 이메일이 데이터 베이스에 있는지 확인
43 + User.findOne({email : req.body.email}, (err, user) =>{
44 + if(!user){
45 + return res.json({
46 + loginSuccess : false,
47 + message : "이메일에 해당하는 유저가 없습니다."
48 + })
49 + }
50 + // 요청된 이메일이 데이터 베이스에 있다면, 비밀번호가 맞는디 확인
51 + user.comparePassword( req.body.password,(err, isMatch) =>{
52 + if(!isMatch)
53 + return res.json({loginSuccess : false, message : "비밀번호 오류"})
54 + // 비밀번호까지 맞다면 그 유저에 대한 토큰을 생성한다.
55 + user.generateToken((err,user) =>{
56 + if(err) return res.status(400).send(err);
57 +
58 + //토큰을 저장한다.
59 + res.cookie("x_auth",user.token)
60 + .status(200)
61 + .json({loginSuccess:true, userId: user._id})
62 +
63 +
64 + })
65 + })
66 + })
67 +})
38 68
39 app.listen(port, () => console.log('example app listen on port %s!', port)) 69 app.listen(port, () => console.log('example app listen on port %s!', port))
70 +
71 +
......
1 const mongoose = require('mongoose'); 1 const mongoose = require('mongoose');
2 const bcrypt = require('bcrypt'); 2 const bcrypt = require('bcrypt');
3 const saltRounds = 10; 3 const saltRounds = 10;
4 +const jwt = require('jsonwebtoken');
4 const userSchema = mongoose.Schema({ 5 const userSchema = mongoose.Schema({
5 name : { 6 name : {
6 type : String, 7 type : String,
...@@ -49,10 +50,33 @@ userSchema.pre('save', function(next){ ...@@ -49,10 +50,33 @@ userSchema.pre('save', function(next){
49 next() 50 next()
50 }) 51 })
51 }) 52 })
53 + } else{
54 + next() // 비밀번호를 바꾸는 것이 아니라면, 넘어감
52 } 55 }
53 56
54 }) 57 })
55 58
59 +userSchema.methods.comparePassword = function(plainPassword, cb){
60 + bcrypt.compare(plainPassword, this.password, function(err, isMatch){
61 + if(err) return cb(err),
62 + cb(null, isMatch)
63 + })
64 +}
65 +userSchema.methods.generateToken = function(cb){
66 + // token생성
67 + var user = this;
68 + var token = jwt.sign(user._id, 'secretToken')
69 + user.token = token
70 + user.save(function(err, user){
71 + if(err) return cb(err)
72 + cb(null, user)
73 + })
74 +
75 +
76 +
77 +
78 +}
79 +
56 80
57 const User = mongoose.model('Users', userSchema) 81 const User = mongoose.model('Users', userSchema)
58 82
......
...@@ -196,6 +196,11 @@ ...@@ -196,6 +196,11 @@
196 "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.4.tgz", 196 "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.4.tgz",
197 "integrity": "sha512-S/yKGU1syOMzO86+dGpg2qGoDL0zvzcb262G+gqEy6TgP6rt6z6qxSFX/8X6vLC91P7G7C3nLs0+bvDzmvBA3Q==" 197 "integrity": "sha512-S/yKGU1syOMzO86+dGpg2qGoDL0zvzcb262G+gqEy6TgP6rt6z6qxSFX/8X6vLC91P7G7C3nLs0+bvDzmvBA3Q=="
198 }, 198 },
199 + "buffer-equal-constant-time": {
200 + "version": "1.0.1",
201 + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
202 + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
203 + },
199 "bytes": { 204 "bytes": {
200 "version": "3.1.0", 205 "version": "3.1.0",
201 "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", 206 "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
...@@ -370,11 +375,25 @@ ...@@ -370,11 +375,25 @@
370 "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz", 375 "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
371 "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==" 376 "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
372 }, 377 },
378 + "cookie-parser": {
379 + "version": "1.4.5",
380 + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.5.tgz",
381 + "integrity": "sha512-f13bPUj/gG/5mDr+xLmSxxDsB9DQiTIfhJS/sqjrmfAWiAN+x2O4i/XguTL9yDZ+/IFDanJ+5x7hC4CXT9Tdzw==",
382 + "requires": {
383 + "cookie": "0.4.0",
384 + "cookie-signature": "1.0.6"
385 + }
386 + },
373 "cookie-signature": { 387 "cookie-signature": {
374 "version": "1.0.6", 388 "version": "1.0.6",
375 "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", 389 "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
376 "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" 390 "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
377 }, 391 },
392 + "cookieparser": {
393 + "version": "0.1.0",
394 + "resolved": "https://registry.npmjs.org/cookieparser/-/cookieparser-0.1.0.tgz",
395 + "integrity": "sha1-6hLLEIXBdPMWf66veYX3mr5nHQ4="
396 + },
378 "core-util-is": { 397 "core-util-is": {
379 "version": "1.0.2", 398 "version": "1.0.2",
380 "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", 399 "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
...@@ -454,6 +473,14 @@ ...@@ -454,6 +473,14 @@
454 "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=", 473 "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
455 "dev": true 474 "dev": true
456 }, 475 },
476 + "ecdsa-sig-formatter": {
477 + "version": "1.0.11",
478 + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
479 + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
480 + "requires": {
481 + "safe-buffer": "^5.0.1"
482 + }
483 + },
457 "ee-first": { 484 "ee-first": {
458 "version": "1.1.1", 485 "version": "1.1.1",
459 "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", 486 "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
...@@ -886,6 +913,49 @@ ...@@ -886,6 +913,49 @@
886 "integrity": "sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg=", 913 "integrity": "sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg=",
887 "dev": true 914 "dev": true
888 }, 915 },
916 + "jsonwebtoken": {
917 + "version": "8.5.1",
918 + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz",
919 + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==",
920 + "requires": {
921 + "jws": "^3.2.2",
922 + "lodash.includes": "^4.3.0",
923 + "lodash.isboolean": "^3.0.3",
924 + "lodash.isinteger": "^4.0.4",
925 + "lodash.isnumber": "^3.0.3",
926 + "lodash.isplainobject": "^4.0.6",
927 + "lodash.isstring": "^4.0.1",
928 + "lodash.once": "^4.0.0",
929 + "ms": "^2.1.1",
930 + "semver": "^5.6.0"
931 + },
932 + "dependencies": {
933 + "ms": {
934 + "version": "2.1.2",
935 + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
936 + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
937 + }
938 + }
939 + },
940 + "jwa": {
941 + "version": "1.4.1",
942 + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
943 + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
944 + "requires": {
945 + "buffer-equal-constant-time": "1.0.1",
946 + "ecdsa-sig-formatter": "1.0.11",
947 + "safe-buffer": "^5.0.1"
948 + }
949 + },
950 + "jws": {
951 + "version": "3.2.2",
952 + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
953 + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
954 + "requires": {
955 + "jwa": "^1.4.1",
956 + "safe-buffer": "^5.0.1"
957 + }
958 + },
889 "kareem": { 959 "kareem": {
890 "version": "2.3.1", 960 "version": "2.3.1",
891 "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.1.tgz", 961 "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.1.tgz",
...@@ -909,6 +979,41 @@ ...@@ -909,6 +979,41 @@
909 "package-json": "^6.3.0" 979 "package-json": "^6.3.0"
910 } 980 }
911 }, 981 },
982 + "lodash.includes": {
983 + "version": "4.3.0",
984 + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
985 + "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
986 + },
987 + "lodash.isboolean": {
988 + "version": "3.0.3",
989 + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
990 + "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
991 + },
992 + "lodash.isinteger": {
993 + "version": "4.0.4",
994 + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
995 + "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
996 + },
997 + "lodash.isnumber": {
998 + "version": "3.0.3",
999 + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
1000 + "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
1001 + },
1002 + "lodash.isplainobject": {
1003 + "version": "4.0.6",
1004 + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
1005 + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
1006 + },
1007 + "lodash.isstring": {
1008 + "version": "4.0.1",
1009 + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
1010 + "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
1011 + },
1012 + "lodash.once": {
1013 + "version": "4.1.1",
1014 + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
1015 + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
1016 + },
912 "lowercase-keys": { 1017 "lowercase-keys": {
913 "version": "1.0.1", 1018 "version": "1.0.1",
914 "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", 1019 "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",
......
...@@ -17,7 +17,9 @@ ...@@ -17,7 +17,9 @@
17 "dependencies": { 17 "dependencies": {
18 "bcrypt": "^4.0.1", 18 "bcrypt": "^4.0.1",
19 "body-parser": "^1.19.0", 19 "body-parser": "^1.19.0",
20 + "cookie-parser": "^1.4.5",
20 "express": "^4.17.1", 21 "express": "^4.17.1",
22 + "jsonwebtoken": "^8.5.1",
21 "mongoose": "^5.9.15" 23 "mongoose": "^5.9.15"
22 }, 24 },
23 "devDependencies": { 25 "devDependencies": {
......