2020-10-21 18:34:59 +02:00
|
|
|
import React, { useState, useEffect, createContext, ReactNode } from 'react';
|
2020-12-12 19:57:16 +01:00
|
|
|
import { LoggedUser } from '../types';
|
2020-11-21 12:57:30 +01:00
|
|
|
import { axiosInstance } from '../utils/axiosInstance';
|
2020-08-12 04:13:14 +02:00
|
|
|
|
|
|
|
export interface CASContext {
|
2020-12-12 19:57:16 +01:00
|
|
|
user: LoggedUser | undefined;
|
2020-08-12 04:13:14 +02:00
|
|
|
logout: () => void;
|
2020-12-12 19:57:16 +01:00
|
|
|
token: string | undefined;
|
2020-12-29 01:06:39 +01:00
|
|
|
refreshToken: string | undefined;
|
2020-08-12 04:13:14 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 18:34:59 +02:00
|
|
|
export const CASContext = createContext<CASContext | undefined>(undefined);
|
2020-08-12 04:13:14 +02:00
|
|
|
|
|
|
|
export interface CASProviderProps {
|
2020-10-21 18:34:59 +02:00
|
|
|
children: ReactNode;
|
2020-08-12 04:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const CASProvider = ({ children }: CASProviderProps) => {
|
2020-12-12 19:57:16 +01:00
|
|
|
const [user, setUser] = useState<LoggedUser>();
|
|
|
|
const [token, setToken] = useState<string | undefined>();
|
2020-12-29 01:06:39 +01:00
|
|
|
const [refreshToken, setRefreshToken] = useState<string | undefined>();
|
2020-08-12 04:13:14 +02:00
|
|
|
useEffect(() => {
|
2020-11-22 18:22:07 +01:00
|
|
|
const login = async () => {
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
const ticket = urlParams.get('ticket');
|
|
|
|
if (!ticket) {
|
|
|
|
redirectToCASLoginService();
|
|
|
|
}
|
|
|
|
try {
|
2020-12-12 17:54:13 +01:00
|
|
|
if (!localStorage.getItem('userToken')) {
|
2020-12-29 01:06:39 +01:00
|
|
|
const { data: user } = await axiosInstance.get<LoggedUser & { token: string; refreshToken: string }>(
|
2020-12-12 19:57:16 +01:00
|
|
|
`${process.env.REACT_APP_API_URL}/token?ticket=${ticket}`,
|
|
|
|
);
|
2020-12-29 01:06:39 +01:00
|
|
|
console.log('token response: ', user);
|
2020-12-12 19:57:16 +01:00
|
|
|
setUser({ authorityRole: user.authorityRole, email: user.email, id: user.id });
|
|
|
|
localStorage.setItem('userToken', user.token);
|
|
|
|
localStorage.setItem('userPrivilige', user.authorityRole);
|
2020-12-29 01:06:39 +01:00
|
|
|
localStorage.setItem('refreshToken', user.refreshToken);
|
2020-11-22 18:22:07 +01:00
|
|
|
}
|
2020-12-12 19:57:16 +01:00
|
|
|
const token = localStorage.getItem('userToken');
|
2020-12-29 01:06:39 +01:00
|
|
|
const refreshToken = localStorage.getItem('refreshToken');
|
2020-12-12 19:57:16 +01:00
|
|
|
token && setToken(token);
|
2020-12-29 01:06:39 +01:00
|
|
|
refreshToken && setRefreshToken(refreshToken);
|
2020-11-22 18:22:07 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|
2020-08-12 04:13:14 +02:00
|
|
|
login();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
function logout() {
|
2020-12-12 19:57:16 +01:00
|
|
|
localStorage.removeItem('userToken');
|
2020-12-29 01:06:39 +01:00
|
|
|
localStorage.removeItem('refreshToken');
|
2020-12-16 00:50:09 +01:00
|
|
|
localStorage.removeItem('userPrivilige');
|
2020-08-12 04:13:14 +02:00
|
|
|
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`);
|
|
|
|
}
|
|
|
|
|
2020-12-29 01:06:39 +01:00
|
|
|
return <CASContext.Provider value={{ user, token, refreshToken, logout }}>{children}</CASContext.Provider>;
|
2020-08-12 04:13:14 +02:00
|
|
|
};
|