Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jwt_toots
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shaik Moses
jwt_toots
Commits
ec09b27f
Commit
ec09b27f
authored
Mar 26, 2021
by
Shaik Moses
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
with database
parent
26dfbe9e
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
347 additions
and
37 deletions
+347
-37
.env
.env
+3
-2
authentication.js
authentication.js
+23
-10
User_model.js
models/User_model.js
+28
-0
package-lock.json
package-lock.json
+227
-0
package.json
package.json
+2
-0
routes.js
routes.js
+59
-23
server.js
server.js
+5
-2
No files found.
.env
View file @
ec09b27f
ACCESS_TOKEN=a1cf0e9b88b85a81ccfaebf1e7a1a5636fff70d16d9a68ee7c8f6e18985cbac68e6206175f319b0f045ca9554a38039cb11c50b18c92ab46abbd6d19b3904031
REFRESH_ACCESS_TOKEN=1eab8976df88c553a16cecf187fc29c5cd9a16a5665c8571506acdb9d595587afbe76e8e2633fde572a20bb342d91bb4e508c26cb7837b62eedaffe73106d1f4
\ No newline at end of file
REFRESH_ACCESS_TOKEN=1eab8976df88c553a16cecf187fc29c5cd9a16a5665c8571506acdb9d595587afbe76e8e2633fde572a20bb342d91bb4e508c26cb7837b62eedaffe73106d1f4
MONGO_DB_URL = mongodb://localhost:27017/jwt_toots
\ No newline at end of file
authentication.js
View file @
ec09b27f
const
jwt
=
require
(
"jsonwebtoken"
)
require
(
'dotenv'
).
config
()
const
authontication
=
(
req
,
res
,
next
)
=>
{
const
header
=
req
.
headers
[
'authorization'
]
const
token
=
header
&&
header
.
split
(
' '
)[
1
]
if
(
token
==
null
)
return
res
.
status
(
400
)
jwt
.
verify
(
token
,
process
.
env
.
ACCESS_TOKEN
,(
err
,
user
)
=>
{
if
(
err
)
return
res
.
status
(
403
)
req
.
user
=
user
})
next
()
}
//
const authontication=(req,res,next)=>{
//
const header = req.headers['authorization']
//
const token = header && header.split(' ')[1]
//
if(token == null)return res.status(400)
//
jwt.verify(token,process.env.ACCESS_TOKEN,(err,user)=>{
//
if(err) return res.status(403)
//
req.user = user
//
})
//
next()
//
}
const
authontication
=
(
req
,
res
,
next
)
=>
{
const
token
=
req
.
header
(
'auth-token'
)
if
(
!
token
)
return
res
.
status
(
401
)
try
{
const
verified
=
jwt
.
verify
(
token
,
process
.
env
.
ACCESS_TOKEN
)
req
.
user
=
verified
next
()
}
catch
(
err
)
{
res
.
status
(
400
)
}
}
module
.
exports
=
authontication
\ No newline at end of file
models/User_model.js
0 → 100644
View file @
ec09b27f
const
mongoose
=
require
(
'mongoose'
)
const
User_sechma
=
new
mongoose
.
Schema
({
name
:
{
type
:
String
,
required
:
true
,
min
:
6
,
max
:
255
},
email
:
{
type
:
String
,
required
:
true
,
max
:
255
,
min
:
6
},
password
:
{
type
:
String
,
required
:
true
,
max
:
1024
,
min
:
6
},
date
:
{
type
:
Date
,
default
:
Date
.
now
()
}
})
module
.
exports
=
mongoose
.
model
(
'user_data'
,
User_sechma
)
\ No newline at end of file
package-lock.json
View file @
ec09b27f
...
...
@@ -17,6 +17,28 @@
"defer-to-connect"
:
"^1.0.1"
}
},
"@types/bson"
:
{
"version"
:
"4.0.3"
,
"resolved"
:
"https://registry.npmjs.org/@types/bson/-/bson-4.0.3.tgz"
,
"integrity"
:
"sha512-mVRvYnTOZJz3ccpxhr3wgxVmSeiYinW+zlzQz3SXWaJmD1DuL05Jeq7nKw3SnbKmbleW5qrLG5vdyWe/A9sXhw=="
,
"requires"
:
{
"@types/node"
:
"*"
}
},
"@types/mongodb"
:
{
"version"
:
"3.6.10"
,
"resolved"
:
"https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.10.tgz"
,
"integrity"
:
"sha512-BkwAHFiZSSWdTIqbUVGmgvIsiXXjqAketeK7Izy7oSs6G3N8Bn993tK9eq6QEovQDx6OQ2FGP2KWDDxBzdlJ6Q=="
,
"requires"
:
{
"@types/bson"
:
"*"
,
"@types/node"
:
"*"
}
},
"@types/node"
:
{
"version"
:
"14.14.36"
,
"resolved"
:
"https://registry.npmjs.org/@types/node/-/node-14.14.36.tgz"
,
"integrity"
:
"sha512-kjivUwDJfIjngzbhooRnOLhGYz6oRFi+L+EpMjxroDYXwDw9lHrJJ43E+dJ6KAd3V3WxWAJ/qZE9XKYHhjPOFQ=="
},
"abbrev"
:
{
"version"
:
"1.1.1"
,
"resolved"
:
"https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz"
,
...
...
@@ -83,11 +105,30 @@
"resolved"
:
"https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz"
,
"integrity"
:
"sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
},
"bcryptjs"
:
{
"version"
:
"2.4.3"
,
"resolved"
:
"https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz"
,
"integrity"
:
"sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms="
},
"binary-extensions"
:
{
"version"
:
"2.2.0"
,
"resolved"
:
"https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz"
,
"integrity"
:
"sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA=="
},
"bl"
:
{
"version"
:
"2.2.1"
,
"resolved"
:
"https://registry.npmjs.org/bl/-/bl-2.2.1.tgz"
,
"integrity"
:
"sha512-6Pesp1w0DEX1N550i/uGV/TqucVL4AM/pgThFSN/Qq9si1/DF9aIHs1BxD8V/QU0HoeHO6cQRTAuYnLPKq1e4g=="
,
"requires"
:
{
"readable-stream"
:
"^2.3.5"
,
"safe-buffer"
:
"^5.1.1"
}
},
"bluebird"
:
{
"version"
:
"3.5.1"
,
"resolved"
:
"https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz"
,
"integrity"
:
"sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA=="
},
"body-parser"
:
{
"version"
:
"1.19.0"
,
"resolved"
:
"https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz"
,
...
...
@@ -137,6 +178,11 @@
"fill-range"
:
"^7.0.1"
}
},
"bson"
:
{
"version"
:
"1.1.6"
,
"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"
,
...
...
@@ -292,6 +338,11 @@
"resolved"
:
"https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz"
,
"integrity"
:
"sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"core-util-is"
:
{
"version"
:
"1.0.2"
,
"resolved"
:
"https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz"
,
"integrity"
:
"sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"crypto-random-string"
:
{
"version"
:
"2.0.0"
,
"resolved"
:
"https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz"
,
...
...
@@ -323,6 +374,11 @@
"resolved"
:
"https://registry.npmjs.org/defer-to-connect/-/defer-to-connect-1.1.3.tgz"
,
"integrity"
:
"sha512-0ISdNousHvZT2EiFlZeZAHBUvSxmKswVCEf8hW7KWgG4a8MVEu/3Vb6uWYozkjylyCxe0JBIiRB1jV45S70WVQ=="
},
"denque"
:
{
"version"
:
"1.5.0"
,
"resolved"
:
"https://registry.npmjs.org/denque/-/denque-1.5.0.tgz"
,
"integrity"
:
"sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ=="
},
"depd"
:
{
"version"
:
"1.1.2"
,
"resolved"
:
"https://registry.npmjs.org/depd/-/depd-1.1.2.tgz"
,
...
...
@@ -657,6 +713,11 @@
"resolved"
:
"https://registry.npmjs.org/is-yarn-global/-/is-yarn-global-0.3.0.tgz"
,
"integrity"
:
"sha512-VjSeb/lHmkoyd8ryPVIKvOCn4D1koMqY+vqyjjUfc3xyKtP4dYOxM44sZrnqQSzSds3xyOrUTLTC9LVCVgLngw=="
},
"isarray"
:
{
"version"
:
"1.0.0"
,
"resolved"
:
"https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz"
,
"integrity"
:
"sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
},
"json-buffer"
:
{
"version"
:
"3.0.0"
,
"resolved"
:
"https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.0.tgz"
,
...
...
@@ -705,6 +766,11 @@
"safe-buffer"
:
"^5.0.1"
}
},
"kareem"
:
{
"version"
:
"2.3.2"
,
"resolved"
:
"https://registry.npmjs.org/kareem/-/kareem-2.3.2.tgz"
,
"integrity"
:
"sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
},
"keyv"
:
{
"version"
:
"3.1.0"
,
"resolved"
:
"https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz"
,
...
...
@@ -781,6 +847,12 @@
"resolved"
:
"https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz"
,
"integrity"
:
"sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
},
"memory-pager"
:
{
"version"
:
"1.5.0"
,
"resolved"
:
"https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz"
,
"integrity"
:
"sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg=="
,
"optional"
:
true
},
"merge-descriptors"
:
{
"version"
:
"1.0.1"
,
"resolved"
:
"https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz"
,
...
...
@@ -827,6 +899,82 @@
"resolved"
:
"https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz"
,
"integrity"
:
"sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
},
"mongodb"
:
{
"version"
:
"3.6.5"
,
"resolved"
:
"https://registry.npmjs.org/mongodb/-/mongodb-3.6.5.tgz"
,
"integrity"
:
"sha512-mQlYKw1iGbvJJejcPuyTaytq0xxlYbIoVDm2FODR+OHxyEiMR021vc32bTvamgBjCswsD54XIRwhg3yBaWqJjg=="
,
"requires"
:
{
"bl"
:
"^2.2.1"
,
"bson"
:
"^1.1.4"
,
"denque"
:
"^1.4.1"
,
"require_optional"
:
"^1.0.1"
,
"safe-buffer"
:
"^5.1.2"
,
"saslprep"
:
"^1.0.0"
}
},
"mongoose"
:
{
"version"
:
"5.12.2"
,
"resolved"
:
"https://registry.npmjs.org/mongoose/-/mongoose-5.12.2.tgz"
,
"integrity"
:
"sha512-kT9t6Nvu9WPsfssn7Gzke446Il8UdMilY7Sa5vALtwoOoNOGtZEVjekZBFwsBFzTWtBA/x5gBmJoYFP+1LeDlg=="
,
"requires"
:
{
"@types/mongodb"
:
"^3.5.27"
,
"bson"
:
"^1.1.4"
,
"kareem"
:
"2.3.2"
,
"mongodb"
:
"3.6.5"
,
"mongoose-legacy-pluralize"
:
"1.0.2"
,
"mpath"
:
"0.8.3"
,
"mquery"
:
"3.2.4"
,
"ms"
:
"2.1.2"
,
"regexp-clone"
:
"1.0.0"
,
"safe-buffer"
:
"5.2.1"
,
"sift"
:
"7.0.1"
,
"sliced"
:
"1.0.1"
},
"dependencies"
:
{
"ms"
:
{
"version"
:
"2.1.2"
,
"resolved"
:
"https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
,
"integrity"
:
"sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"safe-buffer"
:
{
"version"
:
"5.2.1"
,
"resolved"
:
"https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz"
,
"integrity"
:
"sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
}
}
},
"mongoose-legacy-pluralize"
:
{
"version"
:
"1.0.2"
,
"resolved"
:
"https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz"
,
"integrity"
:
"sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ=="
},
"mpath"
:
{
"version"
:
"0.8.3"
,
"resolved"
:
"https://registry.npmjs.org/mpath/-/mpath-0.8.3.tgz"
,
"integrity"
:
"sha512-eb9rRvhDltXVNL6Fxd2zM9D4vKBxjVVQNLNijlj7uoXUy19zNDsIif5zR+pWmPCWNKwAtqyo4JveQm4nfD5+eA=="
},
"mquery"
:
{
"version"
:
"3.2.4"
,
"resolved"
:
"https://registry.npmjs.org/mquery/-/mquery-3.2.4.tgz"
,
"integrity"
:
"sha512-uOLpp7iRX0BV1Uu6YpsqJ5b42LwYnmu0WeF/f8qgD/On3g0XDaQM6pfn0m6UxO6SM8DioZ9Bk6xxbWIGHm2zHg=="
,
"requires"
:
{
"bluebird"
:
"3.5.1"
,
"debug"
:
"3.1.0"
,
"regexp-clone"
:
"^1.0.0"
,
"safe-buffer"
:
"5.1.2"
,
"sliced"
:
"1.0.1"
},
"dependencies"
:
{
"debug"
:
{
"version"
:
"3.1.0"
,
"resolved"
:
"https://registry.npmjs.org/debug/-/debug-3.1.0.tgz"
,
"integrity"
:
"sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g=="
,
"requires"
:
{
"ms"
:
"2.0.0"
}
}
}
},
"ms"
:
{
"version"
:
"2.0.0"
,
"resolved"
:
"https://registry.npmjs.org/ms/-/ms-2.0.0.tgz"
,
...
...
@@ -946,6 +1094,11 @@
"resolved"
:
"https://registry.npmjs.org/prepend-http/-/prepend-http-2.0.0.tgz"
,
"integrity"
:
"sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc="
},
"process-nextick-args"
:
{
"version"
:
"2.0.1"
,
"resolved"
:
"https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz"
,
"integrity"
:
"sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
},
"proxy-addr"
:
{
"version"
:
"2.0.6"
,
"resolved"
:
"https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz"
,
...
...
@@ -1009,6 +1162,20 @@
"strip-json-comments"
:
"~2.0.1"
}
},
"readable-stream"
:
{
"version"
:
"2.3.7"
,
"resolved"
:
"https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz"
,
"integrity"
:
"sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw=="
,
"requires"
:
{
"core-util-is"
:
"~1.0.0"
,
"inherits"
:
"~2.0.3"
,
"isarray"
:
"~1.0.0"
,
"process-nextick-args"
:
"~2.0.0"
,
"safe-buffer"
:
"~5.1.1"
,
"string_decoder"
:
"~1.1.1"
,
"util-deprecate"
:
"~1.0.1"
}
},
"readdirp"
:
{
"version"
:
"3.5.0"
,
"resolved"
:
"https://registry.npmjs.org/readdirp/-/readdirp-3.5.0.tgz"
,
...
...
@@ -1017,6 +1184,11 @@
"picomatch"
:
"^2.2.1"
}
},
"regexp-clone"
:
{
"version"
:
"1.0.0"
,
"resolved"
:
"https://registry.npmjs.org/regexp-clone/-/regexp-clone-1.0.0.tgz"
,
"integrity"
:
"sha512-TuAasHQNamyyJ2hb97IuBEif4qBHGjPHBS64sZwytpLEqtBQ1gPJTnOaQ6qmpET16cK14kkjbazl6+p0RRv0yw=="
},
"registry-auth-token"
:
{
"version"
:
"4.2.1"
,
"resolved"
:
"https://registry.npmjs.org/registry-auth-token/-/registry-auth-token-4.2.1.tgz"
,
...
...
@@ -1033,6 +1205,20 @@
"rc"
:
"^1.2.8"
}
},
"require_optional"
:
{
"version"
:
"1.0.1"
,
"resolved"
:
"https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz"
,
"integrity"
:
"sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g=="
,
"requires"
:
{
"resolve-from"
:
"^2.0.0"
,
"semver"
:
"^5.1.0"
}
},
"resolve-from"
:
{
"version"
:
"2.0.0"
,
"resolved"
:
"https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz"
,
"integrity"
:
"sha1-lICrIOlP+h2egKgEx+oUdhGWa1c="
},
"responselike"
:
{
"version"
:
"1.0.2"
,
"resolved"
:
"https://registry.npmjs.org/responselike/-/responselike-1.0.2.tgz"
,
...
...
@@ -1051,6 +1237,15 @@
"resolved"
:
"https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz"
,
"integrity"
:
"sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"saslprep"
:
{
"version"
:
"1.0.3"
,
"resolved"
:
"https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz"
,
"integrity"
:
"sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag=="
,
"optional"
:
true
,
"requires"
:
{
"sparse-bitfield"
:
"^3.0.3"
}
},
"semver"
:
{
"version"
:
"5.7.1"
,
"resolved"
:
"https://registry.npmjs.org/semver/-/semver-5.7.1.tgz"
,
...
...
@@ -1114,11 +1309,30 @@
"resolved"
:
"https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz"
,
"integrity"
:
"sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
},
"sift"
:
{
"version"
:
"7.0.1"
,
"resolved"
:
"https://registry.npmjs.org/sift/-/sift-7.0.1.tgz"
,
"integrity"
:
"sha512-oqD7PMJ+uO6jV9EQCl0LrRw1OwsiPsiFQR5AR30heR+4Dl7jBBbDLnNvWiak20tzZlSE1H7RB30SX/1j/YYT7g=="
},
"signal-exit"
:
{
"version"
:
"3.0.3"
,
"resolved"
:
"https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz"
,
"integrity"
:
"sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA=="
},
"sliced"
:
{
"version"
:
"1.0.1"
,
"resolved"
:
"https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz"
,
"integrity"
:
"sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E="
},
"sparse-bitfield"
:
{
"version"
:
"3.0.3"
,
"resolved"
:
"https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz"
,
"integrity"
:
"sha1-/0rm5oZWBWuks+eSqzM004JzyhE="
,
"optional"
:
true
,
"requires"
:
{
"memory-pager"
:
"^1.0.2"
}
},
"statuses"
:
{
"version"
:
"1.5.0"
,
"resolved"
:
"https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz"
,
...
...
@@ -1159,6 +1373,14 @@
}
}
},
"string_decoder"
:
{
"version"
:
"1.1.1"
,
"resolved"
:
"https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz"
,
"integrity"
:
"sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg=="
,
"requires"
:
{
"safe-buffer"
:
"~5.1.0"
}
},
"strip-ansi"
:
{
"version"
:
"5.2.0"
,
"resolved"
:
"https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz"
,
...
...
@@ -1282,6 +1504,11 @@
"prepend-http"
:
"^2.0.0"
}
},
"util-deprecate"
:
{
"version"
:
"1.0.2"
,
"resolved"
:
"https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz"
,
"integrity"
:
"sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
},
"utils-merge"
:
{
"version"
:
"1.0.1"
,
"resolved"
:
"https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz"
,
...
...
package.json
View file @
ec09b27f
...
...
@@ -10,9 +10,11 @@
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
bcryptjs
"
:
"^2.4.3"
,
"
dotenv
"
:
"^8.2.0"
,
"
express
"
:
"^4.17.1"
,
"
jsonwebtoken
"
:
"^8.5.1"
,
"
mongoose
"
:
"^5.12.2"
,
"
nodemon
"
:
"^2.0.7"
}
}
routes.js
View file @
ec09b27f
require
(
'dotenv'
).
config
()
const
express
=
require
(
'express'
)
const
router
=
express
.
Router
()
const
AUTH
=
require
(
'./authentication.js'
)
const
jwt
=
require
(
"jsonwebtoken"
)
const
User
=
require
(
'./models/User_model'
)
const
bcrypt
=
require
(
'bcryptjs'
)
const
jwt
=
require
(
'jsonwebtoken'
)
// const AUTH = require('./authentication.js')
// const jwt= require("jsonwebtoken")
const
post_data
=
[
{
name
:
"cool"
},
{
name
:
"coool1"
}
]
//
const post_data=[
//
{
//
name : "cool"
//
},
//
{
//
name:"coool1"
//
}
//
]
router
.
get
(
'/'
,
AUTH
,(
req
,
res
)
=>
{
const
post
=
post_data
.
filter
(
post
=>
post
.
name
===
req
.
user
.
name
)
if
(
post
.
length
==
0
){
res
.
json
({
message
:
"this is empty"
})
}
else
{
res
.
json
({
data
:
post_data
.
filter
(
post
=>
post
.
name
===
req
.
user
.
name
)})
}
})
router
.
post
(
'/login'
,(
req
,
res
)
=>
{
const
name
=
req
.
body
.
name
const
user
=
{
name
:
name
}
const
access_token
=
jwt
.
sign
(
user
,
process
.
env
.
ACCESS_TOKEN
,{
expiresIn
:
'100s'
})
res
.
json
({
access_token
:
access_token
})
// router.get('/',AUTH,(req,res)=>{
// const post=post_data.filter(post=> post.name === req.user.name)
// if(post.length == 0){
// res.json({message:"this is empty"})
// }else{
// res.json({data:post_data.filter(post=> post.name === req.user.name)})
// }
// })
// router.post('/login',(req,res)=>{
// const name = req.body.name
// const user = {name : name}
// const access_token=jwt.sign(user,process.env.ACCESS_TOKEN,{expiresIn : '100s'})
// res.json({access_token:access_token})
// })
router
.
post
(
'/register'
,
async
(
req
,
res
)
=>
{
// to check wheather email is in database or not
const
exist_email
=
await
User
.
findOne
({
email
:
req
.
body
.
email
})
if
(
exist_email
)
return
res
.
status
(
400
).
send
(
"this email already exist"
)
const
salt
=
await
bcrypt
.
genSalt
(
10
)
const
hashedPassword
=
await
bcrypt
.
hash
(
req
.
body
.
password
,
salt
)
//to create new user if not exist
const
user
=
new
User
({
name
:
req
.
body
.
name
,
password
:
hashedPassword
,
email
:
req
.
body
.
email
,
})
try
{
const
save_data
=
await
user
.
save
()
res
.
send
(
save_data
)
}
catch
(
error
)
{
res
.
status
(
400
).
send
(
error
)
}
})
router
.
post
(
'/login'
,
async
(
req
,
res
)
=>
{
const
user
=
await
User
.
findOne
({
email
:
req
.
body
.
email
})
if
(
!
user
)
return
res
.
status
(
400
).
send
(
"email or password is not correct"
)
const
validpass
=
await
bcrypt
.
compare
(
req
.
body
.
password
,
user
.
password
)
if
(
!
validpass
)
return
res
.
status
(
400
).
send
(
"invalid password"
)
const
token
=
jwt
.
sign
({
_id
:
user
.
_id
},
process
.
env
.
ACCESS_TOKEN
)
res
.
header
(
'auth-token'
,
token
).
send
(
token
)
})
module
.
exports
=
router
\ No newline at end of file
server.js
View file @
ec09b27f
...
...
@@ -3,11 +3,15 @@ const express = require('express')
const
app
=
express
()
const
jwt
=
require
(
'jsonwebtoken'
)
const
router
=
require
(
'./routes.js'
)
const
mongoose
=
require
(
'mongoose'
)
//to parse the data in json
app
.
use
(
express
.
json
())
mongoose
.
connect
(
process
.
env
.
MONGO_DB_URL
,{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},()
=>
console
.
log
(
"db"
))
//api for jwt authentication
app
.
use
(
'/api'
,
router
)
//port config
app
.
listen
(
3000
,()
=>
console
.
log
(
"this server is running at 3000"
))
\ No newline at end of file
app
.
listen
(
3000
,()
=>
console
.
log
(
"this server is running at 3000"
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment