feat: implement revoke access token with RQ
This commit is contained in:
@@ -4,22 +4,30 @@ import PropTypes from 'prop-types';
|
|||||||
import MenuItem from '@mui/material/MenuItem';
|
import MenuItem from '@mui/material/MenuItem';
|
||||||
import Menu from '@mui/material/Menu';
|
import Menu from '@mui/material/Menu';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
import Can from 'components/Can';
|
import Can from 'components/Can';
|
||||||
import apolloClient from 'graphql/client';
|
import apolloClient from 'graphql/client';
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import useAuthentication from 'hooks/useAuthentication';
|
import useAuthentication from 'hooks/useAuthentication';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
import useRevokeAccessToken from 'hooks/useRevokeAccessToken';
|
||||||
function AccountDropdownMenu(props) {
|
function AccountDropdownMenu(props) {
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const authentication = useAuthentication();
|
const authentication = useAuthentication();
|
||||||
|
const token = authentication.token;
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const revokeAccessTokenMutation = useRevokeAccessToken(token);
|
||||||
const { open, onClose, anchorEl, id } = props;
|
const { open, onClose, anchorEl, id } = props;
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
|
revokeAccessTokenMutation.mutate();
|
||||||
|
|
||||||
authentication.removeToken();
|
authentication.removeToken();
|
||||||
await apolloClient.clearStore();
|
await apolloClient.clearStore();
|
||||||
onClose();
|
onClose();
|
||||||
navigate(URLS.LOGIN);
|
navigate(URLS.LOGIN);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Menu
|
<Menu
|
||||||
anchorEl={anchorEl}
|
anchorEl={anchorEl}
|
||||||
|
15
packages/web/src/hooks/useRevokeAccessToken.js
Normal file
15
packages/web/src/hooks/useRevokeAccessToken.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { useMutation } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
|
export default function useRevokeAccessToken(token) {
|
||||||
|
const query = useMutation({
|
||||||
|
mutationFn: async () => {
|
||||||
|
const { data } = await api.delete(`/v1/access-tokens/${token}`);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
Reference in New Issue
Block a user