-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathauthentication.ts
More file actions
30 lines (28 loc) · 1.17 KB
/
authentication.ts
File metadata and controls
30 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type {
IFormattedInstanceConfiguration,
TInstanceAuthenticationModes,
TInstanceConfigurationKeys,
} from "@plane/types";
/**
* Checks if a given authentication method can be disabled.
* @param configKey - The configuration key to check.
* @param authModes - The authentication modes to check.
* @param formattedConfig - The formatted configuration to check.
* @returns True if the authentication method can be disabled, false otherwise.
*/
export const canDisableAuthMethod = (
configKey: TInstanceConfigurationKeys,
authModes: TInstanceAuthenticationModes[],
formattedConfig: IFormattedInstanceConfiguration | undefined
): boolean => {
// Count currently enabled methods
const enabledCount = authModes.reduce((count, method) => {
const enabledKey = method.enabledConfigKey;
if (!enabledKey || !formattedConfig) return count;
const isEnabled = Boolean(parseInt(formattedConfig[enabledKey] ?? "0"));
return isEnabled ? count + 1 : count;
}, 0);
// If trying to disable and only 1 method is enabled, prevent it
const isCurrentlyEnabled = Boolean(parseInt(formattedConfig?.[configKey] ?? "0"));
return !(isCurrentlyEnabled && enabledCount === 1);
};