diff --git a/app/javascript/contexts/auth/AuthProvider.jsx b/app/javascript/contexts/auth/AuthProvider.jsx index 5c896afc5c..835206c97a 100644 --- a/app/javascript/contexts/auth/AuthProvider.jsx +++ b/app/javascript/contexts/auth/AuthProvider.jsx @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License along // with Greenlight; if not, see . -import React, { useContext, useMemo } from 'react'; +import React, { useContext, useMemo, useState } from 'react'; import PropTypes from 'prop-types'; import useSessions from '../../hooks/queries/users/useSessions'; @@ -30,6 +30,7 @@ export function useAuth() { export default function AuthProvider({ children }) { const { isLoading, data: currentUser } = useSessions(); + const [stateChanging, setStateChanging] = useState(false); const user = { id: currentUser?.id, @@ -44,8 +45,9 @@ export default function AuthProvider({ children }) { verified: currentUser?.verified, status: currentUser?.status, external_account: currentUser?.external_account, - stateChanging: false, + stateChanging, isSuperAdmin: currentUser?.super_admin, + setStateChanging, }; const memoizedCurrentUser = useMemo(() => user, [user]); diff --git a/app/javascript/hooks/mutations/sessions/useCreateSession.jsx b/app/javascript/hooks/mutations/sessions/useCreateSession.jsx index 10fef82ec0..0c0040897e 100644 --- a/app/javascript/hooks/mutations/sessions/useCreateSession.jsx +++ b/app/javascript/hooks/mutations/sessions/useCreateSession.jsx @@ -19,6 +19,7 @@ import { useNavigate, useSearchParams } from 'react-router-dom'; import { toast } from 'react-toastify'; import { useTranslation } from 'react-i18next'; import axios from '../../../helpers/Axios'; +import { useAuth } from '../../../contexts/auth/AuthProvider'; export default function useCreateSession() { const { t } = useTranslation(); @@ -26,11 +27,13 @@ export default function useCreateSession() { const navigate = useNavigate(); const [searchParams] = useSearchParams(); const redirect = searchParams.get('location'); + const { setStateChanging } = useAuth(); return useMutation( ({ session, token }) => axios.post('/sessions.json', { session, token }).then((resp) => resp.data.data), { onSuccess: async (response) => { + setStateChanging(true); await queryClient.refetchQueries('useSessions'); // if the current user does NOT have the CreateRoom permission, then do not re-direct to rooms page @@ -41,6 +44,7 @@ export default function useCreateSession() { } else { navigate('/rooms'); } + setStateChanging(true); }, onError: (err) => { if (err.response.data.errors === 'PendingUser') { diff --git a/app/javascript/hooks/mutations/sessions/useDeleteSession.jsx b/app/javascript/hooks/mutations/sessions/useDeleteSession.jsx index f16b406b55..e7baa4a659 100644 --- a/app/javascript/hooks/mutations/sessions/useDeleteSession.jsx +++ b/app/javascript/hooks/mutations/sessions/useDeleteSession.jsx @@ -25,17 +25,17 @@ export default function useDeleteSession({ showToast = true }) { const { t } = useTranslation(); const queryClient = useQueryClient(); const navigate = useNavigate(); - const currentUser = useAuth(); + const { setStateChanging } = useAuth(); return useMutation( () => axios.delete('/sessions/signout.json'), { onSuccess: async () => { - currentUser.stateChanging = true; + setStateChanging(true); queryClient.refetchQueries('useSessions'); await navigate('/'); if (showToast) { toast.success(t('toast.success.session.signed_out')); } - currentUser.stateChanging = false; + setStateChanging(false); }, onError: () => { toast.error(t('toast.error.problem_completing_action')); diff --git a/app/javascript/routes/UnauthenticatedOnly.jsx b/app/javascript/routes/UnauthenticatedOnly.jsx index d47c39e110..2ff10bfc7b 100644 --- a/app/javascript/routes/UnauthenticatedOnly.jsx +++ b/app/javascript/routes/UnauthenticatedOnly.jsx @@ -21,7 +21,7 @@ import { useAuth } from '../contexts/auth/AuthProvider'; export default function UnauthenticatedOnly() { const currentUser = useAuth(); - if (currentUser.signed_in) { + if (currentUser.signed_in && !currentUser.stateChanging) { return ; }