Skip to content

Commit

Permalink
web(api): create logout func integrate w/api
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusvrs committed Nov 23, 2023
1 parent 25b49ca commit c0f661b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions web/app/utils/api/logout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { UserContextType, defaultUser } from '@/app/contexts/UserContext';

import request from '../request';
import { settings } from '../settings';

import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime';

import toast from 'react-hot-toast';

interface handleLogoutParamsType {
userContext: UserContextType,
router: AppRouterInstance
};

export default function handleLogout({
userContext,
router
}: handleLogoutParamsType) {
const { user, setUser } = userContext;

if (!user.is_anonymous) {
request.post('/users/logout/', {}, settings).then(response => {
if (response.status == 200) {
setUser(defaultUser);
router.replace('/');
}
}).catch(error => toast.error('Não foi possível sair!'));
}
else router.replace('/');
}

0 comments on commit c0f661b

Please sign in to comment.