Skip to content

Commit 2ab5ac7

Browse files
author
sw-yx
committedApr 24, 2019
expose netlifyinstance
1 parent 91e84b9 commit 2ab5ac7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed
 

‎src/app.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ function Gate() {
7171
return isLoggedIn ? <LoggedInScreen /> : <LoggedOutScreen />
7272
}
7373

74-
export function Widget() {
75-
const identity = useNetlifyIdentity("https://identity.netlify.com/") // supply the url of your Netlify site instance. VERY IMPORTANT
74+
export function Widget({ netlifyInstance }: { netlifyInstance: string }) {
75+
const identity = useNetlifyIdentity(netlifyInstance) // supply the url of your Netlify site instance. VERY IMPORTANT
7676
return (
7777
<IdentityContext.Provider value={identity}>
7878
<Gate />

‎src/index.tsx

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@ import VisuallyHidden from "@reach/visually-hidden"
88

99
import { Widget } from "./app"
1010
type ModalProps = {
11+
/** URL of your Netlify Instance with Identity enabled e.g. https://netlify-gotrue-in-react.netlify.com */
12+
netlifyInstance: string
1113
/** pass a boolean to be true or false */
1214
showDialog: boolean
1315
/** modal will call this function to set the state of showDialog to false */
1416
onCloseDialog: () => void
1517
}
16-
export function Modal({ showDialog, onCloseDialog }: ModalProps) {
18+
export function Modal({ showDialog, onCloseDialog, netlifyInstance }: ModalProps) {
19+
if (!netlifyInstance) {
20+
// just a safety check in case a JS user tries to skip this
21+
if (!validateUrl(netlifyInstance))
22+
throw new Error(
23+
"invalid netlify instance URL: " +
24+
netlifyInstance +
25+
". Please check the docs for proper usage or file an issue."
26+
)
27+
}
1728
return (
1829
<DialogOverlay isOpen={showDialog}>
1930
<DialogContent
@@ -28,9 +39,14 @@ export function Modal({ showDialog, onCloseDialog }: ModalProps) {
2839
</button>
2940
<div>
3041
widget
31-
<Widget />
42+
<Widget netlifyInstance={netlifyInstance} />
3243
</div>
3344
</DialogContent>
3445
</DialogOverlay>
3546
)
3647
}
48+
function validateUrl(value) {
49+
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(
50+
value
51+
)
52+
}

0 commit comments

Comments
 (0)