fix: redirect unauthenticated client portal users
This commit is contained in:
parent
266ae01a7e
commit
adcf2fc9b8
@ -10,6 +10,7 @@ import {
|
||||
} from '@mdi/js';
|
||||
import axios from 'axios';
|
||||
import Head from 'next/head';
|
||||
import { useRouter } from 'next/router';
|
||||
import React from 'react';
|
||||
import type { ReactElement } from 'react';
|
||||
import BaseIcon from '../components/BaseIcon';
|
||||
@ -69,6 +70,7 @@ function formatDate(value?: string) {
|
||||
}
|
||||
|
||||
const ClientPortal = () => {
|
||||
const router = useRouter();
|
||||
const { currentUser } = useAppSelector((state) => state.auth);
|
||||
const [clients, setClients] = React.useState<PortalClient[]>([]);
|
||||
const [clientId, setClientId] = React.useState('');
|
||||
@ -81,24 +83,46 @@ const ClientPortal = () => {
|
||||
|
||||
const isClientUser = currentUser?.app_role?.name === 'Client';
|
||||
|
||||
function redirectToLogin() {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
router.push('/login/');
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
async function loadClients() {
|
||||
if (isClientUser) {
|
||||
const response = await axios.get('/coaching/client-portal/me');
|
||||
setPortalClient(response.data);
|
||||
setClientId(response.data.id);
|
||||
const latestReflection =
|
||||
response.data.prep_briefs?.[0]?.client_reflection || '';
|
||||
setReflection(latestReflection);
|
||||
setReflectionSaved(Boolean(latestReflection));
|
||||
const token = localStorage.getItem('token');
|
||||
|
||||
if (!token) {
|
||||
redirectToLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await axios.get('/coaching/clients');
|
||||
setClients(response.data);
|
||||
try {
|
||||
if (isClientUser) {
|
||||
const response = await axios.get('/coaching/client-portal/me');
|
||||
setPortalClient(response.data);
|
||||
setClientId(response.data.id);
|
||||
const latestReflection =
|
||||
response.data.prep_briefs?.[0]?.client_reflection || '';
|
||||
setReflection(latestReflection);
|
||||
setReflectionSaved(Boolean(latestReflection));
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.data.length > 0) {
|
||||
setClientId(response.data[0].id);
|
||||
const response = await axios.get('/coaching/clients');
|
||||
setClients(response.data);
|
||||
|
||||
if (response.data.length > 0) {
|
||||
setClientId(response.data[0].id);
|
||||
}
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 401) {
|
||||
redirectToLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@ -111,12 +135,21 @@ const ClientPortal = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await axios.get(`/coaching/client-portal/${clientId}`);
|
||||
setPortalClient(response.data);
|
||||
const latestReflection =
|
||||
response.data.prep_briefs?.[0]?.client_reflection || '';
|
||||
setReflection(latestReflection);
|
||||
setReflectionSaved(Boolean(latestReflection));
|
||||
try {
|
||||
const response = await axios.get(`/coaching/client-portal/${clientId}`);
|
||||
setPortalClient(response.data);
|
||||
const latestReflection =
|
||||
response.data.prep_briefs?.[0]?.client_reflection || '';
|
||||
setReflection(latestReflection);
|
||||
setReflectionSaved(Boolean(latestReflection));
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 401) {
|
||||
redirectToLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isClientUser) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user