-
Notifications
You must be signed in to change notification settings - Fork 27
/
app.js
228 lines (208 loc) · 8.11 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* FirebaseUI initialization to be used in a Single Page application context.
*/
const TORUS_DIRECT_SDK_VERIFIER_NAME = "demo-firebase-example";
var torusdirectsdk;
/**
* @return {!Object} The FirebaseUI config.
*/
function getUiConfig() {
return {
callbacks: {
// Called when the user has been successfully signed in.
signInSuccessWithAuthResult: async function (authResult, redirectUrl) {
// Do not redirect.
return false;
},
},
// Opens IDP Providers sign-in flow in a popup.
signInFlow: "popup",
signInOptions: [
// TODO(developer): Remove the providers you don't need for your app.
{
provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID,
// Required to enable ID token credentials for this provider.
clientId: CLIENT_ID,
},
// {
// provider: firebase.auth.FacebookAuthProvider.PROVIDER_ID,
// scopes: ["public_profile", "email", "user_likes", "user_friends"],
// },
// firebase.auth.TwitterAuthProvider.PROVIDER_ID,
// firebase.auth.GithubAuthProvider.PROVIDER_ID,
// {
// provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
// // Whether the display name should be displayed in Sign Up page.
// requireDisplayName: true,
// signInMethod: getEmailSignInMethod(),
// disableSignUp: {
// status: getDisableSignUpStatus(),
// },
// },
// {
// provider: firebase.auth.PhoneAuthProvider.PROVIDER_ID,
// recaptchaParameters: {
// size: getRecaptchaMode(),
// },
// },
// {
// provider: "microsoft.com",
// loginHintKey: "login_hint",
// },
// {
// provider: "apple.com",
// },
// firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID,
],
credentialHelper:
CLIENT_ID && CLIENT_ID != "YOUR_OAUTH_CLIENT_ID" ? firebaseui.auth.CredentialHelper.GOOGLE_YOLO : firebaseui.auth.CredentialHelper.NONE,
};
}
// Initialize the FirebaseUI Widget using Firebase.
var ui = new firebaseui.auth.AuthUI(firebase.auth());
// Disable auto-sign in.
ui.disableAutoSignIn();
/**
* @return {string} The URL of the FirebaseUI standalone widget.
*/
function getWidgetUrl() {
return (
"/widget#recaptcha=" +
getRecaptchaMode() +
"&emailSignInMethod=" +
getEmailSignInMethod() +
"&disableEmailSignUpStatus=" +
getDisableSignUpStatus() +
"&adminRestrictedOperationStatus=" +
getAdminRestrictedOperationStatus()
);
}
/**
* Redirects to the FirebaseUI widget.
*/
var signInWithRedirect = function () {
window.location.assign(getWidgetUrl());
};
/**
* Open a popup with the FirebaseUI widget.
*/
var signInWithPopup = function () {
window.open(getWidgetUrl(), "Sign In", "width=985,height=735");
};
/**
* Displays the UI for a signed in user.
* @param {!firebase.User} user
*/
var handleSignedInUser = function (user, privateKey, publicAddress) {
document.getElementById("user-signed-in").style.display = "block";
document.getElementById("user-signed-out").style.display = "none";
document.getElementById("privateKey").textContent = "Private Key: " + privateKey;
document.getElementById("publicKey").textContent = "Public Address: " + publicAddress;
document.getElementById("name").textContent = user.displayName;
document.getElementById("email").textContent = user.email;
document.getElementById("phone").textContent = user.phoneNumber;
if (user.photoURL) {
var photoURL = user.photoURL;
// Append size to the photo URL for Google hosted images to avoid requesting
// the image with its original resolution (using more bandwidth than needed)
// when it is going to be presented in smaller size.
if (photoURL.indexOf("googleusercontent.com") != -1 || photoURL.indexOf("ggpht.com") != -1) {
photoURL = photoURL + "?sz=" + document.getElementById("photo").clientHeight;
}
document.getElementById("photo").src = photoURL;
document.getElementById("photo").style.display = "block";
} else {
document.getElementById("photo").style.display = "none";
}
};
/**
* Displays the UI for a signed out user.
*/
var handleSignedOutUser = function () {
document.getElementById("user-signed-in").style.display = "none";
document.getElementById("user-signed-out").style.display = "block";
ui.start("#firebaseui-container", getUiConfig());
};
// Listen to change in auth state so it displays the correct UI for when
// the user is signed in or not.
firebase.auth().onAuthStateChanged(async function (user) {
document.getElementById("loading").style.display = "none";
document.getElementById("loaded").style.display = "block";
if (user) {
// fetch the id token of loggedIn user
const idToken = await firebase.auth().currentUser.getIdToken(/* forceRefresh */ true);
// Send token to torus nodes for constructing privateKey
const { privateKey, publicAddress } = await torusdirectsdk.getTorusKey(
TORUS_DIRECT_SDK_VERIFIER_NAME,
firebase.auth().currentUser.uid,
{ verifier_id: firebase.auth().currentUser.uid },
idToken
);
handleSignedInUser(user, privateKey, publicAddress);
} else {
handleSignedOutUser();
}
});
/**
* Handles when the user changes the reCAPTCHA, email signInMethod or email
* disableSignUp config.
*/
function handleConfigChange() {
var newRecaptchaValue = document.querySelector('input[name="recaptcha"]:checked').value;
var newEmailSignInMethodValue = document.querySelector('input[name="emailSignInMethod"]:checked').value;
var currentDisableSignUpStatus = document.getElementById("email-disableSignUp-status").checked;
var currentAdminRestrictedOperationStatus = document.getElementById("admin-restricted-operation-status").checked;
location.replace(
location.pathname +
"#recaptcha=" +
newRecaptchaValue +
"&emailSignInMethod=" +
newEmailSignInMethodValue +
"&disableEmailSignUpStatus=" +
currentDisableSignUpStatus +
"&adminRestrictedOperationStatus=" +
currentAdminRestrictedOperationStatus
);
// Reset the inline widget so the config changes are reflected.
ui.reset();
ui.start("#firebaseui-container", getUiConfig());
}
/**
* Initializes the app.
*/
var initApp = async function () {
document.getElementById("sign-in-with-redirect").addEventListener("click", signInWithRedirect);
document.getElementById("sign-in-with-popup").addEventListener("click", signInWithPopup);
document.getElementById("sign-out").addEventListener("click", function () {
firebase.auth().signOut();
});
// document.getElementById("recaptcha-normal").addEventListener("change", handleConfigChange);
// document.getElementById("recaptcha-invisible").addEventListener("change", handleConfigChange);
// // Check the selected reCAPTCHA mode.
// document.querySelector('input[name="recaptcha"][value="' + getRecaptchaMode() + '"]').checked = true;
// document.getElementById("email-signInMethod-password").addEventListener("change", handleConfigChange);
// document.getElementById("email-signInMethod-emailLink").addEventListener("change", handleConfigChange);
// // Check the selected email signInMethod mode.
// document.querySelector('input[name="emailSignInMethod"][value="' + getEmailSignInMethod() + '"]').checked = true;
torusdirectsdk = new window.Customauth.default({
baseUrl: `${window.location.origin}`,
redirectPathName: "/",
enableLogging: true,
network: "testnet", // details for test net
});
await torusdirectsdk.init({ skipSw: true });
};
window.addEventListener("load", initApp);