frontend/src/businesslogic/BusinessLogicProvider.tsx

77 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-06-17 15:19:51 +02:00
import BusinessLogicContext from "./BusinessLogicContext";
import React, { Component } from "react";
2020-07-24 17:00:06 +02:00
import { User } from "./types/user";
2020-06-17 15:19:51 +02:00
export interface BuisnessProvided {
2020-06-20 11:13:12 +02:00
states: BusinessState;
2020-06-20 14:10:58 +02:00
reducers: {
userlogout: () => void;
};
2020-06-17 15:19:51 +02:00
}
2020-06-20 11:13:12 +02:00
interface BusinessState {
2020-06-17 15:19:51 +02:00
user: User | null;
}
interface Props {}
2020-06-20 11:13:12 +02:00
class BusinessLogicProvider extends Component<Props, BusinessState> {
2020-06-17 15:19:51 +02:00
constructor(props: Props) {
super(props);
this.state = {
user: null,
};
}
componentDidMount() {
this.login();
}
login() {
const urlParams = new URLSearchParams(window.location.search);
const ticket = urlParams.get("ticket");
2020-06-20 11:13:12 +02:00
if (!ticket) {
2020-06-20 11:27:19 +02:00
this.redirectToCASLoginService();
2020-06-20 11:13:12 +02:00
}
2020-06-20 11:27:19 +02:00
if (ticket) {
2020-06-17 15:19:51 +02:00
this.setState({ user: { ticket } });
}
}
logout() {
2020-06-20 11:27:19 +02:00
this.redirectToCASLogoutService();
}
redirectToCASLogoutService() {
2020-06-20 14:10:58 +02:00
window.location.replace(
`https://cas.amu.edu.pl/cas/logout?service=${window.origin}`
);
2020-06-17 15:19:51 +02:00
}
2020-06-20 11:27:19 +02:00
redirectToCASLoginService() {
2020-06-20 14:10:58 +02:00
window.location.replace(
`https://cas.amu.edu.pl/cas/login?service=${window.origin}&locale=pl`
);
2020-06-20 11:27:19 +02:00
}
2020-06-17 15:19:51 +02:00
render() {
return (
<BusinessLogicContext.Provider
value={{
states: this.state,
2020-06-20 14:10:58 +02:00
reducers: {
userlogout: () => {
this.logout();
},
2020-06-17 15:19:51 +02:00
},
}}
>
{this.props.children}
</BusinessLogicContext.Provider>
);
}
}
export default BusinessLogicProvider;