Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 5x 5x 20x 20x 20x 20x 20x 20x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 20x | import React, { useState, useEffect, createContext, ReactNode } from 'react'; import { LoggedUser } from '../types'; import { axiosInstance } from '../utils/axiosInstance'; export interface CASContext { user: LoggedUser | undefined; logout: () => void; token: string | undefined; refreshToken: string | undefined; isFetchingToken: boolean; role: string | undefined; } export const CASContext = createContext<CASContext | undefined>(undefined); export interface CASProviderProps { children: ReactNode; } export const CASProvider = ({ children }: CASProviderProps) => { const [user, setUser] = useState<LoggedUser>(); const [token, setToken] = useState<string | undefined>(); const [refreshToken, setRefreshToken] = useState<string | undefined>(); const [role, setRole] = useState<string | undefined>(undefined); const [isFetchingToken, setIsFetchingToken] = useState(false); useEffect(() => { const login = async () => { const urlParams = new URLSearchParams(window.location.search); const ticket = urlParams.get('ticket'); Eif (!ticket) { redirectToCASLoginService(); } try { Eif (!localStorage.getItem('userToken')) { setIsFetchingToken(true); const { data: user } = await axiosInstance.get<LoggedUser & { token: string; refreshToken: string }>( `${process.env.REACT_APP_API_URL}/token?ticket=${ticket}`, ); setUser({ authorityRole: user.authorityRole, email: user.email, id: user.id }); localStorage.setItem('userToken', user.token); localStorage.setItem('userPrivilige', user.authorityRole); localStorage.setItem('refreshToken', user.refreshToken); setIsFetchingToken(false); } const token = localStorage.getItem('userToken'); const refreshToken = localStorage.getItem('refreshToken'); const role = localStorage.getItem('userPrivilige'); token && setToken(token); refreshToken && setRefreshToken(refreshToken); role && setRole(role); } catch (e) { console.log(e); } }; login(); }, []); function logout() { localStorage.removeItem('userToken'); localStorage.removeItem('refreshToken'); localStorage.removeItem('userPrivilige'); redirectToCASLogoutService(); } function redirectToCASLogoutService() { window.location.replace(`https://cas.amu.edu.pl/cas/logout?service=${window.origin}`); } function redirectToCASLoginService() { window.location.replace(`https://cas.amu.edu.pl/cas/login?service=${window.origin}&locale=pl`); } return ( <CASContext.Provider value={{ user, token, refreshToken, logout, isFetchingToken, role }}> {children} </CASContext.Provider> ); }; |