From 5c402a73de97ce5b8d30e4cd356d4557eafd0541 Mon Sep 17 00:00:00 2001 From: Guilherme Baufaker Date: Tue, 9 Apr 2024 16:03:31 -0300 Subject: [PATCH] created publicpage control --- components/ui/PublicPageControl.tsx | 43 ++++++++++ fresh.gen.ts | 2 + islands/PublicPageControl.tsx | 8 ++ manifest.gen.ts | 87 ++++++++++---------- sections/Miscellaneous/PublicPageControl.tsx | 1 + 5 files changed, 99 insertions(+), 42 deletions(-) create mode 100644 components/ui/PublicPageControl.tsx create mode 100644 islands/PublicPageControl.tsx create mode 100644 sections/Miscellaneous/PublicPageControl.tsx diff --git a/components/ui/PublicPageControl.tsx b/components/ui/PublicPageControl.tsx new file mode 100644 index 0000000..20104ef --- /dev/null +++ b/components/ui/PublicPageControl.tsx @@ -0,0 +1,43 @@ +/** + * This component was made to control if user is logged in to access pages + */ + +import { useEffect } from "preact/hooks"; + +export interface Props { + redirectTo?: string; +} + +function PublicPageControl(props: Props) { + async function isLogged({ accessToken }: { accessToken: string }) { + try { + const response = await fetch("http://localhost:3000/auth/me", { + method: "GET", + headers: { + "content-type": "application/json", + accept: "application/json", + Authorization: accessToken, + }, + }).then((r) => r.json()); + + const username = response.data.Username; + + if (username) { + window.location.href = props.redirectTo || "/meus-dados"; + } + } catch (error) { + console.error("Erro ao carregar dados:", error); + } + } + + useEffect(() => { + // Pega accessCode no localStorage para verificar se ainda está válida a sessão via api + const accessToken = localStorage.getItem("AccessToken") || ""; + + isLogged({ accessToken }); + }, []); // Passando um array de dependências vazio + + return <>; +} + +export default PublicPageControl; diff --git a/fresh.gen.ts b/fresh.gen.ts index aab1941..f18fe26 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -30,6 +30,7 @@ import * as $Newsletter from "./islands/Newsletter.tsx"; import * as $OutOfStock from "./islands/OutOfStock.tsx"; import * as $PrivatePageControl from "./islands/PrivatePageControl.tsx"; import * as $ProductImageZoom from "./islands/ProductImageZoom.tsx"; +import * as $PublicPageControl from "./islands/PublicPageControl.tsx"; import * as $SearchControls from "./islands/SearchControls.tsx"; import * as $ShippingSimulation from "./islands/ShippingSimulation.tsx"; import * as $ShowMore from "./islands/ShowMore.tsx"; @@ -74,6 +75,7 @@ const manifest = { "./islands/OutOfStock.tsx": $OutOfStock, "./islands/PrivatePageControl.tsx": $PrivatePageControl, "./islands/ProductImageZoom.tsx": $ProductImageZoom, + "./islands/PublicPageControl.tsx": $PublicPageControl, "./islands/SearchControls.tsx": $SearchControls, "./islands/ShippingSimulation.tsx": $ShippingSimulation, "./islands/ShowMore.tsx": $ShowMore, diff --git a/islands/PublicPageControl.tsx b/islands/PublicPageControl.tsx new file mode 100644 index 0000000..0fdb344 --- /dev/null +++ b/islands/PublicPageControl.tsx @@ -0,0 +1,8 @@ +import Component from "deco-sites/ecannadeco/components/ui/PublicPageControl.tsx"; +import type { Props } from "deco-sites/ecannadeco/components/ui/PublicPageControl.tsx"; + +function Island(props: Props) { + return ; +} + +export default Island; diff --git a/manifest.gen.ts b/manifest.gen.ts index aa84c74..a3a2f36 100644 --- a/manifest.gen.ts +++ b/manifest.gen.ts @@ -71,27 +71,28 @@ import * as $$$$$$43 from "./sections/Miscellaneous/MyAccount.tsx"; import * as $$$$$$44 from "./sections/Miscellaneous/MyDocs.tsx"; import * as $$$$$$45 from "./sections/Miscellaneous/MyInfo.tsx"; import * as $$$$$$46 from "./sections/Miscellaneous/PrivatePageControl.tsx"; -import * as $$$$$$47 from "./sections/Miscellaneous/PublicProfile.tsx"; -import * as $$$$$$48 from "./sections/Miscellaneous/SignIn.tsx"; -import * as $$$$$$50 from "./sections/Miscellaneous/Signout.tsx"; -import * as $$$$$$49 from "./sections/Miscellaneous/SignUp.tsx"; -import * as $$$$$$51 from "./sections/Miscellaneous/Slide.tsx"; -import * as $$$$$$52 from "./sections/Miscellaneous/UserAlerts.tsx"; -import * as $$$$$$53 from "./sections/Newsletter/Newsletter.tsx"; -import * as $$$$$$54 from "./sections/Product/ImageGalleryFrontBack.tsx"; -import * as $$$$$$55 from "./sections/Product/ImageGallerySlider.tsx"; -import * as $$$$$$56 from "./sections/Product/NotFound.tsx"; -import * as $$$$$$57 from "./sections/Product/NotFoundChallenge.tsx"; -import * as $$$$$$58 from "./sections/Product/ProductDetails.tsx"; -import * as $$$$$$59 from "./sections/Product/ProductInfo.tsx"; -import * as $$$$$$60 from "./sections/Product/ProductShelf.tsx"; -import * as $$$$$$61 from "./sections/Product/ProductShelfTabbed.tsx"; -import * as $$$$$$62 from "./sections/Product/SearchResult.tsx"; -import * as $$$$$$63 from "./sections/Product/ShelfWithImage.tsx"; -import * as $$$$$$64 from "./sections/Product/Wishlist.tsx"; -import * as $$$$$$65 from "./sections/Social/InstagramPosts.tsx"; -import * as $$$$$$66 from "./sections/Social/WhatsApp.tsx"; -import * as $$$$$$67 from "./sections/Theme/Theme.tsx"; +import * as $$$$$$47 from "./sections/Miscellaneous/PublicPageControl.tsx"; +import * as $$$$$$48 from "./sections/Miscellaneous/PublicProfile.tsx"; +import * as $$$$$$49 from "./sections/Miscellaneous/SignIn.tsx"; +import * as $$$$$$51 from "./sections/Miscellaneous/Signout.tsx"; +import * as $$$$$$50 from "./sections/Miscellaneous/SignUp.tsx"; +import * as $$$$$$52 from "./sections/Miscellaneous/Slide.tsx"; +import * as $$$$$$53 from "./sections/Miscellaneous/UserAlerts.tsx"; +import * as $$$$$$54 from "./sections/Newsletter/Newsletter.tsx"; +import * as $$$$$$55 from "./sections/Product/ImageGalleryFrontBack.tsx"; +import * as $$$$$$56 from "./sections/Product/ImageGallerySlider.tsx"; +import * as $$$$$$57 from "./sections/Product/NotFound.tsx"; +import * as $$$$$$58 from "./sections/Product/NotFoundChallenge.tsx"; +import * as $$$$$$59 from "./sections/Product/ProductDetails.tsx"; +import * as $$$$$$60 from "./sections/Product/ProductInfo.tsx"; +import * as $$$$$$61 from "./sections/Product/ProductShelf.tsx"; +import * as $$$$$$62 from "./sections/Product/ProductShelfTabbed.tsx"; +import * as $$$$$$63 from "./sections/Product/SearchResult.tsx"; +import * as $$$$$$64 from "./sections/Product/ShelfWithImage.tsx"; +import * as $$$$$$65 from "./sections/Product/Wishlist.tsx"; +import * as $$$$$$66 from "./sections/Social/InstagramPosts.tsx"; +import * as $$$$$$67 from "./sections/Social/WhatsApp.tsx"; +import * as $$$$$$68 from "./sections/Theme/Theme.tsx"; const manifest = { "loaders": { @@ -153,28 +154,30 @@ const manifest = { "deco-sites/ecannadeco/sections/Miscellaneous/MyInfo.tsx": $$$$$$45, "deco-sites/ecannadeco/sections/Miscellaneous/PrivatePageControl.tsx": $$$$$$46, - "deco-sites/ecannadeco/sections/Miscellaneous/PublicProfile.tsx": $$$$$$47, - "deco-sites/ecannadeco/sections/Miscellaneous/SignIn.tsx": $$$$$$48, - "deco-sites/ecannadeco/sections/Miscellaneous/Signout.tsx": $$$$$$50, - "deco-sites/ecannadeco/sections/Miscellaneous/SignUp.tsx": $$$$$$49, - "deco-sites/ecannadeco/sections/Miscellaneous/Slide.tsx": $$$$$$51, - "deco-sites/ecannadeco/sections/Miscellaneous/UserAlerts.tsx": $$$$$$52, - "deco-sites/ecannadeco/sections/Newsletter/Newsletter.tsx": $$$$$$53, + "deco-sites/ecannadeco/sections/Miscellaneous/PublicPageControl.tsx": + $$$$$$47, + "deco-sites/ecannadeco/sections/Miscellaneous/PublicProfile.tsx": $$$$$$48, + "deco-sites/ecannadeco/sections/Miscellaneous/SignIn.tsx": $$$$$$49, + "deco-sites/ecannadeco/sections/Miscellaneous/Signout.tsx": $$$$$$51, + "deco-sites/ecannadeco/sections/Miscellaneous/SignUp.tsx": $$$$$$50, + "deco-sites/ecannadeco/sections/Miscellaneous/Slide.tsx": $$$$$$52, + "deco-sites/ecannadeco/sections/Miscellaneous/UserAlerts.tsx": $$$$$$53, + "deco-sites/ecannadeco/sections/Newsletter/Newsletter.tsx": $$$$$$54, "deco-sites/ecannadeco/sections/Product/ImageGalleryFrontBack.tsx": - $$$$$$54, - "deco-sites/ecannadeco/sections/Product/ImageGallerySlider.tsx": $$$$$$55, - "deco-sites/ecannadeco/sections/Product/NotFound.tsx": $$$$$$56, - "deco-sites/ecannadeco/sections/Product/NotFoundChallenge.tsx": $$$$$$57, - "deco-sites/ecannadeco/sections/Product/ProductDetails.tsx": $$$$$$58, - "deco-sites/ecannadeco/sections/Product/ProductInfo.tsx": $$$$$$59, - "deco-sites/ecannadeco/sections/Product/ProductShelf.tsx": $$$$$$60, - "deco-sites/ecannadeco/sections/Product/ProductShelfTabbed.tsx": $$$$$$61, - "deco-sites/ecannadeco/sections/Product/SearchResult.tsx": $$$$$$62, - "deco-sites/ecannadeco/sections/Product/ShelfWithImage.tsx": $$$$$$63, - "deco-sites/ecannadeco/sections/Product/Wishlist.tsx": $$$$$$64, - "deco-sites/ecannadeco/sections/Social/InstagramPosts.tsx": $$$$$$65, - "deco-sites/ecannadeco/sections/Social/WhatsApp.tsx": $$$$$$66, - "deco-sites/ecannadeco/sections/Theme/Theme.tsx": $$$$$$67, + $$$$$$55, + "deco-sites/ecannadeco/sections/Product/ImageGallerySlider.tsx": $$$$$$56, + "deco-sites/ecannadeco/sections/Product/NotFound.tsx": $$$$$$57, + "deco-sites/ecannadeco/sections/Product/NotFoundChallenge.tsx": $$$$$$58, + "deco-sites/ecannadeco/sections/Product/ProductDetails.tsx": $$$$$$59, + "deco-sites/ecannadeco/sections/Product/ProductInfo.tsx": $$$$$$60, + "deco-sites/ecannadeco/sections/Product/ProductShelf.tsx": $$$$$$61, + "deco-sites/ecannadeco/sections/Product/ProductShelfTabbed.tsx": $$$$$$62, + "deco-sites/ecannadeco/sections/Product/SearchResult.tsx": $$$$$$63, + "deco-sites/ecannadeco/sections/Product/ShelfWithImage.tsx": $$$$$$64, + "deco-sites/ecannadeco/sections/Product/Wishlist.tsx": $$$$$$65, + "deco-sites/ecannadeco/sections/Social/InstagramPosts.tsx": $$$$$$66, + "deco-sites/ecannadeco/sections/Social/WhatsApp.tsx": $$$$$$67, + "deco-sites/ecannadeco/sections/Theme/Theme.tsx": $$$$$$68, }, "actions": { "deco-sites/ecannadeco/actions/changePassword.ts": $$$$$$$$$0, diff --git a/sections/Miscellaneous/PublicPageControl.tsx b/sections/Miscellaneous/PublicPageControl.tsx new file mode 100644 index 0000000..3baa1cf --- /dev/null +++ b/sections/Miscellaneous/PublicPageControl.tsx @@ -0,0 +1 @@ +export { default } from "deco-sites/ecannadeco/islands/PublicPageControl.tsx";