From 38ca25518a1f8aab125e43712c38e2fcd9fd7966 Mon Sep 17 00:00:00 2001 From: BrianJiang2021 Date: Wed, 8 Nov 2023 09:57:08 +0800 Subject: [PATCH] fix: login failure verification --- .../src/pages/accountSetting/utils.ts | 1 - apps/storefront/src/pages/login/Login.tsx | 44 +++++++++---------- apps/storefront/src/pages/login/config.ts | 14 ++++++ apps/storefront/src/shared/routes/routes.ts | 5 +++ 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/apps/storefront/src/pages/accountSetting/utils.ts b/apps/storefront/src/pages/accountSetting/utils.ts index 4dd087a6..697ebc8d 100644 --- a/apps/storefront/src/pages/accountSetting/utils.ts +++ b/apps/storefront/src/pages/accountSetting/utils.ts @@ -71,7 +71,6 @@ function sendEmail(data: any, extraFields: any) { (field: Partial) => field.name === item.bcLabel ).value formData.append(`FormField[1][${key}]`, val) - console.log(key, val) } }) } diff --git a/apps/storefront/src/pages/login/Login.tsx b/apps/storefront/src/pages/login/Login.tsx index 08d92e8a..15936de1 100644 --- a/apps/storefront/src/pages/login/Login.tsx +++ b/apps/storefront/src/pages/login/Login.tsx @@ -19,15 +19,8 @@ import { getBCForcePasswordReset, superAdminEndMasquerade, } from '@/shared/service/b2b' -import { - bcLogin, - // bcLogoutLogin, -} from '@/shared/service/bc' -import { - B3SStorage, - clearCurrentCustomerInfo, - getCurrentCustomerInfo, -} from '@/utils' +import { bcLogin } from '@/shared/service/bc' +import { B3SStorage, getCurrentCustomerInfo } from '@/utils' import LoginWidget from './component/LoginWidget' import { @@ -35,6 +28,7 @@ import { loginCheckout, LoginConfig, LoginInfoInit, + logout, } from './config' import LoginForm from './LoginForm' import LoginPanel from './LoginPanel' @@ -87,7 +81,6 @@ export default function Login(props: RegisteredProps) { salesRepCompanyId = 0, isAgenting, registerEnabled, - customerId, }, dispatch, } = useContext(GlobaledContext) @@ -137,31 +130,38 @@ export default function Login(props: RegisteredProps) { const loginFlag = getLoginFlag(search, 'loginFlag') const showTipInfo = getLoginFlag(search, 'showTip') !== 'false' + const closeIsLogout = getLoginFlag(search, 'closeIsLogout') === '1' setShowTipInfo(showTipInfo) if (loginFlag) setLoginFlag(loginFlag) - if (loginFlag === '3') { - // await bcLogoutLogin() + const isLogout = B3SStorage.get('isLogout') === '1' + if (loginFlag === '3' && !isLogout) { + const isLogout = await logout() - if (!customerId) return + if (!isLogout) return if (isAgenting) { await superAdminEndMasquerade(+salesRepCompanyId, +B3UserId) } - dispatch({ - type: 'common', - payload: { - isCloseGotoBCHome: true, - }, - }) // SUP-1282 Clear sessionStorage to allow visitors to display the checkout page window.sessionStorage.clear() - clearCurrentCustomerInfo(dispatch) - await fetch('/login.php?action=logout') + B3SStorage.set('isLogout', '1') + + if (window.location.pathname.includes('login.php')) { + window.location.reload() + } else { + window.location.href = '/login.php/#/login?loginFlag=3' + } + + return + } + + if (closeIsLogout) { + B3SStorage.delete('isLogout') } setLoginInfo(Info) @@ -172,7 +172,7 @@ export default function Login(props: RegisteredProps) { } init() - }, [loginPageButton, loginPageDisplay, loginPageHtml]) + }, [loginPageButton, loginPageDisplay, loginPageHtml, location]) const tipInfo = (loginFlag: string, email = '') => { let str = '' diff --git a/apps/storefront/src/pages/login/config.ts b/apps/storefront/src/pages/login/config.ts index 755533fe..b8073716 100644 --- a/apps/storefront/src/pages/login/config.ts +++ b/apps/storefront/src/pages/login/config.ts @@ -169,3 +169,17 @@ export const getBCChannelId = (storeSitesany: Array) => { return channelId } + +export const logout = () => new Promise((resolve, reject) => { + fetch('/login.php?action=logout').then((response) => { + if (!response.ok) { + throw new Error('Network response was not ok') + } + return response.text() + }).then((responseData) => { + const isFlag = responseData.includes('alertBox--success') + resolve(isFlag) + }).catch(e => { + reject(e) + }) +}) \ No newline at end of file diff --git a/apps/storefront/src/shared/routes/routes.ts b/apps/storefront/src/shared/routes/routes.ts index c6dda315..7ff088ae 100644 --- a/apps/storefront/src/shared/routes/routes.ts +++ b/apps/storefront/src/shared/routes/routes.ts @@ -2,6 +2,7 @@ import { lazy } from 'react' import { matchPath } from 'react-router-dom' import { GlobalState, QuoteConfigProps } from '@/shared/global/context/config' +import { B3SStorage } from '@/utils' const OrderList = lazy(() => import('../../pages/order/MyOrder')) @@ -382,6 +383,10 @@ const gotoAllowedAppPage = ( gotoPage: (url: string) => void, isAccountEnter?: boolean ) => { + if (B3SStorage.get('isLogout') === '1') { + gotoPage('/login?loginFlag=3&&closeIsLogout=1') + return + } const { hash, pathname } = window.location let url = hash.split('#')[1] || '' if (