fix: 401 error code redirect to login page

pull/3813/head
Arik Chakma 2 years ago
parent 9ca74a5750
commit 11d7618230
  1. 8
      src/components/Setting/ChangePasswordForm.tsx
  2. 91
      src/components/Setting/UpdateProfileForm.tsx

@ -93,6 +93,14 @@ export default function ChangePasswordForm() {
}); });
const json = await res.json(); const json = await res.json();
if (json.status === 401) {
// If the user is not authenticated, redirect to the login page
// Clear the cookie
Cookies.remove(TOKEN_COOKIE_NAME);
window.location.href = '/login';
}
if (res.ok) { if (res.ok) {
setAuthProvider(json.authProvider); setAuthProvider(json.authProvider);
} else { } else {

@ -1,4 +1,4 @@
import { useEffect, useState } from 'preact/hooks'; import { useCallback, useEffect, useState } from 'preact/hooks';
import { TOKEN_COOKIE_NAME } from '../../lib/constants'; import { TOKEN_COOKIE_NAME } from '../../lib/constants';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
@ -55,6 +55,9 @@ export default function UpdateProfileForm() {
setLinkedin(linkedin || ''); setLinkedin(linkedin || '');
setWebsite(website || ''); setWebsite(website || '');
} }
// Check if the user has changed their email
fetchProfile();
setMessage({ setMessage({
type: 'success', type: 'success',
message: 'Profile updated successfully', message: 'Profile updated successfully',
@ -69,50 +72,58 @@ export default function UpdateProfileForm() {
}); });
}; };
// Make a request to the backend to fill in the form with the current values const fetchProfile = useCallback(async () => {
useEffect(() => { // Set the loading state
async function fetchProfile() { setIsLoading(true);
// Set the loading state
setIsLoading(true);
// Create headers with the cookie
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append(
'Cookie',
`${TOKEN_COOKIE_NAME}=${Cookies.get(TOKEN_COOKIE_NAME)}`
);
try {
const res = await fetch('http://localhost:8080/v1-me', {
method: 'POST',
credentials: 'include',
headers,
});
const json = await res.json(); // Create headers with the cookie
if (res.ok) { const headers = new Headers();
setName(json.name); headers.append('Content-Type', 'application/json');
setEmail(json.email); headers.append(
'Cookie',
if (json.links) { `${TOKEN_COOKIE_NAME}=${Cookies.get(TOKEN_COOKIE_NAME)}`
const { github, linkedin, website } = json.links; );
setGithub(github || '');
setLinkedin(linkedin || ''); try {
setWebsite(website || ''); const res = await fetch('http://localhost:8080/v1-me', {
} method: 'POST',
} else { credentials: 'include',
throw new Error(json.message); headers,
});
const json = await res.json();
if (json.status === 401) {
// If the user is not authenticated, redirect to the login page
// Clear the cookie
Cookies.remove(TOKEN_COOKIE_NAME);
window.location.href = '/login';
}
if (res.ok) {
setName(json.name);
setEmail(json.email);
if (json.links) {
const { github, linkedin, website } = json.links;
setGithub(github || '');
setLinkedin(linkedin || '');
setWebsite(website || '');
} }
} catch (error: any) { } else {
setMessage({ throw new Error(json.message);
type: 'error',
message: error?.message || 'Something went wrong',
});
} }
setIsLoading(false); } catch (error: any) {
setMessage({
type: 'error',
message: error?.message || 'Something went wrong',
});
} }
setIsLoading(false);
}, []);
// Make a request to the backend to fill in the form with the current values
useEffect(() => {
fetchProfile(); fetchProfile();
}, []); }, []);

Loading…
Cancel
Save