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
- "0.0.0.0" : true
73
- }
72
+ "0.0.0.0" : true ,
73
+ "identity.netlify.com" : true
74
+ } ;
74
75
75
76
function instantiateGotrue ( APIUrl ) {
76
- const isLocal = localHosts [ document . location . host . split ( ":" ) . shift ( ) ]
77
- const siteURL = isLocal && localStorage . getItem ( "netlifySiteURL" )
77
+ const isLocal = localHosts [ document . location . host . split ( ":" ) . shift ( ) ] ;
78
+ const siteURL = isLocal && localStorage . getItem ( "netlifySiteURL" ) ;
78
79
if ( APIUrl ) {
79
- return new GoTrue ( { APIUrl, setCookie : ! isLocal } )
80
+ return new GoTrue ( { APIUrl, setCookie : ! isLocal } ) ;
80
81
}
81
82
if ( isLocal && siteURL ) {
82
- const parts = [ siteURL ]
83
+ const parts = [ siteURL ] ;
83
84
if ( ! siteURL . match ( / \/ $ / ) ) {
84
- parts . push ( "/" )
85
+ parts . push ( "/" ) ;
85
86
}
86
- parts . push ( ".netlify/identity" )
87
- return new GoTrue ( { APIUrl : parts . join ( "" ) , setCookie : ! isLocal } )
87
+ parts . push ( ".netlify/identity" ) ;
88
+ store . setIsLocal ( isLocal ) ;
89
+ store . setSiteURL ( siteURL ) ;
90
+ return new GoTrue ( { APIUrl : parts . join ( "" ) , setCookie : ! isLocal } ) ;
88
91
}
89
92
if ( isLocal ) {
90
- return null
93
+ store . setIsLocal ( isLocal ) ;
94
+ return null ;
91
95
}
92
96
93
- return new GoTrue ( { setCookie : ! isLocal } )
97
+ return new GoTrue ( { setCookie : ! isLocal } ) ;
94
98
}
95
99
96
- let root
97
- let iframe
100
+ let root ;
101
+ let iframe ;
98
102
const iframeStyle = {
99
103
position : "fixed" ,
100
104
top : 0 ,
@@ -106,129 +110,136 @@ const iframeStyle = {
106
110
background : "transparent" ,
107
111
display : "none" ,
108
112
"z-index" : 99
109
- }
113
+ } ;
110
114
111
115
observe ( store . modal , "isOpen" , ( ) => {
112
116
if ( ! store . settings ) {
113
- store . loadSettings ( )
117
+ store . loadSettings ( ) ;
114
118
}
115
119
setStyle ( iframe , {
116
120
...iframeStyle ,
117
121
display : store . modal . isOpen ? "block !important" : "none"
118
- } )
122
+ } ) ;
119
123
if ( store . modal . isOpen ) {
120
- trigger ( "open" , store . modal . page )
124
+ trigger ( "open" , store . modal . page ) ;
121
125
} else {
122
- trigger ( "close" )
126
+ trigger ( "close" ) ;
123
127
}
124
- } )
128
+ } ) ;
125
129
126
130
observe ( store , "siteURL" , ( ) => {
127
131
if ( store . siteURL === null || store . siteURL === undefined ) {
128
- localStorage . removeItem ( "netlifySiteURL" )
132
+ localStorage . removeItem ( "netlifySiteURL" ) ;
129
133
} else {
130
- localStorage . setItem ( "netlifySiteURL" , store . siteURL )
134
+ localStorage . setItem ( "netlifySiteURL" , store . siteURL ) ;
131
135
}
132
- store . init ( instantiateGotrue ( ) , true )
133
- } )
136
+ store . init ( instantiateGotrue ( ) , true ) ;
137
+ } ) ;
134
138
135
139
observe ( store , "user" , ( ) => {
136
140
if ( store . user ) {
137
- trigger ( "login" , store . user )
141
+ trigger ( "login" , store . user ) ;
138
142
} else {
139
- trigger ( "logout" )
143
+ trigger ( "logout" ) ;
140
144
}
141
- } )
145
+ } ) ;
142
146
143
147
observe ( store , "gotrue" , ( ) => {
144
- store . gotrue && trigger ( "init" , store . gotrue . currentUser ( ) )
145
- } )
148
+ store . gotrue && trigger ( "init" , store . gotrue . currentUser ( ) ) ;
149
+ } ) ;
146
150
147
151
observe ( store , "error" , ( ) => {
148
- trigger ( "error" , store . error )
149
- } )
152
+ trigger ( "error" , store . error ) ;
153
+ } ) ;
150
154
151
- 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 = ( [ ^ & ] + ) /
152
- 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 /
153
- const accessTokenRoute = / a c c e s s _ t o k e n = /
155
+ 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 = ( [ ^ & ] + ) / ;
156
+ 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 / ;
157
+ const accessTokenRoute = / a c c e s s _ t o k e n = / ;
154
158
155
159
function runRoutes ( ) {
156
- const hash = ( document . location . hash || "" ) . replace ( / ^ # \/ ? / , "" )
160
+ const hash = ( document . location . hash || "" ) . replace ( / ^ # \/ ? / , "" ) ;
157
161
if ( ! hash ) {
158
- return
162
+ return ;
159
163
}
160
164
161
- const m = hash . match ( routes )
165
+ const m = hash . match ( routes ) ;
162
166
if ( m ) {
163
- store . verifyToken ( m [ 1 ] , m [ 2 ] )
164
- document . location . hash = ""
167
+ store . verifyToken ( m [ 1 ] , m [ 2 ] ) ;
168
+ document . location . hash = "" ;
165
169
}
166
170
167
- const em = hash . match ( errorRoute )
171
+ const em = hash . match ( errorRoute ) ;
168
172
if ( em ) {
169
- store . openModal ( "signup" )
170
- document . location . hash = ""
173
+ store . openModal ( "signup" ) ;
174
+ document . location . hash = "" ;
171
175
}
172
176
173
- const am = hash . match ( accessTokenRoute )
177
+ const am = hash . match ( accessTokenRoute ) ;
174
178
if ( am ) {
175
- const params = { }
176
- hash . split ( "&" ) . forEach ( ( pair ) => {
177
- const [ key , value ] = pair . split ( "=" )
178
- params [ key ] = value
179
- } )
179
+ const params = { } ;
180
+ hash . split ( "&" ) . forEach ( pair => {
181
+ const [ key , value ] = pair . split ( "=" ) ;
182
+ params [ key ] = value ;
183
+ } ) ;
180
184
if ( ! ! document && params [ "access_token" ] ) {
181
- document . cookie = `nf_jwt=${ params [ "access_token" ] } `
185
+ document . cookie = `nf_jwt=${ params [ "access_token" ] } ` ;
182
186
}
183
- document . location . hash = ""
184
- store . openModal ( "login" )
185
- store . completeExternalLogin ( params )
187
+ document . location . hash = "" ;
188
+ store . openModal ( "login" ) ;
189
+ store . completeExternalLogin ( params ) ;
186
190
}
187
191
}
188
192
189
193
function init ( options = { } ) {
190
- const { APIUrl, logo = true , namePlaceholder } = options
191
- const controlEls = document . querySelectorAll ( "[data-netlify-identity-menu],[data-netlify-identity-button]" )
192
- Array . prototype . slice . call ( controlEls ) . forEach ( ( el ) => {
193
- let controls = null
194
- const mode = el . getAttribute ( "data-netlify-identity-menu" ) === null ? "button" : "menu"
194
+ const { APIUrl, logo = true , namePlaceholder } = options ;
195
+ const controlEls = document . querySelectorAll (
196
+ "[data-netlify-identity-menu],[data-netlify-identity-button]"
197
+ ) ;
198
+ Array . prototype . slice . call ( controlEls ) . forEach ( el => {
199
+ let controls = null ;
200
+ const mode =
201
+ el . getAttribute ( "data-netlify-identity-menu" ) === null
202
+ ? "button"
203
+ : "menu" ;
195
204
render (
196
205
< Provider store = { store } >
197
206
< Controls mode = { mode } text = { el . innerText . trim ( ) } />
198
207
</ Provider > ,
199
208
el ,
200
209
controls
201
- )
202
- } )
203
-
204
- store . init ( instantiateGotrue ( APIUrl ) )
205
- store . modal . logo = logo
206
- store . setNamePlaceholder ( namePlaceholder )
207
- iframe = document . createElement ( "iframe" )
208
- iframe . id = "netlify-identity-widget"
210
+ ) ;
211
+ } ) ;
212
+
213
+ store . init ( instantiateGotrue ( APIUrl ) ) ;
214
+ store . modal . logo = logo ;
215
+ store . setNamePlaceholder ( namePlaceholder ) ;
216
+ iframe = document . createElement ( "iframe" ) ;
217
+ iframe . id = "netlify-identity-widget" ;
209
218
iframe . onload = ( ) => {
210
- const styles = iframe . contentDocument . createElement ( "style" )
211
- styles . innerHTML = modalCSS . toString ( )
212
- iframe . contentDocument . head . appendChild ( styles )
219
+ const styles = iframe . contentDocument . createElement ( "style" ) ;
220
+ styles . innerHTML = modalCSS . toString ( ) ;
221
+ iframe . contentDocument . head . appendChild ( styles ) ;
213
222
root = render (
214
223
< Provider store = { store } >
215
224
< App />
216
225
</ Provider > ,
217
226
iframe . contentDocument . body ,
218
227
root
219
- )
220
- runRoutes ( )
221
- }
222
- setStyle ( iframe , iframeStyle )
223
- iframe . src = "about:blank"
224
- const container = options . container ? document . querySelector ( options . container ) : document . body
225
- container . appendChild ( iframe )
228
+ ) ;
229
+ runRoutes ( ) ;
230
+ } ;
231
+ setStyle ( iframe , iframeStyle ) ;
232
+ iframe . src = "about:blank" ;
233
+ const container = options . container
234
+ ? document . querySelector ( options . container )
235
+ : document . body ;
236
+ container . appendChild ( iframe ) ;
226
237
/* There's a certain case where we might have called setStyle before the iframe was ready.
227
238
Make sure we take the last style and apply it */
228
239
if ( queuedIframeStyle ) {
229
- iframe . setAttribute ( "style" , queuedIframeStyle )
230
- queuedIframeStyle = null
240
+ iframe . setAttribute ( "style" , queuedIframeStyle ) ;
241
+ queuedIframeStyle = null ;
231
242
}
232
243
}
233
244
234
- export default netlifyIdentity
245
+ export default netlifyIdentity ;
0 commit comments