Skip to content

Commit 595cb1c

Browse files
author
sw-yx
committed
add cookies on external login
1 parent 753bff9 commit 595cb1c

File tree

2 files changed

+110
-119
lines changed

2 files changed

+110
-119
lines changed

example/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"netlify-identity-widget": "^1.4.2",
6+
"netlify-identity-widget": "^1.4.14",
77
"react": "^16.6.0",
88
"react-dom": "^16.6.0",
99
"react-router-dom": "^4.3.1",
@@ -15,10 +15,5 @@
1515
"test": "react-scripts test --env=jsdom",
1616
"eject": "react-scripts eject"
1717
},
18-
"browserslist": [
19-
">0.2%",
20-
"not dead",
21-
"not ie <= 11",
22-
"not op_mini all"
23-
]
18+
"browserslist": [">0.2%", "not dead", "not ie <= 11", "not op_mini all"]
2419
}

src/netlify-identity.js

Lines changed: 108 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,100 @@
1-
import { h, render } from "preact";
2-
import { observe } from "mobx";
3-
import { Provider } from "mobx-preact";
4-
import GoTrue from "gotrue-js";
5-
import App from "./components/app";
6-
import store from "./state/store";
7-
import Controls from "./components/controls";
8-
import modalCSS from "./components/modal.css";
9-
10-
const callbacks = {};
1+
import { h, render } from "preact"
2+
import { observe } from "mobx"
3+
import { Provider } from "mobx-preact"
4+
import GoTrue from "gotrue-js"
5+
import App from "./components/app"
6+
import store from "./state/store"
7+
import Controls from "./components/controls"
8+
import modalCSS from "./components/modal.css"
9+
10+
const callbacks = {}
1111
function trigger(callback) {
12-
(callbacks[callback] || []).forEach(cb => {
13-
cb.apply(cb, Array.prototype.slice.call(arguments, 1));
14-
});
12+
;(callbacks[callback] || []).forEach((cb) => {
13+
cb.apply(cb, Array.prototype.slice.call(arguments, 1))
14+
})
1515
}
1616

1717
const validActions = {
1818
login: true,
1919
signup: true,
2020
error: true
21-
};
21+
}
2222

2323
const netlifyIdentity = {
2424
on: (event, cb) => {
25-
callbacks[event] = callbacks[event] || [];
26-
callbacks[event].push(cb);
25+
callbacks[event] = callbacks[event] || []
26+
callbacks[event].push(cb)
2727
},
28-
open: action => {
29-
action = action || "login";
28+
open: (action) => {
29+
action = action || "login"
3030
if (!validActions[action]) {
31-
throw new Error(`Invalid action for open: ${action}`);
31+
throw new Error(`Invalid action for open: ${action}`)
3232
}
33-
store.openModal(store.user ? "user" : action);
33+
store.openModal(store.user ? "user" : action)
3434
},
3535
close: () => {
36-
store.closeModal();
36+
store.closeModal()
3737
},
3838
currentUser: () => {
39-
return store.gotrue && store.gotrue.currentUser();
39+
return store.gotrue && store.gotrue.currentUser()
4040
},
4141
logout: () => {
42-
return store.logout();
42+
return store.logout()
4343
},
4444
get gotrue() {
4545
if (!store.gotrue) {
46-
store.openModal("login");
46+
store.openModal("login")
4747
}
48-
return store.gotrue;
48+
return store.gotrue
4949
},
50-
init: options => {
51-
init(options);
50+
init: (options) => {
51+
init(options)
5252
},
5353
store
54-
};
54+
}
5555

56-
let queuedIframeStyle = null;
56+
let queuedIframeStyle = null
5757
function setStyle(el, css) {
58-
let style = "";
58+
let style = ""
5959
for (const key in css) {
60-
style += `${key}: ${css[key]}; `;
60+
style += `${key}: ${css[key]}; `
6161
}
6262
if (el) {
63-
el.setAttribute("style", style);
63+
el.setAttribute("style", style)
6464
} else {
65-
queuedIframeStyle = style;
65+
queuedIframeStyle = style
6666
}
6767
}
6868

6969
const localHosts = {
7070
localhost: true,
7171
"127.0.0.1": true,
7272
"0.0.0.0": true
73-
};
73+
}
7474

7575
function instantiateGotrue(APIUrl) {
76-
const isLocal = localHosts[document.location.host.split(":").shift()];
77-
const siteURL = isLocal && localStorage.getItem("netlifySiteURL");
76+
const isLocal = localHosts[document.location.host.split(":").shift()]
77+
const siteURL = isLocal && localStorage.getItem("netlifySiteURL")
7878
if (APIUrl) {
79-
return new GoTrue({ APIUrl, setCookie: !isLocal });
79+
return new GoTrue({ APIUrl, setCookie: !isLocal })
8080
}
8181
if (isLocal && siteURL) {
82-
const parts = [siteURL];
82+
const parts = [siteURL]
8383
if (!siteURL.match(/\/$/)) {
84-
parts.push("/");
84+
parts.push("/")
8585
}
86-
parts.push(".netlify/identity");
87-
return new GoTrue({ APIUrl: parts.join(""), setCookie: !isLocal });
86+
parts.push(".netlify/identity")
87+
return new GoTrue({ APIUrl: parts.join(""), setCookie: !isLocal })
8888
}
8989
if (isLocal) {
90-
return null;
90+
return null
9191
}
9292

93-
return new GoTrue({ setCookie: !isLocal });
93+
return new GoTrue({ setCookie: !isLocal })
9494
}
9595

96-
let root;
97-
let iframe;
96+
let root
97+
let iframe
9898
const iframeStyle = {
9999
position: "fixed",
100100
top: 0,
@@ -106,128 +106,124 @@ const iframeStyle = {
106106
background: "transparent",
107107
display: "none",
108108
"z-index": 99
109-
};
109+
}
110110

111111
observe(store.modal, "isOpen", () => {
112112
if (!store.settings) {
113-
store.loadSettings();
113+
store.loadSettings()
114114
}
115115
setStyle(iframe, {
116116
...iframeStyle,
117117
display: store.modal.isOpen ? "block !important" : "none"
118-
});
118+
})
119119
if (store.modal.isOpen) {
120-
trigger("open", store.modal.page);
120+
trigger("open", store.modal.page)
121121
} else {
122-
trigger("close");
122+
trigger("close")
123123
}
124-
});
124+
})
125125

126126
observe(store, "siteURL", () => {
127-
localStorage.setItem("netlifySiteURL", store.siteURL);
128-
store.init(instantiateGotrue(), true);
129-
});
127+
localStorage.setItem("netlifySiteURL", store.siteURL)
128+
store.init(instantiateGotrue(), true)
129+
})
130130

131131
observe(store, "user", () => {
132132
if (store.user) {
133-
trigger("login", store.user);
133+
trigger("login", store.user)
134134
} else {
135-
trigger("logout");
135+
trigger("logout")
136136
}
137-
});
137+
})
138138

139139
observe(store, "gotrue", () => {
140-
store.gotrue && trigger("init", store.gotrue.currentUser());
141-
});
140+
store.gotrue && trigger("init", store.gotrue.currentUser())
141+
})
142142

143143
observe(store, "error", () => {
144-
trigger("error", store.error);
145-
});
144+
trigger("error", store.error)
145+
})
146146

147-
const routes = /(confirmation|invite|recovery|email_change)_token=([^&]+)/;
148-
const errorRoute = /error=access_denied&error_description=403/;
149-
const accessTokenRoute = /access_token=/;
147+
const routes = /(confirmation|invite|recovery|email_change)_token=([^&]+)/
148+
const errorRoute = /error=access_denied&error_description=403/
149+
const accessTokenRoute = /access_token=/
150150

151151
function runRoutes() {
152-
const hash = (document.location.hash || "").replace(/^#\/?/, "");
152+
const hash = (document.location.hash || "").replace(/^#\/?/, "")
153153
if (!hash) {
154-
return;
154+
return
155155
}
156156

157-
const m = hash.match(routes);
157+
const m = hash.match(routes)
158158
if (m) {
159-
store.verifyToken(m[1], m[2]);
160-
document.location.hash = "";
159+
store.verifyToken(m[1], m[2])
160+
document.location.hash = ""
161161
}
162162

163-
const em = hash.match(errorRoute);
163+
const em = hash.match(errorRoute)
164164
if (em) {
165-
store.openModal("signup");
166-
document.location.hash = "";
165+
store.openModal("signup")
166+
document.location.hash = ""
167167
}
168168

169-
const am = hash.match(accessTokenRoute);
169+
const am = hash.match(accessTokenRoute)
170170
if (am) {
171-
const params = {};
172-
hash.split("&").forEach(pair => {
173-
const [key, value] = pair.split("=");
174-
params[key] = value;
175-
});
176-
document.location.hash = "";
177-
store.openModal("login");
178-
store.completeExternalLogin(params);
171+
const params = {}
172+
hash.split("&").forEach((pair) => {
173+
const [key, value] = pair.split("=")
174+
params[key] = value
175+
})
176+
if (!!document && params["store"]) {
177+
document.cookie = `nf_jwt=${params["store"]}`
178+
}
179+
document.location.hash = ""
180+
store.openModal("login")
181+
store.completeExternalLogin(params)
179182
}
180183
}
181184

182185
function init(options = {}) {
183-
const { APIUrl, logo = true } = options;
184-
const controlEls = document.querySelectorAll(
185-
"[data-netlify-identity-menu],[data-netlify-identity-button]"
186-
);
187-
Array.prototype.slice.call(controlEls).forEach(el => {
188-
let controls = null;
189-
const mode =
190-
el.getAttribute("data-netlify-identity-menu") === null
191-
? "button"
192-
: "menu";
186+
const { APIUrl, logo = true } = options
187+
const controlEls = document.querySelectorAll("[data-netlify-identity-menu],[data-netlify-identity-button]")
188+
Array.prototype.slice.call(controlEls).forEach((el) => {
189+
let controls = null
190+
const mode = el.getAttribute("data-netlify-identity-menu") === null ? "button" : "menu"
193191
render(
194192
<Provider store={store}>
195193
<Controls mode={mode} text={el.innerText.trim()} />
196194
</Provider>,
197195
el,
198196
controls
199-
);
200-
});
197+
)
198+
})
201199

202-
store.init(instantiateGotrue(APIUrl));
203-
store.modal.logo = logo;
204-
iframe = document.createElement("iframe");
205-
iframe.id = "netlify-identity-widget";
200+
store.init(instantiateGotrue(APIUrl))
201+
store.modal.logo = logo
202+
iframe = document.createElement("iframe")
203+
iframe.id = "netlify-identity-widget"
206204
iframe.onload = () => {
207-
const styles = iframe.contentDocument.createElement("style");
208-
styles.innerHTML = modalCSS.toString();
209-
iframe.contentDocument.head.appendChild(styles);
205+
const styles = iframe.contentDocument.createElement("style")
206+
styles.innerHTML = modalCSS.toString()
207+
iframe.contentDocument.head.appendChild(styles)
210208
root = render(
211209
<Provider store={store}>
212210
<App />
213211
</Provider>,
214212
iframe.contentDocument.body,
215213
root
216-
);
217-
runRoutes();
218-
};
219-
setStyle(iframe, iframeStyle);
220-
iframe.src = "about:blank";
221-
const container = options.container
222-
? document.querySelector(options.container)
223-
: document.body;
224-
container.appendChild(iframe);
214+
)
215+
runRoutes()
216+
}
217+
setStyle(iframe, iframeStyle)
218+
iframe.src = "about:blank"
219+
const container = options.container ? document.querySelector(options.container) : document.body
220+
container.appendChild(iframe)
225221
/* There's a certain case where we might have called setStyle before the iframe was ready.
226222
Make sure we take the last style and apply it */
227223
if (queuedIframeStyle) {
228-
iframe.setAttribute("style", queuedIframeStyle);
229-
queuedIframeStyle = null;
224+
iframe.setAttribute("style", queuedIframeStyle)
225+
queuedIframeStyle = null
230226
}
231227
}
232228

233-
export default netlifyIdentity;
229+
export default netlifyIdentity

0 commit comments

Comments
 (0)