Skip to content

Commit

Permalink
Fix issue with login after redirect not working
Browse files Browse the repository at this point in the history
  • Loading branch information
farhatahmad committed Jun 3, 2024
1 parent c248676 commit e4d4d9d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
6 changes: 4 additions & 2 deletions app/javascript/contexts/auth/AuthProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License along
// with Greenlight; if not, see <http://www.gnu.org/licenses/>.

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';

Expand All @@ -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,
Expand All @@ -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]);
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/hooks/mutations/sessions/useCreateSession.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ 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();
const queryClient = useQueryClient();
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

Expand All @@ -41,6 +44,7 @@ export default function useCreateSession() {
} else {
navigate('/rooms');
}
setStateChanging(true);
},
onError: (err) => {
if (err.response.data.errors === 'PendingUser') {
Expand Down
6 changes: 3 additions & 3 deletions app/javascript/hooks/mutations/sessions/useDeleteSession.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/routes/UnauthenticatedOnly.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Navigate to="/" replace />;
}

Expand Down

0 comments on commit e4d4d9d

Please sign in to comment.