@@ -12,6 +12,8 @@ import useDeviceType from '@site/src/hooks/useDeviceType';
1212
1313import { IUserNavbarItemProps } from './item.types' ;
1414import styles from './UserNavbarItem.module.scss' ;
15+ import Cookies from 'js-cookie' ;
16+ import { useHandleLogin } from '@site/src/hooks/useHandleLogin' ;
1517
1618interface IActionProps {
1719 handleClick : ( ) => void ;
@@ -62,12 +64,16 @@ const DashboardActions: React.FC<IActionProps> = ({ handleClick, isDesktop }) =>
6264const SignedInActions : React . FC < IActionProps > = ( { handleClick, isDesktop } ) => {
6365 const signedInButtonClasses = clsx ( 'navbar__item' , styles . UserNavbarItem , styles . SignedInButton ) ;
6466
67+ const { handleLogin } = useHandleLogin ( {
68+ onClickLogin : handleClick ,
69+ } ) ;
70+
6571 return (
6672 < nav className = 'right-navigation' >
6773 < Button
6874 variant = 'secondary'
6975 color = 'black'
70- onClick = { handleClick }
76+ onClick = { handleLogin }
7177 className = { signedInButtonClasses }
7278 data-testid = 'sa_login'
7379 >
@@ -88,14 +94,41 @@ const SignedInActions: React.FC<IActionProps> = ({ handleClick, isDesktop }) =>
8894} ;
8995
9096const UserNavbarDesktopItem = ( { authUrl, is_logged_in } : IUserNavbarItemProps ) => {
91- const { logout } = useLogout ( ) ;
9297 const { deviceType } = useDeviceType ( ) ;
9398 const isDesktop = deviceType === 'desktop' ;
9499
95100 const handleClick = ( ) => {
96101 location . assign ( authUrl ) ;
97102 } ;
98103
104+ const { handleLogin, isOAuth2Enabled } = useHandleLogin ( {
105+ onClickLogin : handleClick ,
106+ } ) ;
107+
108+ const { logout } = useLogout ( ) ;
109+
110+ const loggedState = Cookies . get ( 'logged_state' ) ;
111+
112+ const loginAccountsSessionStorage = JSON . parse ( sessionStorage . getItem ( 'login-accounts' ) ) ;
113+
114+ const isLoginAccountsPopulated =
115+ loginAccountsSessionStorage && loginAccountsSessionStorage . length > 0 ;
116+
117+ React . useEffect ( ( ) => {
118+ if (
119+ loggedState === 'true' &&
120+ isOAuth2Enabled &&
121+ ! isLoginAccountsPopulated &&
122+ ! window . location . pathname . includes ( 'callback' ) &&
123+ ! window . location . pathname . includes ( 'endpoint' )
124+ ) {
125+ handleLogin ( ) ;
126+ }
127+ if ( loggedState === 'false' && isOAuth2Enabled && isLoginAccountsPopulated ) {
128+ logout ( ) ;
129+ }
130+ } , [ isOAuth2Enabled , loggedState , logout , handleLogin , isLoginAccountsPopulated ] ) ;
131+
99132 return is_logged_in ? (
100133 < DashboardActions handleClick = { logout } isDesktop = { isDesktop } />
101134 ) : (
0 commit comments