Skip to content

Commit b72e884

Browse files
committed
refactor: signed_in simplified
1 parent beebc4a commit b72e884

File tree

4 files changed

+103
-79
lines changed

4 files changed

+103
-79
lines changed

R/class-auth.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ FirebaseAuth <- R6::R6Class(
7575
#' containing the user object or \code{NULL} if signing in failed.
7676
get_signed_in = function(){
7777
response <- private$get_signed_in_checked()
78-
private$.user_signed_in <- response
7978
invisible(response)
8079
},
8180
#' @details Get results of a sign up
@@ -84,7 +83,6 @@ FirebaseAuth <- R6::R6Class(
8483
#' containing the user object or \code{NULL} if signing in failed.
8584
get_signed_up = function(){
8685
user <- private$get_signed_in_checked()
87-
private$.user_sign_up <- user
8886
invisible(user)
8987
},
9088
#' @details Check whether use is signed in
@@ -95,22 +93,23 @@ FirebaseAuth <- R6::R6Class(
9593
msg = "This method is method is deprecated, use `get_signed_in`"
9694
)
9795
user <- private$get_signed_in_checked()
98-
private$.user_signed_in <- user
9996
invisible(user$success)
10097
},
10198
#' @details Makes Shiny output, observer, or reactive require the user to be signed in
10299
req_sign_in = function(){
103100
user <- private$get_signed_in_checked()
104-
private$.user_signed_in <- user
101+
102+
if(!length(user$success))
103+
user <- list(success = FALSE)
104+
105105
req(user$success)
106106
},
107107
#' @details Makes Shiny output, observer, or reactive require the user to be signed out
108108
req_sign_out = function(){
109109
user <- private$get_signed_in_checked()
110-
private$.user_signed_in <- user
111110

112-
if(length(user$success))
113-
return(FALSE)
111+
if(!length(user$success))
112+
user <- list(success = FALSE)
114113

115114
req(!user$success)
116115
},

inst/packer/core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
check: site
1+
check: document
22
Rscript -e "devtools::check()"
33

4-
site: document
5-
Rscript docs/docify.R
6-
mkdocs build
7-
84
document: bundle
95
Rscript -e "devtools::document()"
106

srcjs/core.js

Lines changed: 95 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,98 @@
1-
import 'shiny';
2-
import {
3-
getAuth,
4-
signOut,
1+
import "shiny";
2+
import {
3+
getAuth,
4+
signOut,
55
setPersistence,
66
isSignInWithEmailLink,
77
signInWithEmailLink,
8-
onAuthStateChanged
8+
onAuthStateChanged,
99
} from "firebase/auth";
1010
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,
1515
showHideOnLogout,
1616
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";
2020

2121
// global variables
22-
window.globalNs = '';
22+
window.globalNs = "";
2323
var globalInit = false;
2424
window.uiInitialised = false;
2525
let firebaseApp;
2626

27-
Shiny.addCustomMessageHandler('fireblaze-render-dependencies', (msg) => {
27+
Shiny.addCustomMessageHandler("fireblaze-render-dependencies", (msg) => {
2828
Shiny.renderDependencies(msg.deps);
29-
})
29+
});
3030

31-
Shiny.addCustomMessageHandler('fireblaze-initialize-core', (msg) => {
32-
if(firebaseApp)
33-
return;
31+
Shiny.addCustomMessageHandler("fireblaze-initialize-core", (msg) => {
32+
if (firebaseApp) return;
3433

3534
firebaseApp = initializeApp(msg.conf);
3635
});
3736

38-
Shiny.addCustomMessageHandler('fireblaze-expose-app', (msg) => {
39-
if(!firebaseApp)
40-
return;
37+
Shiny.addCustomMessageHandler("fireblaze-expose-app", (msg) => {
38+
if (!firebaseApp) return;
4139

4240
window.firebaseApp = firebaseApp;
4341
});
4442

45-
Shiny.addCustomMessageHandler('fireblaze-expose-auth', (msg) => {
43+
Shiny.addCustomMessageHandler("fireblaze-expose-auth", (msg) => {
4644
window.firebaseAuth = getAuth();
4745
});
4846

49-
Shiny.addCustomMessageHandler('fireblaze-initialize-storage', (msg) => {
47+
Shiny.addCustomMessageHandler("fireblaze-initialize-storage", (msg) => {
5048
handleStorage(firebaseApp);
5149
});
5250

5351
// 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;
5854

5955
globalInit = true;
6056
// set namespace
6157
window.globalNs = msg.ns;
6258

6359
// auth
6460
const auth = getAuth();
65-
setLanguageCode(msg.languageCode)
61+
setLanguageCode(msg.languageCode);
6662

6763
// set persistence
6864
let persistence = persistenceOpts(msg.persistence);
6965
setPersistence(auth, persistence)
7066
.then(() => {
7167
onAuthStateChanged(auth, (user) => {
72-
if(user){
73-
68+
if (user) {
7469
// show signin authorised
7570
showHideOnLogin("show");
7671
showHideOnLogout("hide");
7772
$("#fireblaze-signin-ui").hide();
7873

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");
8485
});
8586

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;
9488
}
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 });
9596
});
9697

9798
// check email verification link
@@ -101,68 +102,96 @@ Shiny.addCustomMessageHandler('fireblaze-initialize-auth', (msg) => {
101102
// the sign-in operation.
102103
// Get the email if available. This should be available if the user completes
103104
// the flow on the same device where they started it.
104-
var email = window.localStorage.getItem('fireblazeEmailSignIn');
105+
var email = window.localStorage.getItem("fireblazeEmailSignIn");
105106
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+
});
107111
}
108112
// The client SDK will parse the code from the link for you.
109113
signInWithEmailLink(auth, email, window.location.href)
110114
.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+
});
113120
})
114121
.catch((error) => {
115-
setInputValue('email_verification', {success: false, response: error});
122+
setInputValue("email_verification", {
123+
success: false,
124+
response: error,
125+
});
116126
});
117127
}
118128
})
119129
.catch((error) => {
120130
console.error(error);
121131
});
122-
123132
});
124133

125134
// Sign out
126-
Shiny.addCustomMessageHandler('fireblaze-signout', (msg) => {
135+
Shiny.addCustomMessageHandler("fireblaze-signout", (msg) => {
127136
const auth = getAuth();
128137

129138
signOut(auth)
130139
.then(() => {
131-
if(window.uiInitialised){
140+
if (window.uiInitialised) {
132141
window.ui.start("#fireblaze-signin-ui", window.uiOpts);
133142
$("#fireblaze-signin-ui").show();
134143
}
135144

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);
139153
});
140-
141154
});
142155

143156
// Language code
144-
Shiny.addCustomMessageHandler('fireblaze-language-code', (msg) => {
157+
Shiny.addCustomMessageHandler("fireblaze-language-code", (msg) => {
145158
const auth = getAuth();
146159
auth.languageCode = msg.code;
147160
});
148161

149162
// Delete User
150-
Shiny.addCustomMessageHandler('fireblaze-delete-user', (msg) => {
163+
Shiny.addCustomMessageHandler("fireblaze-delete-user", (msg) => {
151164
const auth = getAuth();
152-
auth.currentUser.delete()
165+
auth.currentUser
166+
.delete()
153167
.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+
);
157180
});
158181
});
159182

160-
Shiny.addCustomMessageHandler('fireblaze-id-token', (msg) => {
183+
Shiny.addCustomMessageHandler("fireblaze-id-token", (msg) => {
161184
const auth = getAuth();
162-
auth.currentUser.getIdToken(true)
185+
auth.currentUser
186+
.getIdToken(true)
163187
.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);
167196
});
168197
});

0 commit comments

Comments
 (0)