Skip to content

Commit

Permalink
feat(web): delete keycloack statements
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed Apr 25, 2023
1 parent c4ad6b3 commit ade237e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 82 deletions.
25 changes: 5 additions & 20 deletions web/src/actions/userSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,18 @@ export const fetchUserSession = postPreferences => async dispatch => {
}
};

//TODO: receive authenticated parameter from the provider
export const setAuthSession = () => async dispatch => {
try {
const response = await fetchAccessToken();
dispatch({
type: SET_AUTH_SESSION,
payload: {token: response.data.token}
payload: {
token: response.data.token,
authenticated: true,
}
})
Cookies.set(USER_AUTH_SESSION_TOKEN, response.data.token);
} catch (error) {
dispatch({ type: LOGIN_FAILED, payload: { error } });
}
}

export const setKeycloakSession = (
keycloakInstance,
authenticated
) => async dispatch => {
try {
dispatch({
type: SET_KEYCLOAK_SESSION,
payload: {
keycloakInstance: keycloakInstance,
authenticated: authenticated,
},
});

Cookies.set(USER_SESSION_TOKEN_NAME, keycloakInstance.token);
dispatch(fetchUserSession(true));
} catch (error) {
dispatch({ type: LOGIN_FAILED, payload: { error } });
Expand Down
4 changes: 2 additions & 2 deletions web/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import axios from "axios";
import Cookies from "js-cookie";
import { USER_SESSION_TOKEN_NAME } from "../constants/userSession";
import {USER_AUTH_SESSION_TOKEN, USER_SESSION_TOKEN_NAME} from "../constants/userSession";

const withToken = function(config) {
const token = Cookies.get(USER_SESSION_TOKEN_NAME);
const token = Cookies.get(USER_AUTH_SESSION_TOKEN);
if (token) {
config.headers.Authorization = token;
}
Expand Down
50 changes: 3 additions & 47 deletions web/src/components/ProtectedRoute.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,22 @@
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import Keycloak from "keycloak-js";
import { Dimmer } from "tabler-react";
import { toast } from "react-toastify";
import {setAuthSession, setKeycloakSession} from "../actions/userSession";
import {setAuthSession} from "../actions/userSession";

const ProtectedRoute = ({ component: Component, ...rest }) => {
const dispatch = useDispatch();
const userSession = useSelector(state => state.userSession);

// We want to get token & refreshToken if they exist
// If they don't exist, we want to "provider" login page
// if they exist, we want to check if they are still valid
// if they are not valid, we want to refresh them
// if they are valid, we want to set them in the redux store
// we also want to set authenticated to true
useEffect(() => {
// retrieve token
const { activeKeycloakSession, access_token } = userSession;
const keycloak = new Keycloak("/keycloak.json");
const token = activeKeycloakSession && activeKeycloakSession.token;
const { access_token } = userSession;
if (!access_token) {
dispatch(setAuthSession());
}
const refreshToken =
activeKeycloakSession && activeKeycloakSession.refreshToken;

// if tokens don't exist, we want to redirect to "provider" login page
// verify if token is expired
// set token in cookie
keycloak
.init({
onLoad: "login-required",
checkLoginIframe: false,
enableLogging: true,
token,
refreshToken,
})
.then(authenticated => {
dispatch(setKeycloakSession(keycloak, authenticated));
});

keycloak.onTokenExpired = () => {
keycloak
.updateToken(30)
.success(authenticated => {
dispatch(setKeycloakSession(keycloak, authenticated));
})
.error(() =>
toast.error(`SESSION EXPIRED! Please refresh the page.`, {
autoClose: false,
hideProgressBar: true,
})
);
};
}, []);

if (userSession.activeKeycloakSession) {
if (userSession.isAuthenticated) {
return <Component {...rest} />;
} else return <h3>Auth error, please try again!</h3>;
}
}

return <Dimmer active loader />;
};
Expand Down
1 change: 0 additions & 1 deletion web/src/constants/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
export const LOGIN_FAILED = "LOGIN_FAILED";
export const SET_USER_SESSION = "SET_USER_SESSION";
export const SET_USER_SESSION_FAILED = "SET_USER_SESSION_FAILED";
export const SET_KEYCLOAK_SESSION = "SET_KEYCLOAK_SESSION";
export const SET_AUTH_SESSION = "SET_AUTH_SESSION";
export const LOGOUT = "LOGOUT";
export const DELETE_ACCOUNT_SUCCESS = "DELETE_ACCOUNT_SUCCESS";
Expand Down
13 changes: 1 addition & 12 deletions web/src/state/userSessionReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
LOGOUT,
LOGIN_FAILED,
SET_USER_SESSION,
SET_KEYCLOAK_SESSION,
VALIDATE_COUPON_SUCCESS,
VALIDATE_CHALLENGE_SUCCESS,
BUY_CHALLENGE_SUCCESS,
Expand All @@ -15,7 +14,6 @@ const initialState = {
fetching: false,
isAuthenticated: false,
activeUserSession: undefined,
activeKeycloakSession: undefined,
cash: undefined,
accessToken: undefined,
},
Expand All @@ -32,7 +30,6 @@ export default function userSessionReducer(
return {
...state,
fetching: false,
activeKeycloakSession: undefined,
isAuthenticated: false,
error: action.payload.error,
accessToken: undefined,
Expand All @@ -42,25 +39,17 @@ export default function userSessionReducer(
return {
...state,
fetching: false,
activeKeycloakSession: undefined,
activeUserSession: undefined,
isAuthenticated: false,
error: undefined,
accessToken: undefined,
};

case SET_KEYCLOAK_SESSION:
return {
...state,
fetching: false,
activeKeycloakSession: action.payload.keycloakInstance,
isAuthenticated: action.payload.authenticated,
};

case SET_AUTH_SESSION:
return {
...state,
accessToken: action.payload.token,
isAuthenticated: action.payload.authenticated,
}

case SET_USER_SESSION:
Expand Down

0 comments on commit ade237e

Please sign in to comment.