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
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
120 additions
and
37 deletions
+120
-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
+0
-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
This diff is collapsed.
Click to expand it.
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