-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathnetlify-identity.js
227 lines (205 loc) · 5.15 KB
/
netlify-identity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { h, render } from "preact";
import { observe } from "mobx";
import { Provider } from "mobx-preact";
import GoTrue from "gotrue-js";
import App from "./components/app";
import store from "./state/store";
import Controls from "./components/controls";
import modalCSS from "./components/modal.css";
const callbacks = {};
function trigger(callback) {
(callbacks[callback] || []).forEach(cb => {
cb.apply(cb, Array.prototype.slice.call(arguments, 1));
});
}
const validActions = {
login: true,
error: true
};
const netlifyIdentity = {
on: (event, cb) => {
callbacks[event] = callbacks[event] || [];
callbacks[event].push(cb);
},
open: action => {
action = action || "login";
if (!validActions[action]) {
throw new Error(`Invalid action for open: ${action}`);
}
store.openModal(store.user ? "user" : action);
},
close: () => {
store.closeModal();
},
currentUser: () => {
return store.gotrue && store.gotrue.currentUser();
},
logout: () => {
return store.logout();
},
get gotrue() {
if (!store.gotrue) {
store.openModal("login");
}
return store.gotrue;
},
init: options => {
init(options);
}
};
let queuedIframeStyle = null;
function setStyle(el, css) {
let style = "";
for (const key in css) {
style += `${key}: ${css[key]}; `;
}
if (el) {
el.setAttribute("style", style);
} else {
queuedIframeStyle = style;
}
}
const localHosts = {
localhost: true,
"127.0.0.1": true,
"0.0.0.0": true
};
function instantiateGotrue() {
const isLocal = localHosts[document.location.host.split(":").shift()];
const siteURL = isLocal && localStorage.getItem("netlifySiteURL");
if (isLocal && siteURL) {
const parts = [siteURL];
if (!siteURL.match(/\/$/)) {
parts.push("/");
}
parts.push(".netlify/identity");
return new GoTrue({ APIUrl: parts.join("") });
}
if (isLocal) {
return null;
}
return new GoTrue();
}
let root;
let iframe;
const iframeStyle = {
position: "fixed",
top: 0,
left: 0,
border: "none",
width: "100%",
height: "100%",
overflow: "visible",
background: "transparent",
display: "none"
};
observe(store.modal, "isOpen", () => {
if (!store.settings) {
store.loadSettings();
}
setStyle(iframe, {
...iframeStyle,
display: store.modal.isOpen ? "block" : "none"
});
if (store.modal.isOpen) {
trigger("open", store.modal.page);
} else {
trigger("close");
}
});
observe(store, "siteURL", () => {
localStorage.setItem("netlifySiteURL", store.siteURL);
store.init(instantiateGotrue(), true);
});
observe(store, "user", () => {
if (store.user) {
trigger("login", store.user);
} else {
trigger("logout");
}
});
observe(store, "gotrue", () => {
store.gotrue && trigger("init", store.gotrue.currentUser());
});
observe(store, "error", () => {
trigger("error", store.error);
});
const routes = /(confirmation|invite|recovery|email_change)_token=([^&]+)/;
const errorRoute = /error=access_denied&error_description=403/;
const accessTokenRoute = /access_token=/;
function runRoutes() {
const hash = (document.location.hash || "").replace(/^#/, "");
if (!hash) {
return;
}
const m = hash.match(routes);
if (m) {
store.verifyToken(m[1], m[2]);
document.location.hash = "";
}
const em = hash.match(errorRoute);
if (em) {
store.openModal("signup");
document.location.hash = "";
}
const am = hash.match(accessTokenRoute);
if (am) {
const params = {};
hash.split("&").forEach(pair => {
const [key, value] = pair.split("=");
params[key] = value;
});
document.location.hash = "";
store.openModal("login");
store.completeExternalLogin(params);
}
}
function init(options) {
options = options || {};
const controlEls = document.querySelectorAll(
"[data-netlify-identity-menu],[data-netlify-identity-button]"
);
Array.prototype.slice.call(controlEls).forEach(el => {
let controls = null;
const mode =
el.getAttribute("data-netlify-identity-menu") === null
? "button"
: "menu";
render(
<Provider store={store}>
<Controls mode={mode} text={el.innerText.trim()} />
</Provider>,
el,
controls
);
});
store.init(instantiateGotrue());
iframe = document.createElement("iframe");
iframe.id = "netlify-identity-widget";
iframe.onload = () => {
const styles = iframe.contentDocument.createElement("style");
styles.innerHTML = modalCSS.toString();
iframe.contentDocument.head.appendChild(styles);
root = render(
<Provider store={store}>
<App />
</Provider>,
iframe.contentDocument.body,
root
);
runRoutes();
};
setStyle(iframe, iframeStyle);
iframe.src = "about:blank";
const container = options.container
? document.querySelector(options.container)
: document.body;
container.appendChild(iframe);
/* There's a certain case where we might have called setStyle before the iframe was ready.
Make sure we take the last style and apply it */
if (queuedIframeStyle) {
iframe.setAttribute("style", queuedIframeStyle);
queuedIframeStyle = null;
}
}
export default netlifyIdentity;