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