Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
MCSS-78: Add Logout Endpoint (#79)
Browse files Browse the repository at this point in the history
Co-authored-by: Hana Dowe <hana-dowe@users.noreply.github.com>
  • Loading branch information
anthonytedja and hana-dowe authored Dec 27, 2023
1 parent 676360e commit a1f3fa7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ const users = (customFetch: CustomFetch) =>
const res = await customFetch('POST', 'DH_BE', '/user-update', args)
return res.data as {}
},
userLogout: async () => {
const res = await customFetch('POST', 'DH_BE', '/user-logout')
return res.data as {}
},
} as const)

// Mock Data Response for Development
Expand Down
17 changes: 11 additions & 6 deletions components/Dashboard/TileUser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import Typography from '@mui/material/Typography'

import ModalAccount from '@/components/Dashboard/ModalAccount'
import ModalQRCode from '@/components/Dashboard/ModalQRCode'
import { useAPI } from '@/contexts/API'
import { useToast } from '@/contexts/Toast'
import { useUserLogout } from '@/hooks/User/useUserLogout'
import { User, UserStatusDescription } from '@/types/User'

type Props = {
Expand All @@ -26,7 +27,8 @@ type Props = {
const TileUser = (props: Props) => {
const { user } = props

const api = useAPI()
const { setToast } = useToast()
const { isLoading, mutate: userLogout } = useUserLogout()

const [openAccountDetails, setOpenAccountDetails] = useState(!user.first_name || !user.last_name)

Expand Down Expand Up @@ -164,11 +166,14 @@ const TileUser = (props: Props) => {
<Chip
label="Sign Out"
icon={<OutboundTwoToneIcon />}
disabled={isLoading}
clickable
onClick={() => {
document.cookie = 'Authorization=; expires=Tue, 27 Feb 2001 13:00:00 UTC; path=/;'
api.queryClient.invalidateQueries({ queryKey: ['userGet'] })
}}
onClick={() =>
userLogout(null, {
onError: () =>
setToast({ type: 'error', message: 'Something went wrong, try again.' }),
})
}
/>
</Box>
</Grid>
Expand Down
10 changes: 10 additions & 0 deletions hooks/User/useUserLogout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useAPI } from '@/contexts/API'

export const useUserLogout = () => {
const api = useAPI()
return api.useMutation('userLogout', {
onSettled: () => {
api.queryClient.invalidateQueries({ queryKey: ['userGet'] })
},
})
}

0 comments on commit a1f3fa7

Please sign in to comment.