Skip to content

Commit c78f599

Browse files
authored
Merge pull request #103 from bcomnes/absolute-url
Add APIUrl option to module API
2 parents 36d8789 + e7bdf60 commit c78f599

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ netlifyIdentity.logout();
120120
netlifyIdentity.gotrue;
121121
```
122122

123+
#### `netlifyIdentity.init([opts])`
124+
125+
You can pass an optional `opts` object to configure the widget when using the
126+
module api. Options include:
127+
128+
```js
129+
{
130+
container: "#some-query-selector"; // container to attach to
131+
APIUrl: "https://www.example.com/.netlify/functions/identity"; // Absolute url to endpoint. ONLY USE IN SPECIAL CASES!
132+
}
133+
```
134+
135+
Generally avoid setting the `APIUrl`. You should only set this when your app is
136+
served from a domain that differs from where the identity endpoint is served.
137+
This is common for Cordova or Electron apps where you host from localhost or a
138+
file.
139+
123140
## Localhost
124141

125142
When using the widget on localhost, it will prompt for your Netlify SiteURL the

example/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import App from './App';
55
import registerServiceWorker from './registerServiceWorker';
66
import netlifyIdentity from 'netlify-identity-widget';
77

8+
window.netlifyIdentity = netlifyIdentity
89
// You must run this once before trying to interact with the widget
910
netlifyIdentity.init()
1011

src/components/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class App extends Component {
156156
showHeader={showHeader}
157157
showSignup={showSignup}
158158
devSettings={!store.gotrue}
159-
loading={store.gotrue && !store.settings}
159+
loading={!store.error && store.gotrue && !store.settings}
160160
isOpen={store.modal.isOpen}
161161
onPage={this.handlePage}
162162
onClose={this.handleClose}

src/netlify-identity.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const netlifyIdentity = {
4949
},
5050
init: options => {
5151
init(options);
52-
}
52+
},
53+
store
5354
};
5455

5556
let queuedIframeStyle = null;
@@ -71,9 +72,12 @@ const localHosts = {
7172
"0.0.0.0": true
7273
};
7374

74-
function instantiateGotrue() {
75+
function instantiateGotrue(APIUrl) {
7576
const isLocal = localHosts[document.location.host.split(":").shift()];
7677
const siteURL = isLocal && localStorage.getItem("netlifySiteURL");
78+
if (APIUrl) {
79+
return new GoTrue({ APIUrl, setCookie: !isLocal });
80+
}
7781
if (isLocal && siteURL) {
7882
const parts = [siteURL];
7983
if (!siteURL.match(/\/$/)) {
@@ -175,8 +179,8 @@ function runRoutes() {
175179
}
176180
}
177181

178-
function init(options) {
179-
options = options || {};
182+
function init(options = {}) {
183+
const { APIUrl } = options;
180184
const controlEls = document.querySelectorAll(
181185
"[data-netlify-identity-menu],[data-netlify-identity-button]"
182186
);
@@ -195,8 +199,8 @@ function init(options) {
195199
);
196200
});
197201

198-
store.init(instantiateGotrue());
199-
if (options.hasOwnProperty("logo")) store.modal.logo = options.logo;
202+
store.init(instantiateGotrue(APIUrl));
203+
if (options.logo) store.modal.logo = options.logo;
200204
iframe = document.createElement("iframe");
201205
iframe.id = "netlify-identity-widget";
202206
iframe.onload = () => {

src/state/store.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ store.loadSettings = action(function loadSettings() {
5656
.then(action(settings => (store.settings = settings)))
5757
.catch(
5858
action(err => {
59-
console.error("failed to load settings %o", err);
60-
store.error = err;
59+
store.error = new Error(
60+
`Failed to load settings from ${store.gotrue.api.apiURL}`
61+
);
6162
})
6263
);
6364
});

0 commit comments

Comments
 (0)