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 = { } ;
11
11
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
+ } ) ;
15
15
}
16
16
17
17
const validActions = {
18
18
login : true ,
19
19
signup : true ,
20
20
error : true
21
- }
21
+ } ;
22
22
23
23
const netlifyIdentity = {
24
24
on : ( event , cb ) => {
25
- callbacks [ event ] = callbacks [ event ] || [ ]
26
- callbacks [ event ] . push ( cb )
25
+ callbacks [ event ] = callbacks [ event ] || [ ] ;
26
+ callbacks [ event ] . push ( cb ) ;
27
27
} ,
28
- open : ( action ) => {
29
- action = action || "login"
28
+ open : action => {
29
+ action = action || "login" ;
30
30
if ( ! validActions [ action ] ) {
31
- throw new Error ( `Invalid action for open: ${ action } ` )
31
+ throw new Error ( `Invalid action for open: ${ action } ` ) ;
32
32
}
33
- store . openModal ( store . user ? "user" : action )
33
+ store . openModal ( store . user ? "user" : action ) ;
34
34
} ,
35
35
close : ( ) => {
36
- store . closeModal ( )
36
+ store . closeModal ( ) ;
37
37
} ,
38
38
currentUser : ( ) => {
39
- return store . gotrue && store . gotrue . currentUser ( )
39
+ return store . gotrue && store . gotrue . currentUser ( ) ;
40
40
} ,
41
41
logout : ( ) => {
42
- return store . logout ( )
42
+ return store . logout ( ) ;
43
43
} ,
44
44
get gotrue ( ) {
45
45
if ( ! store . gotrue ) {
46
- store . openModal ( "login" )
46
+ store . openModal ( "login" ) ;
47
47
}
48
- return store . gotrue
48
+ return store . gotrue ;
49
49
} ,
50
- init : ( options ) => {
51
- init ( options )
50
+ init : options => {
51
+ init ( options ) ;
52
52
} ,
53
53
store
54
- }
54
+ } ;
55
55
56
- let queuedIframeStyle = null
56
+ let queuedIframeStyle = null ;
57
57
function setStyle ( el , css ) {
58
- let style = ""
58
+ let style = "" ;
59
59
for ( const key in css ) {
60
- style += `${ key } : ${ css [ key ] } ; `
60
+ style += `${ key } : ${ css [ key ] } ; ` ;
61
61
}
62
62
if ( el ) {
63
- el . setAttribute ( "style" , style )
63
+ el . setAttribute ( "style" , style ) ;
64
64
} else {
65
- queuedIframeStyle = style
65
+ queuedIframeStyle = style ;
66
66
}
67
67
}
68
68
69
69
const localHosts = {
70
70
localhost : true ,
71
71
"127.0.0.1" : true ,
72
72
"0.0.0.0" : true
73
- }
73
+ } ;
74
74
75
75
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" ) ;
78
78
if ( APIUrl ) {
79
- return new GoTrue ( { APIUrl, setCookie : ! isLocal } )
79
+ return new GoTrue ( { APIUrl, setCookie : ! isLocal } ) ;
80
80
}
81
81
if ( isLocal && siteURL ) {
82
- const parts = [ siteURL ]
82
+ const parts = [ siteURL ] ;
83
83
if ( ! siteURL . match ( / \/ $ / ) ) {
84
- parts . push ( "/" )
84
+ parts . push ( "/" ) ;
85
85
}
86
- parts . push ( ".netlify/identity" )
87
- store . setSiteURL ( siteURL )
88
- return new GoTrue ( { APIUrl : parts . join ( "" ) , setCookie : ! isLocal } )
86
+ parts . push ( ".netlify/identity" ) ;
87
+ store . setSiteURL ( siteURL ) ;
88
+ return new GoTrue ( { APIUrl : parts . join ( "" ) , setCookie : ! isLocal } ) ;
89
89
}
90
90
if ( isLocal ) {
91
- return null
91
+ return null ;
92
92
}
93
93
94
- return new GoTrue ( { setCookie : ! isLocal } )
94
+ return new GoTrue ( { setCookie : ! isLocal } ) ;
95
95
}
96
96
97
- let root
98
- let iframe
97
+ let root ;
98
+ let iframe ;
99
99
const iframeStyle = {
100
100
position : "fixed" ,
101
101
top : 0 ,
@@ -107,129 +107,136 @@ const iframeStyle = {
107
107
background : "transparent" ,
108
108
display : "none" ,
109
109
"z-index" : 99
110
- }
110
+ } ;
111
111
112
112
observe ( store . modal , "isOpen" , ( ) => {
113
113
if ( ! store . settings ) {
114
- store . loadSettings ( )
114
+ store . loadSettings ( ) ;
115
115
}
116
116
setStyle ( iframe , {
117
117
...iframeStyle ,
118
118
display : store . modal . isOpen ? "block !important" : "none"
119
- } )
119
+ } ) ;
120
120
if ( store . modal . isOpen ) {
121
- trigger ( "open" , store . modal . page )
121
+ trigger ( "open" , store . modal . page ) ;
122
122
} else {
123
- trigger ( "close" )
123
+ trigger ( "close" ) ;
124
124
}
125
- } )
125
+ } ) ;
126
126
127
127
observe ( store , "siteURL" , ( ) => {
128
128
if ( store . siteURL === null || store . siteURL === undefined ) {
129
- localStorage . removeItem ( "netlifySiteURL" )
129
+ localStorage . removeItem ( "netlifySiteURL" ) ;
130
130
} else {
131
- localStorage . setItem ( "netlifySiteURL" , store . siteURL )
131
+ localStorage . setItem ( "netlifySiteURL" , store . siteURL ) ;
132
132
}
133
- store . init ( instantiateGotrue ( ) , true )
134
- } )
133
+ store . init ( instantiateGotrue ( ) , true ) ;
134
+ } ) ;
135
135
136
136
observe ( store , "user" , ( ) => {
137
137
if ( store . user ) {
138
- trigger ( "login" , store . user )
138
+ trigger ( "login" , store . user ) ;
139
139
} else {
140
- trigger ( "logout" )
140
+ trigger ( "logout" ) ;
141
141
}
142
- } )
142
+ } ) ;
143
143
144
144
observe ( store , "gotrue" , ( ) => {
145
- store . gotrue && trigger ( "init" , store . gotrue . currentUser ( ) )
146
- } )
145
+ store . gotrue && trigger ( "init" , store . gotrue . currentUser ( ) ) ;
146
+ } ) ;
147
147
148
148
observe ( store , "error" , ( ) => {
149
- trigger ( "error" , store . error )
150
- } )
149
+ trigger ( "error" , store . error ) ;
150
+ } ) ;
151
151
152
- const routes = / ( c o n f i r m a t i o n | i n v i t e | r e c o v e r y | e m a i l _ c h a n g e ) _ t o k e n = ( [ ^ & ] + ) /
153
- const errorRoute = / e r r o r = a c c e s s _ d e n i e d & e r r o r _ d e s c r i p t i o n = 4 0 3 /
154
- const accessTokenRoute = / a c c e s s _ t o k e n = /
152
+ const routes = / ( c o n f i r m a t i o n | i n v i t e | r e c o v e r y | e m a i l _ c h a n g e ) _ t o k e n = ( [ ^ & ] + ) / ;
153
+ const errorRoute = / e r r o r = a c c e s s _ d e n i e d & e r r o r _ d e s c r i p t i o n = 4 0 3 / ;
154
+ const accessTokenRoute = / a c c e s s _ t o k e n = / ;
155
155
156
156
function runRoutes ( ) {
157
- const hash = ( document . location . hash || "" ) . replace ( / ^ # \/ ? / , "" )
157
+ const hash = ( document . location . hash || "" ) . replace ( / ^ # \/ ? / , "" ) ;
158
158
if ( ! hash ) {
159
- return
159
+ return ;
160
160
}
161
161
162
- const m = hash . match ( routes )
162
+ const m = hash . match ( routes ) ;
163
163
if ( m ) {
164
- store . verifyToken ( m [ 1 ] , m [ 2 ] )
165
- document . location . hash = ""
164
+ store . verifyToken ( m [ 1 ] , m [ 2 ] ) ;
165
+ document . location . hash = "" ;
166
166
}
167
167
168
- const em = hash . match ( errorRoute )
168
+ const em = hash . match ( errorRoute ) ;
169
169
if ( em ) {
170
- store . openModal ( "signup" )
171
- document . location . hash = ""
170
+ store . openModal ( "signup" ) ;
171
+ document . location . hash = "" ;
172
172
}
173
173
174
- const am = hash . match ( accessTokenRoute )
174
+ const am = hash . match ( accessTokenRoute ) ;
175
175
if ( am ) {
176
- const params = { }
177
- hash . split ( "&" ) . forEach ( ( pair ) => {
178
- const [ key , value ] = pair . split ( "=" )
179
- params [ key ] = value
180
- } )
176
+ const params = { } ;
177
+ hash . split ( "&" ) . forEach ( pair => {
178
+ const [ key , value ] = pair . split ( "=" ) ;
179
+ params [ key ] = value ;
180
+ } ) ;
181
181
if ( ! ! document && params [ "access_token" ] ) {
182
- document . cookie = `nf_jwt=${ params [ "access_token" ] } `
182
+ document . cookie = `nf_jwt=${ params [ "access_token" ] } ` ;
183
183
}
184
- document . location . hash = ""
185
- store . openModal ( "login" )
186
- store . completeExternalLogin ( params )
184
+ document . location . hash = "" ;
185
+ store . openModal ( "login" ) ;
186
+ store . completeExternalLogin ( params ) ;
187
187
}
188
188
}
189
189
190
190
function init ( options = { } ) {
191
- const { APIUrl, logo = true , namePlaceholder } = options
192
- const controlEls = document . querySelectorAll ( "[data-netlify-identity-menu],[data-netlify-identity-button]" )
193
- Array . prototype . slice . call ( controlEls ) . forEach ( ( el ) => {
194
- let controls = null
195
- const mode = el . getAttribute ( "data-netlify-identity-menu" ) === null ? "button" : "menu"
191
+ const { APIUrl, logo = true , namePlaceholder } = options ;
192
+ const controlEls = document . querySelectorAll (
193
+ "[data-netlify-identity-menu],[data-netlify-identity-button]"
194
+ ) ;
195
+ Array . prototype . slice . call ( controlEls ) . forEach ( el => {
196
+ let controls = null ;
197
+ const mode =
198
+ el . getAttribute ( "data-netlify-identity-menu" ) === null
199
+ ? "button"
200
+ : "menu" ;
196
201
render (
197
202
< Provider store = { store } >
198
203
< Controls mode = { mode } text = { el . innerText . trim ( ) } />
199
204
</ Provider > ,
200
205
el ,
201
206
controls
202
- )
203
- } )
204
-
205
- store . init ( instantiateGotrue ( APIUrl ) )
206
- store . modal . logo = logo
207
- store . setNamePlaceholder ( namePlaceholder )
208
- iframe = document . createElement ( "iframe" )
209
- iframe . id = "netlify-identity-widget"
207
+ ) ;
208
+ } ) ;
209
+
210
+ store . init ( instantiateGotrue ( APIUrl ) ) ;
211
+ store . modal . logo = logo ;
212
+ store . setNamePlaceholder ( namePlaceholder ) ;
213
+ iframe = document . createElement ( "iframe" ) ;
214
+ iframe . id = "netlify-identity-widget" ;
210
215
iframe . onload = ( ) => {
211
- const styles = iframe . contentDocument . createElement ( "style" )
212
- styles . innerHTML = modalCSS . toString ( )
213
- iframe . contentDocument . head . appendChild ( styles )
216
+ const styles = iframe . contentDocument . createElement ( "style" ) ;
217
+ styles . innerHTML = modalCSS . toString ( ) ;
218
+ iframe . contentDocument . head . appendChild ( styles ) ;
214
219
root = render (
215
220
< Provider store = { store } >
216
221
< App />
217
222
</ Provider > ,
218
223
iframe . contentDocument . body ,
219
224
root
220
- )
221
- runRoutes ( )
222
- }
223
- setStyle ( iframe , iframeStyle )
224
- iframe . src = "about:blank"
225
- const container = options . container ? document . querySelector ( options . container ) : document . body
226
- container . appendChild ( iframe )
225
+ ) ;
226
+ runRoutes ( ) ;
227
+ } ;
228
+ setStyle ( iframe , iframeStyle ) ;
229
+ iframe . src = "about:blank" ;
230
+ const container = options . container
231
+ ? document . querySelector ( options . container )
232
+ : document . body ;
233
+ container . appendChild ( iframe ) ;
227
234
/* There's a certain case where we might have called setStyle before the iframe was ready.
228
235
Make sure we take the last style and apply it */
229
236
if ( queuedIframeStyle ) {
230
- iframe . setAttribute ( "style" , queuedIframeStyle )
231
- queuedIframeStyle = null
237
+ iframe . setAttribute ( "style" , queuedIframeStyle ) ;
238
+ queuedIframeStyle = null ;
232
239
}
233
240
}
234
241
235
- export default netlifyIdentity
242
+ export default netlifyIdentity ;
0 commit comments