Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [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 | */ |
| 18 | |
Ziwei Wang | 0d8889d | 2023-02-21 17:15:00 -0500 | [diff] [blame] | 19 | import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 20 | import axios from 'axios'; |
Ziwei Wang | 0d8889d | 2023-02-21 17:15:00 -0500 | [diff] [blame] | 21 | import { AccessToken, IAdminConfigState } from 'jami-web-common'; |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 22 | import { HttpStatusCode } from 'jami-web-common'; |
| 23 | import { useContext } from 'react'; |
| 24 | import { useNavigate } from 'react-router-dom'; |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 25 | |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 26 | import { AlertSnackbarContext } from '../contexts/AlertSnackbarProvider'; |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 27 | import { apiUrl } from '../utils/constants'; |
Ziwei Wang | 0d8889d | 2023-02-21 17:15:00 -0500 | [diff] [blame] | 28 | |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 29 | export const useSetupAdminMutation = () => { |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 30 | const navigate = useNavigate(); |
| 31 | const { setAlertContent } = useContext(AlertSnackbarContext); |
| 32 | |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 33 | return useMutation({ |
| 34 | mutationKey: ['admin', 'setup'], |
| 35 | mutationFn: async (password: string) => { |
Ziwei Wang | b7fd04c | 2023-02-28 16:53:30 -0500 | [diff] [blame] | 36 | const { data } = await axios.post('/admin/create', { password }, { baseURL: apiUrl }); |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 37 | return data; |
| 38 | }, |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 39 | onSuccess: () => { |
| 40 | //TODO: Do not login the admin for now in case we need to implement multi-factor authentication |
| 41 | navigate('/admin/login'); |
| 42 | }, |
| 43 | onError: (e: any) => { |
| 44 | if (e.response?.status === HttpStatusCode.BadRequest) { |
| 45 | setAlertContent({ |
| 46 | messageI18nKey: 'login_invalid_password', |
| 47 | severity: 'error', |
| 48 | alertOpen: true, |
| 49 | }); |
| 50 | } else { |
| 51 | setAlertContent({ |
| 52 | messageI18nKey: 'unknown_error_alert', |
| 53 | severity: 'error', |
| 54 | alertOpen: true, |
| 55 | }); |
| 56 | } |
| 57 | }, |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 58 | }); |
| 59 | }; |
| 60 | |
| 61 | export const useLoginAdminMutation = () => { |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 62 | const navigate = useNavigate(); |
| 63 | const { setAlertContent } = useContext(AlertSnackbarContext); |
| 64 | |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 65 | return useMutation({ |
| 66 | mutationKey: ['admin', 'login'], |
| 67 | mutationFn: async (password: string) => { |
Ziwei Wang | b7fd04c | 2023-02-28 16:53:30 -0500 | [diff] [blame] | 68 | const { data } = await axios.post<AccessToken>('/admin/login', { password }, { baseURL: apiUrl }); |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 69 | return data; |
| 70 | }, |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 71 | onSuccess: (data) => { |
| 72 | localStorage.setItem('adminAccessToken', data.accessToken); |
| 73 | navigate('/admin'); |
| 74 | }, |
| 75 | onError: (e: any) => { |
| 76 | if (e.response?.status === HttpStatusCode.Forbidden) { |
| 77 | setAlertContent({ |
| 78 | messageI18nKey: 'admin_page_setup_not_complete', |
| 79 | severity: 'error', |
| 80 | alertOpen: true, |
| 81 | }); |
| 82 | } else if (e.response?.status === HttpStatusCode.BadRequest) { |
| 83 | setAlertContent({ |
| 84 | messageI18nKey: 'password_input_helper_text_empty', |
| 85 | severity: 'error', |
| 86 | alertOpen: true, |
| 87 | }); |
| 88 | } else if (e.response?.status === HttpStatusCode.Unauthorized) { |
| 89 | setAlertContent({ |
| 90 | messageI18nKey: 'login_invalid_password', |
| 91 | severity: 'error', |
| 92 | alertOpen: true, |
| 93 | }); |
| 94 | } else { |
| 95 | setAlertContent({ |
| 96 | messageI18nKey: 'unknown_error_alert', |
| 97 | severity: 'error', |
| 98 | alertOpen: true, |
| 99 | }); |
| 100 | } |
| 101 | }, |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 102 | }); |
| 103 | }; |
| 104 | |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 105 | //For non-react components that need to get the admin setup status |
| 106 | export const checkAdminSetupStatus = async () => { |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 107 | try { |
Ziwei Wang | b7fd04c | 2023-02-28 16:53:30 -0500 | [diff] [blame] | 108 | const { data } = await axios.get<{ isSetupComplete: boolean }>('/admin/check', { baseURL: apiUrl }); |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 109 | return data.isSetupComplete; |
| 110 | } catch (e) { |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 111 | console.log(e); |
Ziwei Wang | 1455d97 | 2023-02-21 14:48:32 -0500 | [diff] [blame] | 112 | } |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | //For react components that can invoke hooks |
| 116 | export const useCheckAdminSetupStatusQuery = () => { |
| 117 | return useQuery({ |
| 118 | queryKey: ['admin', 'status'], |
| 119 | queryFn: async () => { |
Ziwei Wang | b7fd04c | 2023-02-28 16:53:30 -0500 | [diff] [blame] | 120 | const { data } = await axios.get<{ isSetupComplete: boolean }>('/admin/check', { baseURL: apiUrl }); |
Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [diff] [blame] | 121 | return data.isSetupComplete; |
| 122 | }, |
| 123 | }); |
| 124 | }; |
Ziwei Wang | 0d8889d | 2023-02-21 17:15:00 -0500 | [diff] [blame] | 125 | |
| 126 | export const useGetAdminConfigQuery = () => { |
| 127 | const token = localStorage.getItem('adminAccessToken'); |
| 128 | const navigate = useNavigate(); |
| 129 | const { setAlertContent } = useContext(AlertSnackbarContext); |
| 130 | |
| 131 | return useQuery({ |
| 132 | queryKey: ['admin', 'config'], |
| 133 | queryFn: async () => { |
| 134 | const { data } = await axios.get('/admin/config', { |
| 135 | baseURL: apiUrl, |
| 136 | //TODO: create a intercepted axios instance for auth-required requests |
| 137 | headers: { |
| 138 | Authorization: `Bearer ${token}`, |
| 139 | }, |
| 140 | }); |
| 141 | return data; |
| 142 | }, |
| 143 | onError: (e: any) => { |
| 144 | if (e.response.status === HttpStatusCode.Unauthorized) { |
| 145 | localStorage.removeItem('adminAccessToken'); |
| 146 | navigate('/admin/login'); |
| 147 | } else { |
| 148 | setAlertContent({ |
| 149 | messageI18nKey: 'unknown_error_alert', |
| 150 | severity: 'error', |
| 151 | alertOpen: true, |
| 152 | }); |
| 153 | } |
| 154 | }, |
| 155 | }); |
| 156 | }; |
| 157 | |
| 158 | export const useUpdateAdminConfigMutation = () => { |
| 159 | const token = localStorage.getItem('adminAccessToken'); |
| 160 | const navigate = useNavigate(); |
| 161 | const { setAlertContent } = useContext(AlertSnackbarContext); |
| 162 | const queryClient = useQueryClient(); |
| 163 | |
| 164 | return useMutation({ |
| 165 | mutationFn: async (adminConfig: Partial<IAdminConfigState>) => { |
| 166 | const response = await axios.patch('/admin/config', adminConfig, { |
| 167 | baseURL: apiUrl, |
| 168 | headers: { |
| 169 | Authorization: `Bearer ${token}`, |
| 170 | }, |
| 171 | }); |
| 172 | return response; |
| 173 | }, |
| 174 | onSuccess: () => { |
| 175 | queryClient.invalidateQueries({ queryKey: ['admin', 'config'] }); |
| 176 | }, |
| 177 | onError: (e: any) => { |
| 178 | if (e.repsponse.status === HttpStatusCode.Unauthorized) { |
| 179 | localStorage.removeItem('adminAccessToken'); |
| 180 | navigate('/admin/login'); |
| 181 | } else { |
| 182 | setAlertContent({ |
| 183 | messageI18nKey: 'unknown_error_alert', |
| 184 | severity: 'error', |
| 185 | alertOpen: true, |
| 186 | }); |
| 187 | } |
| 188 | }, |
| 189 | }); |
| 190 | }; |