From 28b3e426d18554e4d5de2c8260aa93949f254b76 Mon Sep 17 00:00:00 2001 From: Hawk Date: Sun, 17 May 2020 00:47:07 +0200 Subject: [PATCH] added automatic logout --- .../src/components/profile/changePassword.js | 18 ++++++++++++++++++ .../src/components/profile/changeUsername.js | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/frontend/src/components/profile/changePassword.js b/frontend/src/components/profile/changePassword.js index 13dd678..dd42ca5 100644 --- a/frontend/src/components/profile/changePassword.js +++ b/frontend/src/components/profile/changePassword.js @@ -16,6 +16,21 @@ export default function ChangePassword(props) { const [statusText, setStatusText] = React.useState(null); const [status, setStatus] = React.useState(null); + const logout = async () => { + await fetch(`/simcompanies/API/user/logout`, { + method: 'DELETE', + mode: 'cors', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + referrerPolicy: 'no-referrer' + }).then( + () => { window.location.reload() } + ) + } + const postSetPassword = async (url, data) => { const response = await fetch(url, { @@ -33,6 +48,9 @@ export default function ChangePassword(props) { const text = await response.text() await setStatusText(text) setStatus(response.status) + if (response.status === 200) { + setTimeout(() => { logout() }, 2000) + } setDisabled(false) setTimeout(() => { setStatus(null) }, 2000) } diff --git a/frontend/src/components/profile/changeUsername.js b/frontend/src/components/profile/changeUsername.js index cdfe03f..83244c3 100644 --- a/frontend/src/components/profile/changeUsername.js +++ b/frontend/src/components/profile/changeUsername.js @@ -14,6 +14,21 @@ export default function ChangeUsername(props) { const [statusText, setStatusText] = React.useState(null); const [status, setStatus] = React.useState(null); + const logout = async () => { + await fetch(`/simcompanies/API/user/logout`, { + method: 'DELETE', + mode: 'cors', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + referrerPolicy: 'no-referrer' + }).then( + () => { window.location.reload() } + ) + } + const postSetName = async (url, data) => { const response = await fetch(url, { @@ -31,6 +46,9 @@ export default function ChangeUsername(props) { const text = await response.text() await setStatusText(text) setStatus(response.status) + if (response.status === 200) { + setTimeout(() => { logout() }, 2000) + } setDisabled(false) setTimeout(() => { setStatus(null) }, 2000) }