Skip to content

Commit

Permalink
fix: remove username and logout button when logged out
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhas committed Aug 25, 2022
1 parent c56fb3c commit e047f53
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
23 changes: 14 additions & 9 deletions src/context/sasContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,11 @@ const SASProvider = (props: { children: ReactNode }) => {
}, [])

const login = useCallback(async (username, password) => {
const clientId =
sasjsConfig.serverType === ServerType.Sasjs
? process.env.REACT_APP_CLIENT_ID
: undefined
return sasService
.logIn(username, password, clientId)
.logIn(username, password)
.then(
(res: LoginResult) => {
setUserName(res.userName)
setIsUserLoggedIn(res.isLoggedIn)
return true
},
Expand All @@ -128,6 +125,8 @@ const SASProvider = (props: { children: ReactNode }) => {
const logout = useCallback(() => {
sasService.logOut().then(() => {
setIsUserLoggedIn(false)
setFullName('')
setUserName('')
})
}, [])

Expand All @@ -137,10 +136,16 @@ const SASProvider = (props: { children: ReactNode }) => {

useEffect(() => {
setCheckingSession(true)
sasService.checkSession().then((response) => {
setCheckingSession(false)
setIsUserLoggedIn(response.isLoggedIn)
})
sasService
.checkSession()
.then((response) => {
setCheckingSession(false)
setIsUserLoggedIn(response.isLoggedIn)
})
.catch((_) => {
setCheckingSession(false)
setIsUserLoggedIn(false)
})
}, [])

useEffect(() => {
Expand Down
20 changes: 11 additions & 9 deletions src/layouts/Main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const Main = (props) => {
}}
>
<UserName
userName={sasContext.fullName}
userName={sasContext.fullName || sasContext.userName}
avatarContent={sasContext.avatarContent}
onClickHandler={handleMenu}
className={classes.title}
Expand Down Expand Up @@ -170,14 +170,16 @@ const Main = (props) => {
Documentation
</MenuItem>
<Divider variant="middle" />
<MenuItem
onClick={sasContext.logout}
className={classes.logoutButton}
>
<Button variant="contained" color="primary">
Logout
</Button>
</MenuItem>
{sasContext.isUserLoggedIn && (
<MenuItem
onClick={sasContext.logout}
className={classes.logoutButton}
>
<Button variant="contained" color="primary">
Logout
</Button>
</MenuItem>
)}
</Menu>
</div>
</Toolbar>
Expand Down

0 comments on commit e047f53

Please sign in to comment.