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
- return new GoTrue ( { APIUrl : parts . join ( "" ) , setCookie : ! isLocal } ) ;
86
+ parts . push ( ".netlify/identity" )
87
+ return new GoTrue ( { APIUrl : parts . join ( "" ) , setCookie : ! isLocal } )
88
88
}
89
89
if ( isLocal ) {
90
- return null ;
90
+ return null
91
91
}
92
92
93
- return new GoTrue ( { setCookie : ! isLocal } ) ;
93
+ return new GoTrue ( { setCookie : ! isLocal } )
94
94
}
95
95
96
- let root ;
97
- let iframe ;
96
+ let root
97
+ let iframe
98
98
const iframeStyle = {
99
99
position : "fixed" ,
100
100
top : 0 ,
@@ -106,128 +106,124 @@ const iframeStyle = {
106
106
background : "transparent" ,
107
107
display : "none" ,
108
108
"z-index" : 99
109
- } ;
109
+ }
110
110
111
111
observe ( store . modal , "isOpen" , ( ) => {
112
112
if ( ! store . settings ) {
113
- store . loadSettings ( ) ;
113
+ store . loadSettings ( )
114
114
}
115
115
setStyle ( iframe , {
116
116
...iframeStyle ,
117
117
display : store . modal . isOpen ? "block !important" : "none"
118
- } ) ;
118
+ } )
119
119
if ( store . modal . isOpen ) {
120
- trigger ( "open" , store . modal . page ) ;
120
+ trigger ( "open" , store . modal . page )
121
121
} else {
122
- trigger ( "close" ) ;
122
+ trigger ( "close" )
123
123
}
124
- } ) ;
124
+ } )
125
125
126
126
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
+ } )
130
130
131
131
observe ( store , "user" , ( ) => {
132
132
if ( store . user ) {
133
- trigger ( "login" , store . user ) ;
133
+ trigger ( "login" , store . user )
134
134
} else {
135
- trigger ( "logout" ) ;
135
+ trigger ( "logout" )
136
136
}
137
- } ) ;
137
+ } )
138
138
139
139
observe ( store , "gotrue" , ( ) => {
140
- store . gotrue && trigger ( "init" , store . gotrue . currentUser ( ) ) ;
141
- } ) ;
140
+ store . gotrue && trigger ( "init" , store . gotrue . currentUser ( ) )
141
+ } )
142
142
143
143
observe ( store , "error" , ( ) => {
144
- trigger ( "error" , store . error ) ;
145
- } ) ;
144
+ trigger ( "error" , store . error )
145
+ } )
146
146
147
- 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 = ( [ ^ & ] + ) / ;
148
- 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 / ;
149
- const accessTokenRoute = / a c c e s s _ t o k e n = / ;
147
+ 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 = ( [ ^ & ] + ) /
148
+ 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 /
149
+ const accessTokenRoute = / a c c e s s _ t o k e n = /
150
150
151
151
function runRoutes ( ) {
152
- const hash = ( document . location . hash || "" ) . replace ( / ^ # \/ ? / , "" ) ;
152
+ const hash = ( document . location . hash || "" ) . replace ( / ^ # \/ ? / , "" )
153
153
if ( ! hash ) {
154
- return ;
154
+ return
155
155
}
156
156
157
- const m = hash . match ( routes ) ;
157
+ const m = hash . match ( routes )
158
158
if ( m ) {
159
- store . verifyToken ( m [ 1 ] , m [ 2 ] ) ;
160
- document . location . hash = "" ;
159
+ store . verifyToken ( m [ 1 ] , m [ 2 ] )
160
+ document . location . hash = ""
161
161
}
162
162
163
- const em = hash . match ( errorRoute ) ;
163
+ const em = hash . match ( errorRoute )
164
164
if ( em ) {
165
- store . openModal ( "signup" ) ;
166
- document . location . hash = "" ;
165
+ store . openModal ( "signup" )
166
+ document . location . hash = ""
167
167
}
168
168
169
- const am = hash . match ( accessTokenRoute ) ;
169
+ const am = hash . match ( accessTokenRoute )
170
170
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 )
179
182
}
180
183
}
181
184
182
185
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"
193
191
render (
194
192
< Provider store = { store } >
195
193
< Controls mode = { mode } text = { el . innerText . trim ( ) } />
196
194
</ Provider > ,
197
195
el ,
198
196
controls
199
- ) ;
200
- } ) ;
197
+ )
198
+ } )
201
199
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"
206
204
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 )
210
208
root = render (
211
209
< Provider store = { store } >
212
210
< App />
213
211
</ Provider > ,
214
212
iframe . contentDocument . body ,
215
213
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 )
225
221
/* There's a certain case where we might have called setStyle before the iframe was ready.
226
222
Make sure we take the last style and apply it */
227
223
if ( queuedIframeStyle ) {
228
- iframe . setAttribute ( "style" , queuedIframeStyle ) ;
229
- queuedIframeStyle = null ;
224
+ iframe . setAttribute ( "style" , queuedIframeStyle )
225
+ queuedIframeStyle = null
230
226
}
231
227
}
232
228
233
- export default netlifyIdentity ;
229
+ export default netlifyIdentity
0 commit comments