generated from deco-sites/storefront
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from deco-sites/feature/public-page-control
created publicpage control
- Loading branch information
Showing
5 changed files
with
99 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <Component {...props} />; | ||
} | ||
|
||
export default Island; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "deco-sites/ecannadeco/islands/PublicPageControl.tsx"; |