Skip to content

Commit

Permalink
Merge pull request #103 from bcomnes/absolute-url
Browse files Browse the repository at this point in the history
Add APIUrl option to module API
  • Loading branch information
bcomnes authored Dec 22, 2017
2 parents 36d8789 + e7bdf60 commit c78f599
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ netlifyIdentity.logout();
netlifyIdentity.gotrue;
```

#### `netlifyIdentity.init([opts])`

You can pass an optional `opts` object to configure the widget when using the
module api. Options include:

```js
{
container: "#some-query-selector"; // container to attach to
APIUrl: "https://www.example.com/.netlify/functions/identity"; // Absolute url to endpoint. ONLY USE IN SPECIAL CASES!
}
```

Generally avoid setting the `APIUrl`. You should only set this when your app is
served from a domain that differs from where the identity endpoint is served.
This is common for Cordova or Electron apps where you host from localhost or a
file.

## Localhost

When using the widget on localhost, it will prompt for your Netlify SiteURL the
Expand Down
1 change: 1 addition & 0 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import App from './App';
import registerServiceWorker from './registerServiceWorker';
import netlifyIdentity from 'netlify-identity-widget';

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class App extends Component {
showHeader={showHeader}
showSignup={showSignup}
devSettings={!store.gotrue}
loading={store.gotrue && !store.settings}
loading={!store.error && store.gotrue && !store.settings}
isOpen={store.modal.isOpen}
onPage={this.handlePage}
onClose={this.handleClose}
Expand Down
16 changes: 10 additions & 6 deletions src/netlify-identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const netlifyIdentity = {
},
init: options => {
init(options);
}
},
store
};

let queuedIframeStyle = null;
Expand All @@ -71,9 +72,12 @@ const localHosts = {
"0.0.0.0": true
};

function instantiateGotrue() {
function instantiateGotrue(APIUrl) {
const isLocal = localHosts[document.location.host.split(":").shift()];
const siteURL = isLocal && localStorage.getItem("netlifySiteURL");
if (APIUrl) {
return new GoTrue({ APIUrl, setCookie: !isLocal });
}
if (isLocal && siteURL) {
const parts = [siteURL];
if (!siteURL.match(/\/$/)) {
Expand Down Expand Up @@ -175,8 +179,8 @@ function runRoutes() {
}
}

function init(options) {
options = options || {};
function init(options = {}) {
const { APIUrl } = options;
const controlEls = document.querySelectorAll(
"[data-netlify-identity-menu],[data-netlify-identity-button]"
);
Expand All @@ -195,8 +199,8 @@ function init(options) {
);
});

store.init(instantiateGotrue());
if (options.hasOwnProperty("logo")) store.modal.logo = options.logo;
store.init(instantiateGotrue(APIUrl));
if (options.logo) store.modal.logo = options.logo;
iframe = document.createElement("iframe");
iframe.id = "netlify-identity-widget";
iframe.onload = () => {
Expand Down
5 changes: 3 additions & 2 deletions src/state/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ store.loadSettings = action(function loadSettings() {
.then(action(settings => (store.settings = settings)))
.catch(
action(err => {
console.error("failed to load settings %o", err);
store.error = err;
store.error = new Error(
`Failed to load settings from ${store.gotrue.api.apiURL}`
);
})
);
});
Expand Down

0 comments on commit c78f599

Please sign in to comment.