-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
446 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
import {Store, createStore, applyMiddleware} from 'redux' | ||
import createSagaMiddleware from 'redux-saga' | ||
|
||
|
||
// If you use react-router, don't forget to pass in your history type. | ||
import { History } from "history"; | ||
import { applyMiddleware, createStore, Store } from "redux"; | ||
// `react-router-redux` is deprecated, so we use `connected-react-router`. | ||
// This provides a Redux middleware which connects to our `react-router` instance. | ||
import { routerMiddleware } from 'connected-react-router' | ||
// We'll be using Redux Devtools. We can use the `composeWithDevTools()` | ||
// directive so we can pass our middleware along with it | ||
import { composeWithDevTools } from 'redux-devtools-extension' | ||
// If you use react-router, don't forget to pass in your history type. | ||
import { History } from 'history' | ||
import { ApplicationState, createRootReducer, rootSaga } from './stores' | ||
import { composeWithDevTools } from "redux-devtools-extension"; | ||
import createSagaMiddleware from "redux-saga"; | ||
import { ApplicationState, createRootReducer, rootSaga } from "./stores"; | ||
|
||
export default function configureStore(history: History, initialState: ApplicationState) : Store<ApplicationState> { | ||
// create the composing function for our middlewares | ||
const composeEnhancers = composeWithDevTools({}) | ||
export default function configureStore(history: History, initialState: ApplicationState): Store<ApplicationState> { | ||
// create the composing function for our middlewares | ||
const composeEnhancers = composeWithDevTools({}); | ||
// create the redux-saga middleware | ||
const sagaMiddleware = createSagaMiddleware() | ||
const sagaMiddleware = createSagaMiddleware(); | ||
const store = createStore( | ||
createRootReducer(history), | ||
initialState, | ||
composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware)) | ||
) | ||
sagaMiddleware.run(rootSaga) | ||
return store | ||
} | ||
createRootReducer(history), | ||
initialState, | ||
composeEnhancers(applyMiddleware(sagaMiddleware)) | ||
); | ||
sagaMiddleware.run(rootSaga); | ||
return store; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ReactKeycloakProvider } from "@react-keycloak/web"; | ||
import React, { useState } from "react"; | ||
import keycloak from "../keycloak"; | ||
export interface IAuthContext { | ||
name?: string; | ||
preferred_username?: string; | ||
onLogin: () => void; | ||
onLogout: () => void; | ||
} | ||
|
||
export const AuthContext = React.createContext(null); | ||
export const useAuth = () => { | ||
return React.useContext(AuthContext); | ||
}; | ||
|
||
export const AuthProvider = ({ children }) => { | ||
// const { keycloak } = useKeycloak(); | ||
const [name, setName] = useState(); | ||
|
||
const eventLogger = (event: unknown, error: unknown) => { | ||
if (event === "onAuthSuccess") { | ||
setName(keycloak.idTokenParsed.name); | ||
} | ||
}; | ||
|
||
const tokenLogger = (tokens: unknown) => { | ||
console.log("onKeycloakTokens", tokens); | ||
}; | ||
|
||
// const handleLogin = useCallback(() => { | ||
// keycloak?.login(); | ||
// }, [keycloak]); | ||
// const handleLogout = useCallback(() => keycloak?.logout(), [keycloak]); | ||
const handleLogin = () => { | ||
keycloak.login(); | ||
}; | ||
const handleLogout = () => keycloak.logout(); | ||
const value = { | ||
onLogin: handleLogin, | ||
onLogout: handleLogout, | ||
name: name, | ||
}; | ||
|
||
return ( | ||
<ReactKeycloakProvider onEvent={eventLogger} onTokens={tokenLogger} authClient={keycloak}> | ||
<AuthContext.Provider value={value}>{children}</AuthContext.Provider> | ||
</ReactKeycloakProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,12 @@ | ||
import { useKeycloak } from "@react-keycloak/web"; | ||
import * as React from "react"; | ||
import type { RouteProps } from "react-router-dom"; | ||
import { Redirect, Route, RouteComponentProps } from "react-router-dom"; | ||
import { Navigate } from "react-router"; | ||
import { useAuth } from "./auth"; | ||
|
||
interface PrivateRouteParams extends RouteProps { | ||
component: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>; | ||
} | ||
export const PrivateRoute = ({ children }) => { | ||
const { name } = useAuth(); | ||
|
||
export function PrivateRoute({ component: Component, ...rest }: PrivateRouteParams) { | ||
// const { keycloak } = useKeycloak<KeycloakInstance>() | ||
const { keycloak } = useKeycloak(); | ||
console.log("authenticated: " + keycloak?.authenticated); | ||
console.log({ keycloak }); | ||
return ( | ||
<Route | ||
{...rest} | ||
render={(props) => | ||
keycloak?.authenticated ? ( | ||
<Component {...props} /> | ||
) : ( | ||
<Redirect | ||
to={{ | ||
pathname: "/login", | ||
state: { from: props.location }, | ||
}} | ||
/> | ||
) | ||
} | ||
/> | ||
); | ||
} | ||
if (!name) { | ||
return <Navigate to="/" replace />; | ||
} | ||
return children; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.