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

View File

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