simon | 416d079 | 2022-11-03 02:46:18 -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, { AxiosInstance } from 'axios'; |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 19 | import { HttpStatusCode, IAccount } from 'jami-web-common'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 20 | import { useCallback, useEffect, useMemo, useState } from 'react'; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 21 | import { useNavigate } from 'react-router-dom'; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 22 | |
| 23 | import ProcessingRequest from '../components/ProcessingRequest'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 24 | import { createOptionalContext } from '../hooks/createOptionalContext'; |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 25 | import { Account } from '../models/account'; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 26 | import { apiUrl } from '../utils/constants'; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 27 | import { WithChildren } from '../utils/utils'; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 28 | |
| 29 | interface IAuthContext { |
| 30 | token: string; |
| 31 | account: Account; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 32 | accountId: string; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 33 | logout: () => void; |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 34 | axiosInstance: AxiosInstance; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 35 | } |
| 36 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 37 | const optionalAuthContext = createOptionalContext<IAuthContext>('AuthContext'); |
| 38 | const AuthContext = optionalAuthContext.Context; |
| 39 | export const useAuthContext = optionalAuthContext.useOptionalContext; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 40 | |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 41 | export default ({ children }: WithChildren) => { |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 42 | const [token, setToken] = useState<string | undefined>(); |
| 43 | const [account, setAccount] = useState<Account | undefined>(); |
| 44 | const navigate = useNavigate(); |
| 45 | |
| 46 | const logout = useCallback(() => { |
| 47 | localStorage.removeItem('accessToken'); |
simon | 3f5f3e7 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 48 | navigate('/login'); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 49 | }, [navigate]); |
| 50 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 51 | const axiosInstance = useMemo(() => { |
| 52 | if (!token) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | const instance = axios.create({ |
| 57 | baseURL: apiUrl, |
| 58 | headers: { |
| 59 | Authorization: `Bearer ${token}`, |
| 60 | }, |
| 61 | }); |
| 62 | |
| 63 | instance.interceptors.response.use( |
| 64 | (res) => res, |
| 65 | (e) => { |
| 66 | switch (e.response?.status) { |
| 67 | case HttpStatusCode.Unauthorized: |
| 68 | logout(); |
| 69 | break; |
| 70 | } |
Misha Krieger-Raynauld | 0c2e986 | 2022-11-29 19:04:44 -0500 | [diff] [blame] | 71 | throw e; |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 72 | } |
| 73 | ); |
| 74 | |
| 75 | return instance; |
| 76 | }, [token, logout]); |
| 77 | |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 78 | useEffect(() => { |
| 79 | const accessToken = localStorage.getItem('accessToken'); |
| 80 | |
| 81 | if (!accessToken) { |
| 82 | console.warn('Missing authentication JWT. Redirecting to login page...'); |
| 83 | logout(); |
| 84 | } else { |
| 85 | setToken(accessToken); |
| 86 | } |
| 87 | }, [logout]); |
| 88 | |
| 89 | useEffect(() => { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 90 | if (!axiosInstance) { |
| 91 | return; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 92 | } |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 93 | |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 94 | axiosInstance.get<IAccount>('/account').then(({ data }) => setAccount(Account.fromInterface(data))); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 95 | }, [axiosInstance, logout]); |
| 96 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 97 | const value = useMemo(() => { |
| 98 | if (!token || !account || !axiosInstance) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | return { |
| 103 | token, |
| 104 | logout, |
| 105 | account, |
| 106 | accountId: account.id, |
| 107 | axiosInstance, |
| 108 | }; |
| 109 | }, [token, logout, account, axiosInstance]); |
| 110 | |
| 111 | if (!value) { |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 112 | return <ProcessingRequest open />; |
| 113 | } |
| 114 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 115 | return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 116 | }; |