-
Notifications
You must be signed in to change notification settings - Fork 18
Added signout controller #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
node_modules | ||
.env |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,32 +7,39 @@ const UserProfile = require("../../models/Profile"); | |
/* GET users listing. */ | ||
router.get("/", function (req, res, next) { | ||
res.send("respond with a resource"); | ||
next(); | ||
}); | ||
|
||
router.post("/signup", async (req, res) => { | ||
const user = new User(req.body); | ||
try { | ||
await user.save(); | ||
const token = await user.generateAuthToken(); | ||
const filtered_user = user.toJSON(); | ||
res.status(201).json({ | ||
msg: "Your account has been created successfully", | ||
user: filtered_user, | ||
token, | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
res.status(400).send({ errors: [{ msg: e.message }] }); | ||
} | ||
}); | ||
|
||
router.post("/signup" , async (req, res) => { | ||
const user = new User(req.body) | ||
try{ | ||
await user.save(); | ||
const token = await user.generateAuthToken(); | ||
const filtered_user = user.toJSON() | ||
res.status(201).json({msg: "Your account has been created successfully", user:filtered_user, token}) | ||
}catch(e){ | ||
console.log(e); | ||
res.status(400).send({ errors: [{msg: e.message}] }); | ||
} | ||
}) | ||
|
||
router.post("/login" , async(req,res)=>{ | ||
try{ | ||
const user = await User.findByCredentials(req.body.email, req.body.password) | ||
const token = await user.generateAuthToken() | ||
res.send({ msg: "You have been logged in successfully", user, token}) | ||
}catch(e){ | ||
console.log(e) | ||
res.status(400).send({ errors: [{msg: e.message}] }); | ||
router.post("/login", async (req, res) => { | ||
try { | ||
const user = await User.findByCredentials( | ||
req.body.email, | ||
req.body.password | ||
); | ||
const token = await user.generateAuthToken(); | ||
res.send({ msg: "You have been logged in successfully", user, token }); | ||
} catch (e) { | ||
console.log(e); | ||
res.status(400).send({ errors: [{ msg: e.message }] }); | ||
} | ||
}) | ||
}); | ||
|
||
// @route GET api/users | ||
// @desc Get yourself | ||
|
@@ -41,4 +48,15 @@ router.get("/", auth, (req, res) => { | |
res.send(req.user); | ||
}); | ||
|
||
// @route POST api/users/logout | ||
// @desc Logout a user | ||
// @access Private | ||
|
||
router.post("/logout", auth, async (req, res) => { | ||
res.clearCookie("token"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to invalidate the token |
||
res.status(200).json({ | ||
msg: "User Signout Successfully", | ||
}); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, please include screenshots in your PRs, |
||
module.exports = router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you can make the below listed changes by today, I will merge the PR and you will be awarded points else not.
logged in user is available at req.user and the token he has been using to access the private routes is available at req.token. Why? All of this has been handled in the auth middleware.
Also all the tokens he has been given so far is present in tokens array, (req.user.tokens)
Also, You should know how to test your API endpoints.
https://www.youtube.com/watch?v=t5n07Ybz7yI
Here's a video that might be helpful.