encapsulate global logout function

This commit is contained in:
Martin McKeaveney 2021-10-13 12:26:26 +01:00
parent 11e64b0ffe
commit 74c74d78f2
2 changed files with 11 additions and 16 deletions

View File

@ -240,23 +240,21 @@ exports.saveUser = async (
/** /**
* Logs a user out from budibase. Re-used across account portal and builder. * Logs a user out from budibase. Re-used across account portal and builder.
*/ */
exports.platformLogout = async ({ exports.platformLogout = async ({ ctx, userId, keepActiveSession }) => {
ctx, if (!ctx) throw new Error("Koa context must be supplied to logout.")
userId,
sessionId, const currentSession = this.getCookie(ctx, Cookies.Auth)
keepActiveSession,
}) => {
let sessions = await getUserSessions(userId) let sessions = await getUserSessions(userId)
if (keepActiveSession) { if (keepActiveSession) {
sessions = sessions.filter(session => session.sessionId !== sessionId) sessions = sessions.filter(
session => session.sessionId !== currentSession.sessionId
)
} else { } else {
if (ctx) {
// clear cookies // clear cookies
this.clearCookie(ctx, Cookies.Auth) this.clearCookie(ctx, Cookies.Auth)
this.clearCookie(ctx, Cookies.CurrentApp) this.clearCookie(ctx, Cookies.CurrentApp)
} }
}
await invalidateSessions( await invalidateSessions(
userId, userId,

View File

@ -3,9 +3,8 @@ const {
StaticDatabases, StaticDatabases,
generateNewUsageQuotaDoc, generateNewUsageQuotaDoc,
} = require("@budibase/auth/db") } = require("@budibase/auth/db")
const { hash, getGlobalUserByEmail, saveUser, platformLogout, getCookie } = const { hash, getGlobalUserByEmail, saveUser, platformLogout } =
require("@budibase/auth").utils require("@budibase/auth").utils
const { Cookies } = require("@budibase/auth").constants
const { EmailTemplatePurpose } = require("../../../constants") const { EmailTemplatePurpose } = require("../../../constants")
const { checkInviteCode } = require("../../../utilities/redis") const { checkInviteCode } = require("../../../utilities/redis")
const { sendEmail } = require("../../../utilities/email") const { sendEmail } = require("../../../utilities/email")
@ -178,11 +177,9 @@ exports.updateSelf = async ctx => {
// changing password // changing password
ctx.request.body.password = await hash(ctx.request.body.password) ctx.request.body.password = await hash(ctx.request.body.password)
// Log all other sessions out apart from the current one // Log all other sessions out apart from the current one
const authCookie = getCookie(ctx, Cookies.Auth)
await platformLogout({ await platformLogout({
ctx, ctx,
userId: ctx.user._id, userId: ctx.user._id,
sessionId: authCookie.sessionId,
keepActiveSession: true, keepActiveSession: true,
}) })
} }