1
- import ' shiny' ;
2
- import {
3
- getAuth ,
4
- signOut ,
1
+ import " shiny" ;
2
+ import {
3
+ getAuth ,
4
+ signOut ,
5
5
setPersistence ,
6
6
isSignInWithEmailLink ,
7
7
signInWithEmailLink ,
8
- onAuthStateChanged
8
+ onAuthStateChanged ,
9
9
} from "firebase/auth" ;
10
10
import { initializeApp } from "firebase/app" ;
11
- import ' ./style.css' ;
12
- import {
13
- persistenceOpts ,
14
- showHideOnLogin ,
11
+ import " ./style.css" ;
12
+ import {
13
+ persistenceOpts ,
14
+ showHideOnLogin ,
15
15
showHideOnLogout ,
16
16
setInputValue ,
17
- setLanguageCode
18
- } from ' ./utils.js' ;
19
- import { handleStorage } from ' ./components/storage' ;
17
+ setLanguageCode ,
18
+ } from " ./utils.js" ;
19
+ import { handleStorage } from " ./components/storage" ;
20
20
21
21
// global variables
22
- window . globalNs = '' ;
22
+ window . globalNs = "" ;
23
23
var globalInit = false ;
24
24
window . uiInitialised = false ;
25
25
let firebaseApp ;
26
26
27
- Shiny . addCustomMessageHandler ( ' fireblaze-render-dependencies' , ( msg ) => {
27
+ Shiny . addCustomMessageHandler ( " fireblaze-render-dependencies" , ( msg ) => {
28
28
Shiny . renderDependencies ( msg . deps ) ;
29
- } )
29
+ } ) ;
30
30
31
- Shiny . addCustomMessageHandler ( 'fireblaze-initialize-core' , ( msg ) => {
32
- if ( firebaseApp )
33
- return ;
31
+ Shiny . addCustomMessageHandler ( "fireblaze-initialize-core" , ( msg ) => {
32
+ if ( firebaseApp ) return ;
34
33
35
34
firebaseApp = initializeApp ( msg . conf ) ;
36
35
} ) ;
37
36
38
- Shiny . addCustomMessageHandler ( 'fireblaze-expose-app' , ( msg ) => {
39
- if ( ! firebaseApp )
40
- return ;
37
+ Shiny . addCustomMessageHandler ( "fireblaze-expose-app" , ( msg ) => {
38
+ if ( ! firebaseApp ) return ;
41
39
42
40
window . firebaseApp = firebaseApp ;
43
41
} ) ;
44
42
45
- Shiny . addCustomMessageHandler ( ' fireblaze-expose-auth' , ( msg ) => {
43
+ Shiny . addCustomMessageHandler ( " fireblaze-expose-auth" , ( msg ) => {
46
44
window . firebaseAuth = getAuth ( ) ;
47
45
} ) ;
48
46
49
- Shiny . addCustomMessageHandler ( ' fireblaze-initialize-storage' , ( msg ) => {
47
+ Shiny . addCustomMessageHandler ( " fireblaze-initialize-storage" , ( msg ) => {
50
48
handleStorage ( firebaseApp ) ;
51
49
} ) ;
52
50
53
51
// Initialise
54
- Shiny . addCustomMessageHandler ( 'fireblaze-initialize-auth' , ( msg ) => {
55
-
56
- if ( globalInit )
57
- return ;
52
+ Shiny . addCustomMessageHandler ( "fireblaze-initialize-auth" , ( msg ) => {
53
+ if ( globalInit ) return ;
58
54
59
55
globalInit = true ;
60
56
// set namespace
61
57
window . globalNs = msg . ns ;
62
58
63
59
// auth
64
60
const auth = getAuth ( ) ;
65
- setLanguageCode ( msg . languageCode )
61
+ setLanguageCode ( msg . languageCode ) ;
66
62
67
63
// set persistence
68
64
let persistence = persistenceOpts ( msg . persistence ) ;
69
65
setPersistence ( auth , persistence )
70
66
. then ( ( ) => {
71
67
onAuthStateChanged ( auth , ( user ) => {
72
- if ( user ) {
73
-
68
+ if ( user ) {
74
69
// show signin authorised
75
70
showHideOnLogin ( "show" ) ;
76
71
showHideOnLogout ( "hide" ) ;
77
72
$ ( "#fireblaze-signin-ui" ) . hide ( ) ;
78
73
79
- auth . currentUser . getIdToken ( true )
80
- . then ( function ( token ) {
81
- setInputValue ( 'signed_in_user' , { success : true , response : user , token : token } ) ;
82
- } ) . catch ( function ( error ) {
83
- console . error ( 'failed to login' ) ;
74
+ auth . currentUser
75
+ . getIdToken ( true )
76
+ . then ( function ( token ) {
77
+ setInputValue ( "signed_in_user" , {
78
+ success : true ,
79
+ response : user ,
80
+ token : token ,
81
+ } ) ;
82
+ } )
83
+ . catch ( function ( ) {
84
+ console . error ( "failed to login" ) ;
84
85
} ) ;
85
86
86
- } else {
87
- // hide signin required
88
- showHideOnLogin ( "hide" ) ;
89
- showHideOnLogout ( "show" ) ;
90
-
91
- // set error input
92
- setInputValue ( 'signed_in' , { success : false , response : null } ) ;
93
- setInputValue ( 'signed_in_user' , { success : false , response : null } ) ;
87
+ return ;
94
88
}
89
+ // hide signin required
90
+ showHideOnLogin ( "hide" ) ;
91
+ showHideOnLogout ( "show" ) ;
92
+
93
+ // set error input
94
+ setInputValue ( "signed_in" , { success : false , response : null } ) ;
95
+ setInputValue ( "signed_in_user" , { success : false , response : null } ) ;
95
96
} ) ;
96
97
97
98
// check email verification link
@@ -101,68 +102,96 @@ Shiny.addCustomMessageHandler('fireblaze-initialize-auth', (msg) => {
101
102
// the sign-in operation.
102
103
// Get the email if available. This should be available if the user completes
103
104
// the flow on the same device where they started it.
104
- var email = window . localStorage . getItem ( ' fireblazeEmailSignIn' ) ;
105
+ var email = window . localStorage . getItem ( " fireblazeEmailSignIn" ) ;
105
106
if ( ! email ) {
106
- setInputValue ( 'email_verification' , { success : false , response : "Cannot find email" } ) ;
107
+ setInputValue ( "email_verification" , {
108
+ success : false ,
109
+ response : "Cannot find email" ,
110
+ } ) ;
107
111
}
108
112
// The client SDK will parse the code from the link for you.
109
113
signInWithEmailLink ( auth , email , window . location . href )
110
114
. then ( ( result ) => {
111
- window . localStorage . removeItem ( 'fireblazeEmailSignIn' ) ;
112
- setInputValue ( 'email_verification' , { success : true , response : result } ) ;
115
+ window . localStorage . removeItem ( "fireblazeEmailSignIn" ) ;
116
+ setInputValue ( "email_verification" , {
117
+ success : true ,
118
+ response : result ,
119
+ } ) ;
113
120
} )
114
121
. catch ( ( error ) => {
115
- setInputValue ( 'email_verification' , { success : false , response : error } ) ;
122
+ setInputValue ( "email_verification" , {
123
+ success : false ,
124
+ response : error ,
125
+ } ) ;
116
126
} ) ;
117
127
}
118
128
} )
119
129
. catch ( ( error ) => {
120
130
console . error ( error ) ;
121
131
} ) ;
122
-
123
132
} ) ;
124
133
125
134
// Sign out
126
- Shiny . addCustomMessageHandler ( ' fireblaze-signout' , ( msg ) => {
135
+ Shiny . addCustomMessageHandler ( " fireblaze-signout" , ( msg ) => {
127
136
const auth = getAuth ( ) ;
128
137
129
138
signOut ( auth )
130
139
. then ( ( ) => {
131
- if ( window . uiInitialised ) {
140
+ if ( window . uiInitialised ) {
132
141
window . ui . start ( "#fireblaze-signin-ui" , window . uiOpts ) ;
133
142
$ ( "#fireblaze-signin-ui" ) . show ( ) ;
134
143
}
135
144
136
- setInputValue ( 'signout' , { success : true , response : 'successful' } , msg . ns ) ;
137
- } ) . catch ( ( error ) => {
138
- setInputValue ( 'signout' , { success : false , response : error } , msg . ns ) ;
145
+ setInputValue (
146
+ "signout" ,
147
+ { success : true , response : "successful" } ,
148
+ msg . ns ,
149
+ ) ;
150
+ } )
151
+ . catch ( ( error ) => {
152
+ setInputValue ( "signout" , { success : false , response : error } , msg . ns ) ;
139
153
} ) ;
140
-
141
154
} ) ;
142
155
143
156
// Language code
144
- Shiny . addCustomMessageHandler ( ' fireblaze-language-code' , ( msg ) => {
157
+ Shiny . addCustomMessageHandler ( " fireblaze-language-code" , ( msg ) => {
145
158
const auth = getAuth ( ) ;
146
159
auth . languageCode = msg . code ;
147
160
} ) ;
148
161
149
162
// Delete User
150
- Shiny . addCustomMessageHandler ( ' fireblaze-delete-user' , ( msg ) => {
163
+ Shiny . addCustomMessageHandler ( " fireblaze-delete-user" , ( msg ) => {
151
164
const auth = getAuth ( ) ;
152
- auth . currentUser . delete ( )
165
+ auth . currentUser
166
+ . delete ( )
153
167
. then ( ( ) => {
154
- setInputValue ( 'deleted_user' , { success : true , response : 'successful' } , msg . ns ) ;
155
- } ) . catch ( ( error ) => {
156
- Shiny . setInputValue ( 'deleted_user' , { success : false , response : error } , msg . ns ) ;
168
+ setInputValue (
169
+ "deleted_user" ,
170
+ { success : true , response : "successful" } ,
171
+ msg . ns ,
172
+ ) ;
173
+ } )
174
+ . catch ( ( error ) => {
175
+ Shiny . setInputValue (
176
+ "deleted_user" ,
177
+ { success : false , response : error } ,
178
+ msg . ns ,
179
+ ) ;
157
180
} ) ;
158
181
} ) ;
159
182
160
- Shiny . addCustomMessageHandler ( ' fireblaze-id-token' , ( msg ) => {
183
+ Shiny . addCustomMessageHandler ( " fireblaze-id-token" , ( msg ) => {
161
184
const auth = getAuth ( ) ;
162
- auth . currentUser . getIdToken ( true )
185
+ auth . currentUser
186
+ . getIdToken ( true )
163
187
. then ( ( idToken ) => {
164
- setInputValue ( 'id_token' , { response : { idToken : idToken } , success : true } , msg . ns ) ;
165
- } ) . catch ( ( error ) => {
166
- setInputValue ( 'id_token' , { response : error , success : false } , msg . ns ) ;
188
+ setInputValue (
189
+ "id_token" ,
190
+ { response : { idToken : idToken } , success : true } ,
191
+ msg . ns ,
192
+ ) ;
193
+ } )
194
+ . catch ( ( error ) => {
195
+ setInputValue ( "id_token" , { response : error , success : false } , msg . ns ) ;
167
196
} ) ;
168
197
} ) ;
0 commit comments