blob: 8d4d863f68aadfcf540b4f65848645ddcbda5e38 [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';
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040019import { HttpStatusCode, LookupResolveValue } from 'jami-web-common';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040020
21import { PasswordStrength } from '../enums/password-strength';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040022
23interface PasswordStrengthResult {
24 id: number;
25 value: string;
26 contains: string[];
27 length: number;
28}
29
30export interface PasswordCheckResult {
31 strong: boolean;
32 value: string;
33}
34
35// TODO: Find a way to do it differently or remove this check from account creation.
36// It doesn't work if the server has secured this path, so I tweaked the server for test.
37// The tweak is to remove secured of apiRouter middleware in the server (app.ts).
38export async function isNameRegistered(name: string): Promise<boolean> {
39 try {
40 const response: Response = await fetch(`api/ns/name/${name}`);
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040041 if (response.status === HttpStatusCode.Ok) {
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040042 const data: LookupResolveValue = await response.json();
43 return data.name === name;
44 }
45 return false;
46 } catch (err) {
47 return true;
48 }
49}
50
51export function checkPasswordStrength(password: string): PasswordCheckResult {
52 const strengthResult: PasswordStrengthResult = passwordStrength(password);
53
54 const checkResult: PasswordCheckResult = {
55 strong: strengthResult.id === PasswordStrength.Strong.valueOf(),
56 value: strengthResult.value,
57 };
58
59 return checkResult;
60}