-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
709e6ec
commit 2360804
Showing
12 changed files
with
357 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
JWT_SECRET : "adadbandsgJHWSABVQOcsuwdeuYQWBKA16591388ddafassas2j3h33d23d23dHJJJHWD3121" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const {JWT_SECRET} = require('../keys') | ||
const User = require('../models/user') | ||
|
||
module.exports = (req,res,next)=>{ | ||
const {authorization} = req.headers | ||
|
||
if(!authorization){ | ||
return res.status(401).send({"error" : "The user is not Logged In"}); | ||
} | ||
const token = authorization.replace("Bearer ",""); | ||
jwt.verify(token , JWT_SECRET ,(err, payload)=>{ | ||
if(err){ | ||
return res.status(401).send({"error" : "The user is not Logged In"}); | ||
} | ||
User.findById(payload._id,(err,user)=>{ | ||
if(err){ | ||
console.log(err) | ||
} | ||
req.user = user; | ||
next() | ||
}) | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const mongoose = require('mongoose'); | ||
const User = require('../models/user'); | ||
const bcrypt = require('bcryptjs'); | ||
const jwt = require('jsonwebtoken'); | ||
const {JWT_SECRET} = require('../keys') | ||
const requireLogin = require('../middleware/requireLogin') | ||
const validator = require('email-validator') | ||
|
||
router.get('/protected',requireLogin,(req,res)=>{ | ||
res.send(`Hello You are protected ${req.user.fullname} `) | ||
}) | ||
|
||
router.post('/signup',(req,res)=>{ | ||
const {fullname,info,image,email,type,password} = req.body; | ||
console.log(req.body) | ||
if( !fullname || !email || !password || !info || !type ){ | ||
return res.status(422).send({"error" : "Please fill all the fields"}) | ||
} | ||
if(!validator.validate(email)){ | ||
return res.status(422).send({"error" : "Invalid Email"}) | ||
} | ||
User.findOne({email : email}) | ||
.then((savedUser)=>{ | ||
if(savedUser){ | ||
return res.status(422).send({"error" : "User already exists with this email id"}) | ||
} | ||
|
||
bcrypt.hash(password , 11) | ||
.then(hashedPassword=>{ | ||
User.create({ | ||
email, | ||
password : hashedPassword, | ||
fullname : fullname, | ||
info, | ||
image, | ||
type | ||
}).then((user)=>{ | ||
res.send({"message" : "Saved user details"}) | ||
}).catch((err)=>{ | ||
console.log(err) | ||
}) | ||
}) | ||
|
||
|
||
|
||
}).catch(err=>{ | ||
console.log(err); | ||
}) | ||
}) | ||
|
||
router.post('/login',(req,res)=>{ | ||
const {fullname,password} = req.body; | ||
console.log(req.body) | ||
if( !fullname || !password){ | ||
return res.status(422).send({"error" : "Please fill all the fields"}) | ||
} | ||
User.findOne({fullname : fullname}) | ||
.then(user=>{ | ||
if(!user){ | ||
return res.send({"error" : "Invalid Username or Password"}); | ||
} | ||
bcrypt.compare(password,user.password) | ||
.then(isSame=>{ | ||
if(!isSame){ | ||
return res.send({"error" : "Invalid Username or Password"}); | ||
} | ||
const token = jwt.sign({_id : user._id},JWT_SECRET) | ||
const {fullname, email, _id, info, image } = user; | ||
res.send({"token" : token, "user" : {_id, email , fullname , info , image }}) | ||
|
||
}) | ||
}).catch(err=>{ | ||
console.log(err) | ||
}) | ||
}) | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.