blob: 470486fc4e193db157f0136f7c6bda7286880e65 [file] [log] [blame]
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { passwordStrength } from 'check-password-strength';
19import { LookupResolveValue } from 'jami-web-common';
20
21import { PasswordStrength } from '../enums/password-strength';
22import { StatusCode } from '../enums/status-code';
23
24interface PasswordStrengthResult {
25 id: number;
26 value: string;
27 contains: string[];
28 length: number;
29}
30
31export interface PasswordCheckResult {
32 strong: boolean;
33 value: string;
34}
35
36// TODO: Find a way to do it differently or remove this check from account creation.
37// It doesn't work if the server has secured this path, so I tweaked the server for test.
38// The tweak is to remove secured of apiRouter middleware in the server (app.ts).
39export async function isNameRegistered(name: string): Promise<boolean> {
40 try {
41 const response: Response = await fetch(`api/ns/name/${name}`);
42 if (response.status === StatusCode.Ok) {
43 const data: LookupResolveValue = await response.json();
44 return data.name === name;
45 }
46 return false;
47 } catch (err) {
48 return true;
49 }
50}
51
52export function checkPasswordStrength(password: string): PasswordCheckResult {
53 const strengthResult: PasswordStrengthResult = passwordStrength(password);
54
55 const checkResult: PasswordCheckResult = {
56 strong: strengthResult.id === PasswordStrength.Strong.valueOf(),
57 value: strengthResult.value,
58 };
59
60 return checkResult;
61}