added log out

This commit is contained in:
lnn0q 2025-04-18 15:26:18 +03:00
parent b47b0f3752
commit b5280b70a1
2 changed files with 45 additions and 19 deletions

View file

@ -7,7 +7,7 @@ import { url } from '../main';
interface AuthContextProps { interface AuthContextProps {
headers: Session | null; headers: Session | null;
setHeaders: (session: Session) => void; setHeaders: (session: Session | null) => void;
} }
export const AuthContext = createContext<AuthContextProps | null>(null); export const AuthContext = createContext<AuthContextProps | null>(null);
@ -16,6 +16,7 @@ export const AuthProvider = AuthContext.Provider;
interface UseAuthOutput extends AuthContextProps { interface UseAuthOutput extends AuthContextProps {
isAuthenticated: boolean; isAuthenticated: boolean;
login: (value: { email: string; password: string }) => Promise<Session | null | undefined>; login: (value: { email: string; password: string }) => Promise<Session | null | undefined>;
logout: () => Promise<void>;
} }
export function useAuth(): UseAuthOutput { export function useAuth(): UseAuthOutput {
@ -25,6 +26,11 @@ export function useAuth(): UseAuthOutput {
successMsg: 'Logged in successfully.' successMsg: 'Logged in successfully.'
}); });
const { update: logout } = useToastMutation({
mutation: logoutMutation,
successMsg: 'Logged out successfully.'
});
if (!authContext) throw new Error('useAuth must be used within an AuthProvider'); if (!authContext) throw new Error('useAuth must be used within an AuthProvider');
return { return {
@ -45,6 +51,10 @@ export function useAuth(): UseAuthOutput {
return data.login; return data.login;
} }
return data; return data;
},
logout: async () => {
await logout();
authContext.setHeaders(null);
} }
}; };
} }
@ -81,3 +91,9 @@ const loginMutation = gql`
${sessionFragment} ${sessionFragment}
`; `;
const logoutMutation = gql`
mutation AuthLogout {
logout
}
`;

View file

@ -1,6 +1,7 @@
import { Button, Stack } from '@chakra-ui/react'; import { Button, Stack } from '@chakra-ui/react';
import { createRootRoute, Link, Outlet } from '@tanstack/react-router'; import { createRootRoute, Link, Outlet } from '@tanstack/react-router';
import React from 'react'; import React from 'react';
import { useAuth } from '../hooks/auth';
const TanStackRouterDevtools = const TanStackRouterDevtools =
process.env.NODE_ENV === 'production' process.env.NODE_ENV === 'production'
@ -15,25 +16,34 @@ const TanStackRouterDevtools =
); );
export const Route = createRootRoute({ export const Route = createRootRoute({
component: () => ( component: () => {
<Stack padding={3}> const { isAuthenticated, logout } = useAuth();
<Stack flexDirection={'row'} justifyContent={'space-between'}> return (
<Link to="/"> <Stack padding={3}>
<Button>Home</Button> <Stack flexDirection={'row'} justifyContent={'space-between'}>
</Link> <Link to="/">
<Button>Home</Button>
</Link>
<Stack flexDirection={'row'}> <Stack flexDirection={'row'}>
<Link to="/login"> {!isAuthenticated ? (
<Button>Sign in</Button> <>
</Link> <Link to="/login">
<Link to="/register"> <Button>Sign in</Button>
<Button>Sign up</Button> </Link>
</Link> <Link to="/register">
<Button>Sign up</Button>
</Link>
</>
) : (
<Button onClick={async () => logout()}>Log out</Button>
)}
</Stack>
</Stack> </Stack>
<hr style={{ padding: '4px' }} />
<Outlet />
<TanStackRouterDevtools />
</Stack> </Stack>
<hr style={{ padding: '4px' }} /> );
<Outlet /> }
<TanStackRouterDevtools />
</Stack>
)
}); });