blob: 7336639f3ddadae9382386e3d1cd56c47064a884 [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 */
simon94fe53e2022-11-10 12:51:58 -050018import axios from 'axios';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040019import { passwordStrength } from 'check-password-strength';
Ziwei Wang9b4e2c12023-02-06 14:45:37 -050020import { AccessToken } from 'jami-web-common';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040021
simonf929a362022-11-18 16:53:45 -050022import { PasswordStrength } from '../enums/passwordStrength';
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040023import { apiUrl } from './constants';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040024
25interface PasswordStrengthResult {
26 id: number;
27 value: string;
28 contains: string[];
29 length: number;
30}
31
32export interface PasswordCheckResult {
33 strong: boolean;
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040034 valueCode: StrengthValueCode;
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040035}
36
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040037export type StrengthValueCode = 'default' | 'too_weak' | 'weak' | 'medium' | 'strong';
Gabriel Rochon7057b4f2022-11-21 13:28:01 -050038export type LoginMethod = 'Jami' | 'JAMS';
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040039
40const idToStrengthValueCode: StrengthValueCode[] = ['too_weak', 'weak', 'medium', 'strong'];
41
Ziwei Wang9b4e2c12023-02-06 14:45:37 -050042export function checkIfUserameIsRegistered(username: string) {
Ziwei Wang3c941192023-02-15 12:26:43 -050043 return axios.get(`/ns/username/availability/${username}`, { baseURL: apiUrl });
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040044}
45
46export function checkPasswordStrength(password: string): PasswordCheckResult {
47 const strengthResult: PasswordStrengthResult = passwordStrength(password);
48
simon94fe53e2022-11-10 12:51:58 -050049 return {
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040050 strong: strengthResult.id === PasswordStrength.Strong.valueOf(),
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040051 valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default',
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040052 };
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040053}
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040054
Ziwei Wang9b4e2c12023-02-06 14:45:37 -050055export async function registerUser(username: string, password: string, isJams: boolean) {
56 return axios.post('/auth/new-account', { username, password, isJams }, { baseURL: apiUrl });
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040057}
58
Ziwei Wang9b4e2c12023-02-06 14:45:37 -050059export async function loginUser(username: string, password: string, isJams: boolean) {
60 return axios.post<AccessToken>('/auth/login', { username, password, isJams }, { baseURL: apiUrl });
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040061}
62
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050063export function getAccessToken(): string | undefined {
64 return localStorage.getItem('accessToken') ?? undefined;
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040065}
66
67export function setAccessToken(accessToken: string): void {
68 localStorage.setItem('accessToken', accessToken);
69}