♻ Move project source files to top level from src, update Sentry dependency (#630)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
38
new-frontend/src/hooks/useAuth.tsx
Normal file
38
new-frontend/src/hooks/useAuth.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useUserStore } from '../store/user-store';
|
||||
import { Body_login_login_access_token as AccessToken, LoginService } from '../client';
|
||||
import { useUsersStore } from '../store/users-store';
|
||||
import { useItemsStore } from '../store/items-store';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
const isLoggedIn = () => {
|
||||
return localStorage.getItem('access_token') !== null;
|
||||
};
|
||||
|
||||
const useAuth = () => {
|
||||
const { getUser, resetUser } = useUserStore();
|
||||
const { resetUsers } = useUsersStore();
|
||||
const { resetItems } = useItemsStore();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const login = async (data: AccessToken) => {
|
||||
const response = await LoginService.loginAccessToken({
|
||||
formData: data,
|
||||
});
|
||||
localStorage.setItem('access_token', response.access_token);
|
||||
await getUser();
|
||||
navigate('/');
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
localStorage.removeItem('access_token');
|
||||
resetUser();
|
||||
resetUsers();
|
||||
resetItems();
|
||||
navigate('/login');
|
||||
};
|
||||
|
||||
return { login, logout };
|
||||
}
|
||||
|
||||
export { isLoggedIn };
|
||||
export default useAuth;
|
20
new-frontend/src/hooks/useCustomToast.tsx
Normal file
20
new-frontend/src/hooks/useCustomToast.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { useToast } from '@chakra-ui/react';
|
||||
|
||||
const useCustomToast = () => {
|
||||
const toast = useToast();
|
||||
|
||||
const showToast = useCallback((title: string, description: string, status: 'success' | 'error') => {
|
||||
toast({
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
isClosable: true,
|
||||
});
|
||||
}, [toast]);
|
||||
|
||||
return showToast;
|
||||
};
|
||||
|
||||
export default useCustomToast;
|
Reference in New Issue
Block a user