diff --git a/src/frontend/src/components/Header/index.tsx b/src/frontend/src/components/Header/index.tsx index 1866e5602..5dcc81e4e 100644 --- a/src/frontend/src/components/Header/index.tsx +++ b/src/frontend/src/components/Header/index.tsx @@ -1,36 +1,62 @@ import React from "react"; import { Link } from "react-router-dom"; +import { connect } from "react-redux"; +import { oeciLogout } from "../../service/cookie-service"; +import store, { RootState } from "../../redux/store"; +import { setLoggedIn } from "../../redux/authSlice"; + import Logo from "../Logo"; -export default class Header extends React.Component { - public render() { - return ( -
- +
+ ); } + +export default connect(mapStateToProps)(Header); diff --git a/src/frontend/src/redux/authSlice.ts b/src/frontend/src/redux/authSlice.ts new file mode 100644 index 000000000..b1449bd4a --- /dev/null +++ b/src/frontend/src/redux/authSlice.ts @@ -0,0 +1,20 @@ +import { createSlice } from '@reduxjs/toolkit'; +import { hasOeciToken } from '../service/cookie-service'; + +const initialState = { + loggedIn: hasOeciToken(), +}; + +const authSlice = createSlice({ + name: 'auth', + initialState, + reducers: { + setLoggedIn: (state, action) => { + state.loggedIn = action.payload; + }, + }, +}); + +export const { setLoggedIn } = authSlice.actions; + +export default authSlice.reducer; diff --git a/src/frontend/src/redux/store.tsx b/src/frontend/src/redux/store.tsx index 7e56d8a4f..9a1ad83fd 100644 --- a/src/frontend/src/redux/store.tsx +++ b/src/frontend/src/redux/store.tsx @@ -9,6 +9,7 @@ import summarySlice from "./summarySlice"; import editingSlice from "./editingSlice"; import statsSlice from "./statsSlice"; import searchFormSlice from "./searchFormSlice"; +import authSlice from "./authSlice"; export const clearAllData = createAction("CLEAR_ALL_DATA"); @@ -19,6 +20,7 @@ const appReducer = combineReducers({ editing: editingSlice, stats: statsSlice, searchForm: searchFormSlice, + auth: authSlice, }); // https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store diff --git a/src/frontend/src/service/cookie-service.ts b/src/frontend/src/service/cookie-service.ts index f1c14a85f..9ed6928b3 100644 --- a/src/frontend/src/service/cookie-service.ts +++ b/src/frontend/src/service/cookie-service.ts @@ -1,4 +1,5 @@ import history from "../service/history"; +import store, { clearAllData } from "../redux/store"; interface Cookie { [key: string]: string; @@ -38,3 +39,13 @@ export function checkOeciRedirect() { history.replace("/oeci"); } } + +export function isLoggedIn() { + return hasOeciToken(); +} + +export function oeciLogout() { + removeCookie(); + store.dispatch(clearAllData()); + history.replace("/oeci"); +} diff --git a/src/frontend/src/service/oeci.ts b/src/frontend/src/service/oeci.ts index 5c195fd99..837d3091c 100644 --- a/src/frontend/src/service/oeci.ts +++ b/src/frontend/src/service/oeci.ts @@ -1,6 +1,8 @@ import apiService from "./api-service"; import { hasOeciToken } from "./cookie-service"; import history from "./history"; +import store from "../redux/store"; +import { setLoggedIn } from "../redux/authSlice"; export default function oeciLogIn(username: string, password: string): any { return apiService(() => {}, { @@ -10,6 +12,7 @@ export default function oeciLogIn(username: string, password: string): any { withCredentials: true, }).then((response: any) => { if (hasOeciToken()) { + store.dispatch(setLoggedIn(true)); history.push("/record-search"); } });