Skip to content

Commit

Permalink
feat: adapt web to handle auth0 authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed May 16, 2023
1 parent 111aa83 commit 9c6e9e0
Show file tree
Hide file tree
Showing 9 changed files with 30,825 additions and 295 deletions.
31,043 changes: 30,790 additions & 253 deletions web/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"start:local-api": "env-cmd --file ./.env.integration npm run develop"
},
"dependencies": {
"@auth0/auth0-react": "^2.1.0",
"@emotion/core": "^10.0.28",
"@emotion/styled": "^10.0.27",
"@loadable/component": "^5.10.2",
Expand Down
15 changes: 6 additions & 9 deletions web/src/actions/userSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import {
LOGIN_FAILED,
SET_USER_SESSION,
SET_USER_SESSION_FAILED,
SET_KEYCLOAK_SESSION,
LOGOUT,
DELETE_ACCOUNT_FAILED,
DELETE_ACCOUNT_SUCCESS,
VALIDATE_COUPON_SUCCESS,
VALIDATE_COUPON_FAILED,
SET_AUTH_SESSION,
SET_AUTH0_SESSION,
} from "../constants/actionTypes";
import {USER_AUTH_SESSION_TOKEN, USER_SESSION_TOKEN_NAME} from "../constants/userSession";
import {USER_AUTH_SESSION_TOKEN} from "../constants/userSession";
import {
getUserSession,
deleteUserAccount,
postCouponValidation,
fetchAccessToken,
} from "../api/userSession";
import {
fetchUserOrganizationsInvitations,
Expand Down Expand Up @@ -100,17 +98,16 @@ export const fetchUserSession = postPreferences => async dispatch => {
};

//TODO: receive authenticated parameter from the provider
export const setAuthSession = () => async dispatch => {
export const setAuthSession = token => async dispatch => {
try {
const response = await fetchAccessToken();
dispatch({
type: SET_AUTH_SESSION,
type: SET_AUTH0_SESSION,
payload: {
token: response.data.token,
token: token,
authenticated: true,
}
})
Cookies.set(USER_AUTH_SESSION_TOKEN, response.data.token);
Cookies.set(USER_AUTH_SESSION_TOKEN, token);
dispatch(fetchUserSession(true));
} catch (error) {
dispatch({ type: LOGIN_FAILED, payload: { error } });
Expand Down
6 changes: 1 addition & 5 deletions web/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import Cookies from "js-cookie";
import {USER_AUTH_SESSION_TOKEN, USER_SESSION_TOKEN_NAME} from "../constants/userSession";
import {USER_AUTH_SESSION_TOKEN} from "../constants/userSession";

const withToken = function(config) {
const token = Cookies.get(USER_AUTH_SESSION_TOKEN);
Expand All @@ -14,9 +14,5 @@ export const baseApi = axios.create({
baseURL: process.env.GATSBY_API_URL,
});

export const baseAuthApi = axios.create({
baseURL: process.env.AUTH_SERVICE_URL,
});

// Authenticated routes
baseApi.interceptors.request.use(withToken);
7 changes: 1 addition & 6 deletions web/src/api/userSession.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-unused-vars */
import {baseApi, baseAuthApi} from "./index";
import {baseApi} from "./index";

export function getUserSession() {
return baseApi.get("/user/session");
Expand All @@ -14,8 +14,3 @@ export function deleteUserAccount(reason) {
export function postCouponValidation(hash, teamID) {
return baseApi.post(`/coupon-validation`, { hash: hash, team_id: teamID });
}

// authentication calls
export function fetchAccessToken() {
return baseAuthApi.get("/token");
}
41 changes: 23 additions & 18 deletions web/src/components/ProtectedRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Dimmer } from "tabler-react";
import { useAuth0 } from "@auth0/auth0-react";
import {setAuthSession} from "../actions/userSession";


//TODO: update redux state with isAuthenticated value
Expand All @@ -10,28 +11,32 @@ import { useAuth0 } from "@auth0/auth0-react";
const ProtectedRoute = ({ component: Component, ...rest }) => {
const dispatch = useDispatch();
const userSession = useSelector(state => state.userSession);
const { isLoading, isAuthenticated, loginWithRedirect, getAccessTokenSilently } = useAuth0()
const {
isLoading,
isAuthenticated,
loginWithRedirect,
getAccessTokenSilently,
} = useAuth0()

if (!isLoading && !isAuthenticated) {
loginWithRedirect();
} else if (!isLoading && isAuthenticated) {
getAccessTokenSilently().then((token) => {
console.log(token.__raw);
});
loginWithRedirect()
.then(() => {getAccessTokenSilently()
.then((token) => {
console.log(token);
dispatch(setAuthSession(token))
})
})
} else if (!isLoading && isAuthenticated && !userSession.accessToken) {
getAccessTokenSilently().then((token) => {
console.log(token);
dispatch(setAuthSession(token))
})
}

if (userSession.isAuthenticated && userSession.accessToken) {
return <Component {...rest} />;
}
// useEffect(() => {
// const { access_token } = userSession;
// if (!access_token) {
// dispatch(setAuthSession());
// }
// }, []);

// if (userSession.isAuthenticated) {
// return (
// <Component {...rest} />
// );
// }
console.log(isLoading, isAuthenticated)
return <Dimmer active loader />;
};

Expand Down
2 changes: 1 addition & 1 deletion web/src/constants/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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_AUTH_SESSION = "SET_AUTH_SESSION";
export const SET_AUTH0_SESSION = "SET_AUTH0_SESSION";
export const LOGOUT = "LOGOUT";
export const DELETE_ACCOUNT_SUCCESS = "DELETE_ACCOUNT_SUCCESS";
export const DELETE_ACCOUNT_FAILED = "DELETE_ACCOUNT_FAILED";
Expand Down
1 change: 0 additions & 1 deletion web/src/constants/userSession.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const USER_SESSION_TOKEN_NAME = "KEYCLOAK_IDENTITY";
export const USER_AUTH_SESSION_TOKEN = "AUTH_TOKEN";
4 changes: 2 additions & 2 deletions web/src/state/userSessionReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
VALIDATE_COUPON_SUCCESS,
VALIDATE_CHALLENGE_SUCCESS,
BUY_CHALLENGE_SUCCESS,
SET_AUTH_SESSION,
SET_AUTH0_SESSION,
} from "../constants/actionTypes";

const initialState = {
Expand Down Expand Up @@ -45,7 +45,7 @@ export default function userSessionReducer(
accessToken: undefined,
};

case SET_AUTH_SESSION:
case SET_AUTH0_SESSION:
return {
...state,
accessToken: action.payload.token,
Expand Down

0 comments on commit 9c6e9e0

Please sign in to comment.