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