Skip to content

Commit

Permalink
chore: work continues on authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Jan 5, 2024
1 parent 38c23bf commit eeb033e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/components/nav/MobileNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ export function MobileNav() {
))}
</List> */}
{linkRegister}
<Box fontSize='xs' my='1'>
XBuniverse 2023
<Box fontSize='10px' my='1'>
XBuniverse 2023 - 2024
</Box>
</DrawerFooter>
</DrawerContent>
Expand Down
11 changes: 5 additions & 6 deletions src/hooks/querys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ function useUserRegister() {
mutationKey: [keys.userRegister],
mutationFn: (token: string) => postRegister(token),
onSuccess: (data) => {
if (data) return navigate(`/profile/${data.info.user.uid}`);
if (data) {
return navigate(`/profile/${data.info.user.uid}`, { replace: true });
}
},
onError: async (error) => {
console.error('Error en el servidor:', error);
Expand All @@ -166,13 +168,10 @@ function useUserRegister() {
});
}

function useProfile(
id: string | undefined,
// token: string | undefined
) {
function useProfile(id: string | undefined, token: string | null) {
return useSuspenseQuery({
queryKey: [keys.profile, id],
queryFn: () => getUserAndBooks(id),
queryFn: () => getUserAndBooks(id, token),
gcTime: 3000,
});
}
Expand Down
110 changes: 58 additions & 52 deletions src/pages/profile/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { Box, Flex, Image, useColorModeValue } from '@chakra-ui/react';

import { MySimpleGrid } from '../../components/MySimpleGrid';
import { Card } from '../../components/cards/Card';
import { Aside } from '../../components/Aside';
import { MainHead } from '../../components/Head';
import ResultLength from '../../components/ResultLength';
// import { useAuth } from '../../store/AuthContext';
import { useProfile } from '../../hooks/querys';
import { parseDate } from '../../utils/utils';
import { CardType } from '../../components/types';
import ResultLength from '../../components/ResultLength';
// import { logOut } from '../../services/firebase/auth';

export default function Profile() {
// const [userToken, setUserToken] = useState<string | undefined>('');
const { userId } = useParams();
// const { currentUser } = useAuth();
const { data } = useProfile(userId);
const getToken = localStorage.getItem('app_tk');
const { data } = useProfile(userId, getToken);
const bgCover = useColorModeValue('gray.100', 'gray.700');

// useEffect(() => {
// function getToken() {
// try {
// currentUser?.getIdToken().then((token) => {
// setUserToken(token);
// });
// } catch (error) {
// console.error(error);
// }
// }

// getToken();
// }, []);

return (
<>
<MainHead
Expand All @@ -50,19 +37,34 @@ export default function Profile() {
alt={`Imagen de perfil de ${data.user.name}`}
borderRadius='10'
/>
<Box as='h1' fontSize='3xl' mt='3' textAlign='center'>
<Box
as='h1'
fontSize={{ base: 'xl', md: '3xl' }}
mt='3'
textAlign='center'
>
{data.user.name}
</Box>
<Box as='span' fontSize='sm' textAlign='center'>
<Box as='span' fontSize={{ base: 'xs', md: 'sm' }} textAlign='center'>
{data.user.email}
</Box>
<Flex direction='column' fontSize='sm' mt='2' textAlign='center'>
<Box as='span' fontSize='md' fontWeight='bold'>
<Flex
direction='column'
fontSize={{ base: 'xs', md: 'sm' }}
mt='2'
textAlign='center'
>
<Box as='span' fontSize={{ base: 'sm', md: 'md' }} fontWeight='bold'>
Se unió el
</Box>{' '}
{parseDate(data.user.createdAt)}
</Flex>
</Flex>
<Flex justify='center' borderBottom='1px'>
<Box mt={{ base: 5, md: 8 }} mb='1' fontSize={{ base: 'md', md: 'lg' }}>
PUBLICACIONES
</Box>
</Flex>
<Flex
direction={{ base: 'column', md: 'row' }}
maxW='1700px'
Expand All @@ -74,35 +76,39 @@ export default function Profile() {
{/* {aboutCategoriesUI}
{asideFilter} */}
</Aside>
<MySimpleGrid>
{data.books.map(
({
id,
category,
language,
title,
authors,
synopsis,
sourceLink,
pathUrl,
image,
}) => (
<React.Fragment key={id}>
<Card
id={id}
category={category}
language={language}
title={title}
authors={authors}
synopsis={synopsis}
sourceLink={sourceLink}
pathUrl={pathUrl}
image={image}
/>
</React.Fragment>
),
)}
</MySimpleGrid>
{data.books.length > 0 ? (
<MySimpleGrid>
{data.books.map(
({
id,
category,
language,
title,
authors,
synopsis,
sourceLink,
pathUrl,
image,
}: CardType) => (
<React.Fragment key={id}>
<Card
id={id}
category={category}
language={language}
title={title}
authors={authors}
synopsis={synopsis}
sourceLink={sourceLink}
pathUrl={pathUrl}
image={image}
/>
</React.Fragment>
),
)}
</MySimpleGrid>
) : (
<Box>Aún no hay publicaciones</Box>
)}
</Flex>
</>
);
Expand Down
20 changes: 7 additions & 13 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,13 @@ async function postRegister(token: string) {
return data;
}

async function getUserAndBooks(
id: string | undefined,
// token: string | undefined,
) {
const data = await fetchData(
`${API_URL}/my-books/${id}`,
// {
// method: 'GET',
// headers: {
// Authorization: `Bearer ${token}`,
// },
// }
);
async function getUserAndBooks(id: string | undefined, token: string | null) {
const data = await fetchData(`${API_URL}/user/my-books/${id}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});

return data;
}
Expand Down
8 changes: 5 additions & 3 deletions src/services/firebase/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function SignIn() {
try {
const result = await signInWithPopup(logIn, provider);
const token = await result.user.getIdToken();
await localStorage.setItem('app_tk', token);

return mutateAsync(token);
} catch (error) {
Expand All @@ -48,14 +49,14 @@ function SignIn() {
return (
<>
<Button
w='275px'
w='260px'
fontWeight='normal'
leftIcon={<GrGoogle />}
leftIcon={<GrGoogle size='20px' />}
bg={useColorModeValue('#EA4335', '#EE685D')}
color={useColorModeValue('white', 'black')}
borderRadius='lg'
p='7'
fontSize='xl'
fontSize='md'
_hover={{ bg: '#D23C2F' }}
_active={{ bg: '#BB352A' }}
onClick={signInWithGoogle}
Expand All @@ -72,6 +73,7 @@ function SignIn() {
async function logOut() {
try {
await signOut(logIn);
await localStorage.removeItem('app_tk');
} catch (error) {
console.error('Error al cerrar sesión:', error);
}
Expand Down

1 comment on commit eeb033e

@vercel
Copy link

@vercel vercel bot commented on eeb033e Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

xbu – ./

xbu-git-main-franqsanz.vercel.app
xbu-franqsanz.vercel.app
xbu.vercel.app

Please sign in to comment.