diff --git a/packages/backend-core/src/redis/init.ts b/packages/backend-core/src/redis/init.ts index 55ffe3dd12..a4f1fecc17 100644 --- a/packages/backend-core/src/redis/init.ts +++ b/packages/backend-core/src/redis/init.ts @@ -7,7 +7,9 @@ let userClient: Client, cacheClient: Client, writethroughClient: Client, lockClient: Client, - socketClient: Client + socketClient: Client, + inviteClient: Client, + passwordResetClient: Client async function init() { userClient = await new Client(utils.Databases.USER_CACHE).init() @@ -20,6 +22,8 @@ async function init() { utils.Databases.SOCKET_IO, utils.SelectableDatabase.SOCKET_IO ).init() + inviteClient = await new Client(utils.Databases.INVITATIONS).init() + passwordResetClient = await new Client(utils.Databases.PW_RESETS).init() } export async function shutdown() { @@ -30,6 +34,8 @@ export async function shutdown() { if (writethroughClient) await writethroughClient.finish() if (lockClient) await lockClient.finish() if (socketClient) await socketClient.finish() + if (inviteClient) await inviteClient.finish() + if (passwordResetClient) await passwordResetClient.finish() } process.on("exit", async () => { @@ -84,3 +90,17 @@ export async function getSocketClient() { } return socketClient } + +export async function getInviteClient() { + if (!inviteClient) { + await init() + } + return inviteClient +} + +export async function getPasswordResetClient() { + if (!passwordResetClient) { + await init() + } + return passwordResetClient +} diff --git a/packages/backend-core/src/redis/invite.ts b/packages/backend-core/src/redis/invite.ts index db36d3dfa6..6e6a1fd9e9 100644 --- a/packages/backend-core/src/redis/invite.ts +++ b/packages/backend-core/src/redis/invite.ts @@ -1,5 +1,6 @@ -import { redis, utils, tenancy } from "../" +import { utils, tenancy } from "../" import env from "../environment" +import { getInviteClient } from "./init" const TTL_SECONDS = 60 * 60 * 24 * 7 @@ -12,23 +13,13 @@ interface InviteWithCode extends Invite { code: string } -let client: redis.Client - -async function getClient(): Promise { - if (!client) { - client = new redis.Client(redis.utils.Databases.INVITATIONS) - await client.init() - } - return client -} - /** * Given an invite code and invite body, allow the update an existing/valid invite in redis * @param inviteCode The invite code for an invite in redis * @param value The body of the updated user invitation */ export async function updateInviteCode(code: string, value: Invite) { - const client = await getClient() + const client = await getInviteClient() await client.store(code, value, TTL_SECONDS) } @@ -42,7 +33,7 @@ export async function createInviteCode( email: string, info: any ): Promise { - const client = await getClient() + const client = await getInviteClient() const code = utils.newid() await client.store(code, { email, info }, TTL_SECONDS) return code @@ -54,7 +45,7 @@ export async function createInviteCode( * @return If the code is valid then an email address will be returned. */ export async function getInviteCode(code: string): Promise { - const client = await getClient() + const client = await getInviteClient() const value = (await client.get(code)) as Invite | undefined if (!value) { throw "Invitation is not valid or has expired, please request a new one." @@ -63,7 +54,7 @@ export async function getInviteCode(code: string): Promise { } export async function deleteInviteCode(code: string) { - const client = await getClient() + const client = await getInviteClient() await client.delete(code) } @@ -71,7 +62,7 @@ export async function deleteInviteCode(code: string) { Get all currently available user invitations for the current tenant. **/ export async function getInviteCodes(): Promise { - const client = await getClient() + const client = await getInviteClient() const invites: { key: string; value: Invite }[] = await client.scan() const results: InviteWithCode[] = invites.map(invite => { diff --git a/packages/backend-core/src/redis/passwordReset.ts b/packages/backend-core/src/redis/passwordReset.ts index 63c3371bba..243b73c529 100644 --- a/packages/backend-core/src/redis/passwordReset.ts +++ b/packages/backend-core/src/redis/passwordReset.ts @@ -1,4 +1,5 @@ -import { redis, utils } from "../" +import { utils } from "../" +import { getPasswordResetClient } from "./init" const TTL_SECONDS = 60 * 60 @@ -7,16 +8,6 @@ interface PasswordReset { info: any } -let client: redis.Client - -async function getClient(): Promise { - if (!client) { - client = new redis.Client(redis.utils.Databases.PW_RESETS) - await client.init() - } - return client -} - /** * Given a user ID this will store a code (that is returned) for an hour in redis. * The user can then return this code for resetting their password (through their reset link). @@ -28,7 +19,7 @@ export async function createResetPasswordCode( userId: string, info: any ): Promise { - const client = await getClient() + const client = await getPasswordResetClient() const code = utils.newid() await client.store(code, { userId, info }, TTL_SECONDS) return code @@ -42,7 +33,7 @@ export async function createResetPasswordCode( export async function getResetPasswordCode( code: string ): Promise { - const client = await getClient() + const client = await getPasswordResetClient() const value = (await client.get(code)) as PasswordReset | undefined if (!value) { throw "Provided information is not valid, cannot reset password - please try again."