alert token when connected using useQueryData

This commit is contained in:
Guillaume Dorce 2022-09-03 15:56:01 +02:00
parent 1b819b7b21
commit fcbb580628
1 changed files with 18 additions and 0 deletions

View File

@ -7,6 +7,8 @@ export default () => {
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const queryClient = useQueryClient();
const { refetch } = useQuery( const { refetch } = useQuery(
['login'], ['login'],
async () => { async () => {
@ -37,12 +39,28 @@ export default () => {
await refetch(); await refetch();
} }
type Token = {
token: string;
userId: string;
expiresAt: string;
};
const useLogin = () => {
const token: Token | undefined = queryClient.getQueryData(['login'], { exact: true });
if (!token) {
alert('You are not logged in');
return;
}
alert(token.token);
}
return ( return (
<> <>
<div className="flex flex-col min-h-full items-center justify-center py-12 px-4 bg-grey-dark sm:px-6 lg:px-8"> <div className="flex flex-col min-h-full items-center justify-center py-12 px-4 bg-grey-dark sm:px-6 lg:px-8">
<div> <div>
<img className="mx-auto h-20 pb-2 w-auto" src={logo} alt="Groupomania" /> <img className="mx-auto h-20 pb-2 w-auto" src={logo} alt="Groupomania" />
</div> </div>
<button className='rounded-lg bg-red text-white' onClick={() => useLogin()}>Test</button>
<div className="w-full max-w-md bg-grey rounded-lg p-5"> <div className="w-full max-w-md bg-grey rounded-lg p-5">
<form className="m-6 mb-3" action="#" method="POST" onSubmit={(e) => onSubmit(e)}> <form className="m-6 mb-3" action="#" method="POST" onSubmit={(e) => onSubmit(e)}>
<div className="-space-y-px rounded-md shadow-sm"> <div className="-space-y-px rounded-md shadow-sm">