Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 18 | import axios from 'axios'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 19 | import { passwordStrength } from 'check-password-strength'; |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 20 | import { AccessToken } from 'jami-web-common'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 21 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 22 | import { PasswordStrength } from '../enums/passwordStrength'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 23 | import { apiUrl } from './constants'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 24 | |
| 25 | interface PasswordStrengthResult { |
| 26 | id: number; |
| 27 | value: string; |
| 28 | contains: string[]; |
| 29 | length: number; |
| 30 | } |
| 31 | |
| 32 | export interface PasswordCheckResult { |
| 33 | strong: boolean; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 34 | valueCode: StrengthValueCode; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 37 | export type StrengthValueCode = 'default' | 'too_weak' | 'weak' | 'medium' | 'strong'; |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 38 | export type LoginMethod = 'Jami' | 'JAMS'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 39 | |
| 40 | const idToStrengthValueCode: StrengthValueCode[] = ['too_weak', 'weak', 'medium', 'strong']; |
| 41 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 42 | export function checkIfUserameIsRegistered(username: string) { |
Ziwei Wang | 3c94119 | 2023-02-15 12:26:43 -0500 | [diff] [blame] | 43 | return axios.get(`/ns/username/availability/${username}`, { baseURL: apiUrl }); |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | export function checkPasswordStrength(password: string): PasswordCheckResult { |
| 47 | const strengthResult: PasswordStrengthResult = passwordStrength(password); |
| 48 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 49 | return { |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 50 | strong: strengthResult.id === PasswordStrength.Strong.valueOf(), |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 51 | valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default', |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 52 | }; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 53 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 54 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 55 | export async function registerUser(username: string, password: string, isJams: boolean) { |
| 56 | return axios.post('/auth/new-account', { username, password, isJams }, { baseURL: apiUrl }); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 59 | export async function loginUser(username: string, password: string, isJams: boolean) { |
| 60 | return axios.post<AccessToken>('/auth/login', { username, password, isJams }, { baseURL: apiUrl }); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 63 | export function getAccessToken(): string | undefined { |
| 64 | return localStorage.getItem('accessToken') ?? undefined; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | export function setAccessToken(accessToken: string): void { |
| 68 | localStorage.setItem('accessToken', accessToken); |
| 69 | } |