-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a162b7
commit 5fe9a50
Showing
5 changed files
with
83 additions
and
0 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
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,57 @@ | ||
import { NextPage } from "next"; | ||
import * as React from "react"; | ||
import { PropsWithChildren, ReactNode } from "react"; | ||
|
||
import { isInIframe, useIsMounted } from "../util"; | ||
import { useDashboardToken } from "./use-dashboard-token"; | ||
|
||
function SimpleError({ children }: PropsWithChildren<{}>) { | ||
return ( | ||
<div style={{ padding: 32, color: "red" }}> | ||
<p>{children}</p> | ||
</div> | ||
); | ||
} | ||
|
||
type Props = { | ||
unmounted?: ReactNode; | ||
notIframe?: ReactNode; | ||
noDashboardToken?: ReactNode; | ||
dashboardTokenInvalid?: ReactNode; | ||
}; | ||
|
||
export const withAuthorization = | ||
({ | ||
dashboardTokenInvalid = <SimpleError>Dashboard token is invalid</SimpleError>, | ||
noDashboardToken = <SimpleError>Dashboard token doesn"t exist</SimpleError>, | ||
notIframe = <SimpleError>The view can only be displayed inside iframe.</SimpleError>, | ||
unmounted = <p>Loading</p>, | ||
}: Props) => | ||
<BaseProps extends React.ComponentProps<NextPage>>( | ||
BaseComponent: React.FunctionComponent<BaseProps> | ||
) => { | ||
function AuthorizedPage(props: BaseProps) { | ||
const mounted = useIsMounted(); | ||
const { isTokenValid, hasAppToken } = useDashboardToken(); | ||
|
||
if (!mounted) { | ||
return unmounted; | ||
} | ||
|
||
if (!isInIframe()) { | ||
return notIframe; | ||
} | ||
|
||
if (!hasAppToken) { | ||
return noDashboardToken; | ||
} | ||
|
||
if (!isTokenValid) { | ||
return dashboardTokenInvalid; | ||
} | ||
|
||
return <BaseComponent {...props} />; | ||
} | ||
|
||
return AuthorizedPage; | ||
}; |
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,2 @@ | ||
export * from "./is-in-iframe"; | ||
export * from "./use-is-mounted"; |
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,11 @@ | ||
export const isInIframe = () => { | ||
if (!document || !window) { | ||
throw new Error("isInIframe should be called only in browser"); | ||
} | ||
|
||
try { | ||
return document.location !== window.parent.location; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; |
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 { useEffect, useState } from "react"; | ||
|
||
export const useIsMounted = (): boolean => { | ||
const [mounted, setMounted] = useState(false); | ||
useEffect(() => setMounted(true), []); | ||
|
||
return mounted; | ||
}; |