Skip to content

Commit

Permalink
[department-of-veterans-affairs/va-virtual-agent#951] Add ability to …
Browse files Browse the repository at this point in the history
…retrieve JWT token

   Co-authored-by: Maurice Okumu <maurice.okumu@thoughtworks.com>

1. Add ability to retrieve JWT token
2. Add feature flag / flipper for JWT token
3. fixed a depreciation warning: Module @department-of-veterans-affairs/component-library/LoadingIndicator is deprecated. Use <va-loading-indicator> instead"

cross referencing [this PR](department-of-veterans-affairs/vets-api#12736) in vets-api
  • Loading branch information
aaronyoung-tw committed May 22, 2023
1 parent 7930037 commit fc17960
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/applications/virtual-agent/components/chatbox/Chatbox.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useState, useEffect } from 'react';
import LoadingIndicator from '@department-of-veterans-affairs/component-library/LoadingIndicator';
import { useSelector } from 'react-redux';
import { useSelector, connect } from 'react-redux';
import PropTypes from 'prop-types';
import FEATURE_FLAG_NAMES from 'platform/utilities/feature-toggles/featureFlagNames';
import { toggleValues } from 'platform/site-wide/feature-toggles/selectors';
import SignInModal from 'platform/user/authentication/components/SignInModal';
import ChatbotError from '../chatbot-error/ChatbotError';
import useWebChatFramework from './useWebChatFramework';
Expand Down Expand Up @@ -58,10 +60,15 @@ function showBot(
);
}

return <App timeout={props.timeout || minute} />;
return (
<App
timeout={props.timeout || minute}
virtualAgentFetchJwtToken={props.virtualAgentFetchJwtToken}
/>
);
}

export default function Chatbox(props) {
function Chatbox(props) {
const isLoggedIn = useSelector(state => state.user.login.currentlyLoggedIn);
const isAccepted = useSelector(state => state.virtualAgentData.termsAccepted);
const [isAuthTopic, setIsAuthTopic] = useState(false);
Expand Down Expand Up @@ -139,7 +146,7 @@ function App(props) {
case ERROR:
return <ChatbotError />;
case LOADING:
return <LoadingIndicator message="Loading Chatbot" />;
return <va-loading-indicator message="Loading Chatbot" />;
case COMPLETE:
return (
<WebChat
Expand All @@ -152,3 +159,18 @@ function App(props) {
throw new Error(`Invalid loading status: ${loadingStatus}`);
}
}

useVirtualAgentToken.propTypes = {
virtualAgentFetchJwtToken: PropTypes.bool,
};

const fetchVirtualAgentJwtToken = state =>
toggleValues(state)[FEATURE_FLAG_NAMES.virtualAgentFetchJwtToken];

// const virtualAgentFetchJwtToken = () => true;

const mapStateToProps = state => ({
virtualAgentFetchJwtToken: fetchVirtualAgentJwtToken(state),
});

export default connect(mapStateToProps)(Chatbox);
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useEffect } from 'react';
import { apiRequest } from 'platform/utilities/api';
import { useSelector } from 'react-redux';
import * as Sentry from '@sentry/browser';
import axios from 'axios';
import retryOnce from './retryOnce';
import { COMPLETE, ERROR, LOADING } from './loadingStatus';
import {
Expand All @@ -10,6 +11,8 @@ import {
TOKEN_KEY,
} from './utils';

const JWT_TOKEN = 'JWT_TOKEN';

function useWaitForCsrfToken(props) {
// Once the feature toggles have loaded, the csrf token updates
const csrfTokenLoading = useSelector(state => state.featureToggles.loading);
Expand Down Expand Up @@ -37,7 +40,7 @@ export default function useVirtualAgentToken(props) {
const [apiSession, setApiSession] = useState('');
const [csrfTokenLoading, csrfTokenLoadingError] = useWaitForCsrfToken(props);
const [loadingStatus, setLoadingStatus] = useState(LOADING);

const shouldFetchJwtToken = props.virtualAgentFetchJwtToken;
useEffect(
() => {
if (csrfTokenLoadingError) {
Expand All @@ -54,12 +57,28 @@ export default function useVirtualAgentToken(props) {
clearBotSessionStorage();

async function getToken() {
const fetchJwtTokenAndSaveToSessionStorage = async () => {
try {
const JwtResponse = await axios.get(
'https://sqa.eauth.va.gov/MAP/users/v2/session/jwt',
{ withCredentials: true },
);
sessionStorage.setItem(JWT_TOKEN, JwtResponse.data);
} catch (error) {
sessionStorage.setItem(JWT_TOKEN, error.message);
}
};

try {
const response = await retryOnce(callVirtualAgentTokenApi);

sessionStorage.setItem(CONVERSATION_ID_KEY, response.conversationId);
sessionStorage.setItem(TOKEN_KEY, response.token);

if (shouldFetchJwtToken) {
await fetchJwtTokenAndSaveToSessionStorage();
}

setToken(response.token);
setApiSession(response.apiSession);
setLoadingStatus(COMPLETE);
Expand Down
1 change: 1 addition & 0 deletions src/platform/utilities/feature-toggles/featureFlagNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,6 @@ export default Object.freeze({
vaOnlineSchedulingConvertUtcToLocal: 'va_online_scheduling_convert_utc_to_local',
vaViewDependentsAccess: 'va_view_dependents_access',
virtualAgentShowFloatingChatbot: 'virtual_agent_show_floating_chatbot',
virtualAgentFetchJwtToken: 'virtual_agent_fetch_jwt_token',
yellowRibbonEnhancements: 'yellow_ribbon_mvp_enhancement',
});

0 comments on commit fc17960

Please sign in to comment.