Skip to content

Commit

Permalink
add logout mutation to front end (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgraham4401 authored Jul 25, 2024
1 parent 426c503 commit 103d45e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion client/src/components/Layout/TopNav/TopNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import React, { useContext } from 'react';
import { Button, Dropdown } from 'react-bootstrap';
import { useDispatch, useSelector } from 'react-redux';
import { Link, useNavigate } from 'react-router-dom';
import { removeCredentials, selectAuthenticated } from 'store';
import { removeCredentials, selectAuthenticated, useLogoutMutation } from 'store';

export function TopNav() {
const { showSidebar, setShowSidebar } = useContext<NavContextProps>(NavContext);
const isAuthenticated = useSelector(selectAuthenticated);
const dispatch = useDispatch();
const navigation = useNavigate();
const [logout] = useLogoutMutation();

if (!isAuthenticated) return null;

const handleLogout = () => {
dispatch(removeCredentials());
navigation('/login');
logout(undefined);
};

const toggleSidebar = () => {
Expand Down
1 change: 1 addition & 0 deletions client/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const {

export const {
useLoginMutation,
useLogoutMutation,
useGetUserQuery,
useGetProfileQuery,
useGetRcrainfoProfileQuery,
Expand Down
7 changes: 7 additions & 0 deletions client/src/store/userSlice/user.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export const userApi = haztrakApi.injectEndpoints({
}),
invalidatesTags: ['auth'],
}),
logout: build.mutation({
query: () => ({
url: 'user/logout',
method: 'POST',
}),
invalidatesTags: ['auth'],
}),
getUser: build.query<HaztrakUser, void>({
query: () => ({
url: 'user',
Expand Down

0 comments on commit 103d45e

Please sign in to comment.