Skip to content

Commit

Permalink
Update handleSubmit.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bsu3338 authored Jul 28, 2023
1 parent fa72882 commit b2f0324
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion client/src/utils/handleSubmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ import { v4 } from 'uuid';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';

import store from '~/store';
import { useAuthContext } from '~/hooks/AuthContext';

const useMessageHandler = () => {
const currentConversation = useRecoilValue(store.conversation) || {};
const setSubmission = useSetRecoilState(store.submission);
const isSubmitting = useRecoilValue(store.isSubmitting);
const endpointsConfig = useRecoilValue(store.endpointsConfig);

const { token } = useAuthContext();

const checkTokenExpiration = (token) => {
let tokenParts = token.split('.');
let base64Url = tokenParts[1];
let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
let tokenPayloadJson = window.atob(base64);
let tokenPayload = JSON.parse(tokenPayloadJson);
const currentTime = Date.now() / 1000;
const timeLeft = tokenPayload.exp - currentTime;
return timeLeft < 30;
};
if (checkTokenExpiration(token)) {
window.dispatchEvent(new CustomEvent('attemptRefresh'));
}

const { getToken } = store.useToken(currentConversation?.endpoint);

const latestMessage = useRecoilValue(store.latestMessage);
Expand Down

0 comments on commit b2f0324

Please sign in to comment.