Skip to content

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.0.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "^4.17.1",
Expand Down
62 changes: 40 additions & 22 deletions server/src/routers/api/userRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) => {
Copy link
Contributor

@BhavyaaArora-08 BhavyaaArora-08 Nov 18, 2020

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)

  1. To log him out, simply delete req.token from req.user.tokens. And update the user in the User model too.

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.

res.clearCookie("token");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to invalidate the token
Meaning -> Find this token (in user's token array ) and remove it from that array. Save the changes made.

res.status(200).json({
msg: "User Signout Successfully",
});
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please include screenshots in your PRs,
There is almost a week left so hurry up.
You won't be able to get points after 20th Nov.

module.exports = router;