Skip to content
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

1239 Logout everywhere #1250

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ app.get('/account', passportConfig.isAuthenticated, userController.getAccount);
app.post('/account/profile', passportConfig.isAuthenticated, userController.postUpdateProfile);
app.post('/account/password', passportConfig.isAuthenticated, userController.postUpdatePassword);
app.post('/account/delete', passportConfig.isAuthenticated, userController.postDeleteAccount);
app.post('/account/logout-everywhere', passportConfig.isAuthenticated, userController.postLogoutEverywhere);
app.get('/account/unlink/:provider', passportConfig.isAuthenticated, userController.getOauthUnlink);

/**
Expand Down
21 changes: 21 additions & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const _ = require('lodash');
const validator = require('validator');
const mailChecker = require('mailchecker');
const User = require('../models/User');
const Session = require('../models/Session');

const randomBytesAsync = promisify(crypto.randomBytes);

Expand Down Expand Up @@ -542,3 +543,23 @@ exports.postForgot = (req, res, next) => {
.then(() => res.redirect('/forgot'))
.catch(next);
};

/**
* POST /account/logout-everywhere
* Logout current user from all devices
*/
exports.postLogoutEverywhere = async (req, res, next) => {
const userId = req.user.id;
try {
await Session.removeSessionByUserId(userId);
req.logout((err) => {
if (err) {
return next(err);
}
req.flash('info', { msg: 'You have been logged out of all sessions.' });
res.redirect('/');
});
} catch (err) {
return next(err);
}
};
24 changes: 24 additions & 0 deletions models/Session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require('mongoose');

const sessionSchema = new mongoose.Schema({
session: String,
expires: Date,
});

sessionSchema.statics = {
/**
* Removes all valid sessions for a given user
* @param {string} userId
* @returns {Promise}
*/
removeSessionByUserId(userId) {
return this.deleteMany({
expires: { $gt: new Date() },
session: { $regex: userId }
});
}
};

const Session = mongoose.model('Session', sessionSchema);

module.exports = Session;
15 changes: 14 additions & 1 deletion views/account/profile.pug
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ block content
i.fas.fa-lock.fa-sm.iconpadding
| Change Password

.pb-2.mt-2.mb-4.border-bottom
h3 Logout Everywhere

form(action='/account/logout-everywhere', method='POST')
input(type='hidden', name='_csrf', value=_csrf)
.form-group
p.offset-sm-3.col-md-7.pl-2 This will log you out of all devices and locations.

.offset-sm-3.col-md-7.pl-2
button.btn.btn-danger(type='submit')
i.fas.fa-sm.iconpadding.fa-sign-out-alt
| Logout everywhere

.pb-2.mt-2.mb-4.border-bottom
h3 Delete Account

Expand Down Expand Up @@ -131,4 +144,4 @@ block content
if user.quickbooks
p.mb-1: a.text-danger(href='/account/unlink/quickbooks') Unlink your QuickBooks account
else
p.mb-1: a(href='/auth/quickbooks') Link your QuickBooks account
p.mb-1: a(href='/auth/quickbooks') Link your QuickBooks account