From c0f661ba53a3cba69b11d75b8292ba0c65fd0b09 Mon Sep 17 00:00:00 2001 From: mateuvrs Date: Wed, 22 Nov 2023 21:17:16 -0300 Subject: [PATCH] web(api): create logout func integrate w/api --- web/app/utils/api/logout.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 web/app/utils/api/logout.ts diff --git a/web/app/utils/api/logout.ts b/web/app/utils/api/logout.ts new file mode 100644 index 00000000..997abbca --- /dev/null +++ b/web/app/utils/api/logout.ts @@ -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('/'); +} \ No newline at end of file