From 4e947894992408b13c2b78b2211a687da8b70fe5 Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Tue, 19 Dec 2017 17:23:20 -0800 Subject: [PATCH 1/3] Add APIUrl option to module API --- README.md | 1 + src/netlify-identity.js | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5bdaa7701..409a0ceb3 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ const netlifyIdentity = require("netlify-identity-widget"); netlifyIdentity.init({ container: "#netlify-modal" // defaults to document.body, + APIUrl: "https://some-identity-instance.example.com" // Set an absolute URL for the identity endpoint }); netlifyIdentity.open(); // open the modal diff --git a/src/netlify-identity.js b/src/netlify-identity.js index 1f4bb1382..1496946b5 100644 --- a/src/netlify-identity.js +++ b/src/netlify-identity.js @@ -71,9 +71,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(/\/$/)) { @@ -175,8 +178,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]" ); @@ -195,8 +198,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 = () => { From c58aa6cb09d0a5a293dbecdc48c24bef5ca2c058 Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Tue, 19 Dec 2017 18:25:07 -0800 Subject: [PATCH 2/3] Handle error state when settings endpoint fails --- example/src/index.js | 1 + src/components/app.js | 2 +- src/netlify-identity.js | 3 ++- src/state/store.js | 5 +++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/example/src/index.js b/example/src/index.js index 5387b2f43..d17e55ce2 100644 --- a/example/src/index.js +++ b/example/src/index.js @@ -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() diff --git a/src/components/app.js b/src/components/app.js index 9b8c77d87..bd691d805 100644 --- a/src/components/app.js +++ b/src/components/app.js @@ -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} diff --git a/src/netlify-identity.js b/src/netlify-identity.js index 1496946b5..5eb08670f 100644 --- a/src/netlify-identity.js +++ b/src/netlify-identity.js @@ -49,7 +49,8 @@ const netlifyIdentity = { }, init: options => { init(options); - } + }, + store }; let queuedIframeStyle = null; diff --git a/src/state/store.js b/src/state/store.js index c2963cb28..b32e6512e 100644 --- a/src/state/store.js +++ b/src/state/store.js @@ -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}` + ); }) ); }); From e7bdf601d4ba6d51764fe3ba5848a5e91e98a6ea Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Thu, 21 Dec 2017 13:59:15 -0800 Subject: [PATCH 3/3] Clarify why are adding this option. --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 409a0ceb3..789bff553 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,6 @@ const netlifyIdentity = require("netlify-identity-widget"); netlifyIdentity.init({ container: "#netlify-modal" // defaults to document.body, - APIUrl: "https://some-identity-instance.example.com" // Set an absolute URL for the identity endpoint }); netlifyIdentity.open(); // open the modal @@ -121,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