Commit ec09b27f by Shaik Moses

with database

parent 26dfbe9e
ACCESS_TOKEN=a1cf0e9b88b85a81ccfaebf1e7a1a5636fff70d16d9a68ee7c8f6e18985cbac68e6206175f319b0f045ca9554a38039cb11c50b18c92ab46abbd6d19b3904031
REFRESH_ACCESS_TOKEN=1eab8976df88c553a16cecf187fc29c5cd9a16a5665c8571506acdb9d595587afbe76e8e2633fde572a20bb342d91bb4e508c26cb7837b62eedaffe73106d1f4
MONGO_DB_URL = mongodb://localhost:27017/jwt_toots
\ No newline at end of file
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
})
// 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
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
......@@ -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"
}
}
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.post('/register',async(req,res)=>{
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)})
// 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',(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('/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
......@@ -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"))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment