Check username and password strength
Changes:
- Check username availability
- Measure password strength
GitLab: #12
Change-Id: Ia384aaec01186cc874ddd6dfb4997da91a333a66
diff --git a/client/src/enums/password-strength.ts b/client/src/enums/password-strength.ts
new file mode 100644
index 0000000..d5805ba
--- /dev/null
+++ b/client/src/enums/password-strength.ts
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2022 Savoir-faire Linux Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+export enum PasswordStrength {
+ TooWeak,
+ Weak,
+ Medium,
+ Strong,
+}
diff --git a/client/src/enums/status-code.ts b/client/src/enums/status-code.ts
new file mode 100644
index 0000000..d0db9b4
--- /dev/null
+++ b/client/src/enums/status-code.ts
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2022 Savoir-faire Linux Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+export enum StatusCode {
+ Ok = 200,
+ Created = 201,
+ Accepted = 202,
+ NoContent = 204,
+ BadRequest = 400,
+ Unauthorized = 401,
+ Forbidden = 403,
+ NotFound = 404,
+ NotAcceptable = 406,
+ Conflict = 409,
+ Gone = 410,
+ ImATeapot = 418,
+ TooManyRequests = 429,
+ InternalServerError = 500,
+ NotImplemented = 501,
+}
diff --git a/client/src/utils/auth.ts b/client/src/utils/auth.ts
new file mode 100644
index 0000000..470486f
--- /dev/null
+++ b/client/src/utils/auth.ts
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2022 Savoir-faire Linux Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+import { passwordStrength } from 'check-password-strength';
+import { LookupResolveValue } from 'jami-web-common';
+
+import { PasswordStrength } from '../enums/password-strength';
+import { StatusCode } from '../enums/status-code';
+
+interface PasswordStrengthResult {
+ id: number;
+ value: string;
+ contains: string[];
+ length: number;
+}
+
+export interface PasswordCheckResult {
+ strong: boolean;
+ value: string;
+}
+
+// TODO: Find a way to do it differently or remove this check from account creation.
+// It doesn't work if the server has secured this path, so I tweaked the server for test.
+// The tweak is to remove secured of apiRouter middleware in the server (app.ts).
+export async function isNameRegistered(name: string): Promise<boolean> {
+ try {
+ const response: Response = await fetch(`api/ns/name/${name}`);
+ if (response.status === StatusCode.Ok) {
+ const data: LookupResolveValue = await response.json();
+ return data.name === name;
+ }
+ return false;
+ } catch (err) {
+ return true;
+ }
+}
+
+export function checkPasswordStrength(password: string): PasswordCheckResult {
+ const strengthResult: PasswordStrengthResult = passwordStrength(password);
+
+ const checkResult: PasswordCheckResult = {
+ strong: strengthResult.id === PasswordStrength.Strong.valueOf(),
+ value: strengthResult.value,
+ };
+
+ return checkResult;
+}
diff --git a/client/src/utils/constants.ts b/client/src/utils/constants.ts
new file mode 100644
index 0000000..06d1b92
--- /dev/null
+++ b/client/src/utils/constants.ts
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2022 Savoir-faire Linux Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program. If not, see
+ * <https://www.gnu.org/licenses/>.
+ */
+export const jamiUsernamePattern = '^[a-zA-Z0-9-_]{3,32}$';
+
+export const inputWidth = 260;
+
+export const jamiLogoDefaultSize = '512px';
diff --git a/client/src/utils/utils.ts b/client/src/utils/utils.ts
index c602020..8f2f1cb 100644
--- a/client/src/utils/utils.ts
+++ b/client/src/utils/utils.ts
@@ -15,7 +15,6 @@
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
-
import { ReactNode } from 'react';
export type WithChildren = {