encapsulate global logout function
This commit is contained in:
parent
ed9574b815
commit
81f49dcdf1
|
@ -240,22 +240,20 @@ 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(
|
||||||
|
|
|
@ -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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue