-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
authenticated login and implemented jwt token
- Loading branch information
1 parent
588f37d
commit ad69455
Showing
4 changed files
with
79 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const User = require("../models/user"); | ||
const shortId = require("shortid"); | ||
const expressJwt = require("express-jwt"); | ||
const jwt = require("jsonwebtoken") | ||
|
||
exports.# = (req,res)=>{ | ||
// const {name,email,password} = req.body | ||
// res.json({ | ||
// user:{name,email,password} | ||
// }); | ||
// if user exist | ||
|
||
User.findOne({email: req.body.email}).exec((err,user)=>{ | ||
if(user){ | ||
return res.status(400).json({ | ||
error: "Email already exists" | ||
}) | ||
} | ||
// if not | ||
const {name,email,password} = req.body | ||
let username = shortId.generate() | ||
let profile = `${process.env.CLIENT_URL}/profile/${username}` | ||
|
||
// create a new user | ||
let newUser = new User({name,email,password,profile,username}) | ||
// save that user | ||
newUser.save((err,success)=>{ | ||
if(err){ | ||
return res.status(400).json({ | ||
error:err | ||
}); | ||
}; | ||
res.json({ | ||
message: "Completed # process.Please Login to continue" | ||
}); | ||
// res.json({ | ||
// user: success | ||
// }) | ||
}); | ||
}); | ||
}; | ||
|
||
|
||
exports.login = (req,res)=>{ | ||
const {email,password} = req.body | ||
//checking user existence | ||
User.findOne({email}).exec((err,user)=>{ | ||
if(err || !user){ | ||
return res.status(400).json({ | ||
error: "You forgot to '#'.Email does not exist" | ||
}); | ||
} | ||
// authenticate the user | ||
if(!user.authenticate(password)){ | ||
return res.status(400).json({ | ||
error: "Email and password does not match" | ||
}); | ||
} | ||
// token generation | ||
const token = jwt.sign({_id: user._id},process.env.JWT_TOKEN_SECRET,{expiresIn: "365d"}) | ||
|
||
res.cookie("token",token,{expiresIn:"365d"}) | ||
const {_id,username,name,email,role} = user | ||
return res.json({ | ||
token, | ||
user:{_id,username,name,email,role} | ||
}) | ||
}); | ||
|
||
} |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
const express = require('express') | ||
const router = express.Router(); | ||
const {#} = require("../controllers/#.js") | ||
const {#,login} = require("../controllers/userAuthentication.js") | ||
// const {signin} = require("../controllers") | ||
// importing validators | ||
const {runValidation} = require("../validators") | ||
const {user#Validator} = require("../validators/auth") | ||
const {user#Validator,userLoginValidator} = require("../validators/auth") | ||
|
||
router.post('/#',user#Validator,runValidation,#); | ||
router.post('/#',userLoginValidator,runValidation,login); | ||
|
||
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