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 | */ |
| 18 | import { Account } from 'jami-web-common/dist/Account'; |
| 19 | import { HttpStatusCode } from 'jami-web-common/dist/enums/http-status-code'; |
| 20 | import { createContext, useCallback, useContext, useEffect, 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'; |
| 24 | import { apiUrl } from '../utils/constants'; |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 25 | import { WithChildren } from '../utils/utils'; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 26 | |
| 27 | interface IAuthContext { |
| 28 | token: string; |
| 29 | account: Account; |
| 30 | logout: () => void; |
| 31 | } |
| 32 | |
| 33 | const AuthContext = createContext<IAuthContext | undefined>(undefined); |
| 34 | |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 35 | export default ({ children }: WithChildren) => { |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 36 | const [token, setToken] = useState<string | undefined>(); |
| 37 | const [account, setAccount] = useState<Account | undefined>(); |
| 38 | const navigate = useNavigate(); |
| 39 | |
| 40 | const logout = useCallback(() => { |
| 41 | localStorage.removeItem('accessToken'); |
simon | 3f5f3e7 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 42 | navigate('/login'); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 43 | }, [navigate]); |
| 44 | |
| 45 | useEffect(() => { |
| 46 | const accessToken = localStorage.getItem('accessToken'); |
| 47 | |
| 48 | if (!accessToken) { |
| 49 | console.warn('Missing authentication JWT. Redirecting to login page...'); |
| 50 | logout(); |
| 51 | } else { |
| 52 | setToken(accessToken); |
| 53 | } |
| 54 | }, [logout]); |
| 55 | |
| 56 | useEffect(() => { |
| 57 | if (token) { |
| 58 | const getAccount = async () => { |
| 59 | const url = new URL('/account', apiUrl); |
| 60 | const response = await fetch(url, { |
| 61 | method: 'GET', |
| 62 | headers: { |
| 63 | Authorization: `Bearer ${token}`, |
| 64 | }, |
| 65 | }); |
| 66 | |
| 67 | if (response.status === HttpStatusCode.Ok) { |
| 68 | const serializedAccount = await response.json(); |
| 69 | const account = Account.from(serializedAccount); |
| 70 | setAccount(account); |
| 71 | } else { |
| 72 | throw new Error(response.statusText); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | getAccount().catch((e) => { |
| 77 | console.error('Error while retrieving account: ', e); |
| 78 | logout(); |
| 79 | }); |
| 80 | } |
| 81 | }, [token, logout]); |
| 82 | |
| 83 | if (!token || !account) { |
| 84 | return <ProcessingRequest open />; |
| 85 | } |
| 86 | |
| 87 | return ( |
| 88 | <AuthContext.Provider |
| 89 | value={{ |
| 90 | token, |
| 91 | logout, |
| 92 | account, |
| 93 | }} |
| 94 | > |
Issam E. Maghni | 09a3a1f | 2022-11-02 04:56:21 +0000 | [diff] [blame] | 95 | {children} |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 96 | </AuthContext.Provider> |
| 97 | ); |
| 98 | }; |
| 99 | |
| 100 | export function useAuthContext(dontThrowIfUndefined: true): IAuthContext | undefined; |
| 101 | export function useAuthContext(): IAuthContext; |
| 102 | export function useAuthContext(dontThrowIfUndefined?: true) { |
| 103 | const authContext = useContext(AuthContext); |
| 104 | if (!authContext && !dontThrowIfUndefined) { |
| 105 | throw new Error('AuthContext is not provided'); |
| 106 | } |
| 107 | return authContext; |
| 108 | } |