Skip to content

Commit

Permalink
Add user signing out #13
Browse files Browse the repository at this point in the history
  • Loading branch information
akaluzinski committed May 10, 2022
1 parent 0688abe commit 0220f7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions notez-app/src/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const auth = async (req, res, next) => {
}

req.user = user;
req.token = token;
next();
} catch (e) {
res.status(401).send({error: 'Unauthorized.'});
Expand Down
16 changes: 16 additions & 0 deletions notez-app/src/routers/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express');
const User = require("../models/user");
const {auth} = require("../middleware/auth");
const authRouter = new express.Router();

authRouter.post('/auth/login', async (req, res) => {
Expand All @@ -14,4 +15,19 @@ authRouter.post('/auth/login', async (req, res) => {
}
});

authRouter.post('/auth/logout', auth, async (req, res) => {
try {
console.log('Logout ', req.token);
req.user.tokens = req.user.tokens.filter((token) => {
if(token.token === req.token) {
console.log('Match, logout', token.token);
}
return token.token !== req.token;
});
res.send('OK');
} catch (error) {
res.status(500).send({ error: 'Unable to logout' });
}
});

module.exports = authRouter;

0 comments on commit 0220f7d

Please sign in to comment.