Skip to content

Commit 82f10be

Browse files
committed
fix: add redirect for torus
1 parent d8263f0 commit 82f10be

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed

src/static/oauth/redirect.html

+292
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
7+
<title>Redirect</title>
8+
<style>
9+
* {
10+
box-sizing: border-box;
11+
}
12+
13+
html,
14+
body {
15+
background: #fcfcfc;
16+
height: 100%;
17+
padding: 0;
18+
margin: 0;
19+
}
20+
21+
.container {
22+
width: 100%;
23+
height: 100%;
24+
25+
display: flex;
26+
justify-content: center;
27+
align-items: center;
28+
flex-direction: column;
29+
}
30+
31+
h1.title {
32+
font-size: 14px;
33+
color: #0f1222;
34+
font-family: 'Roboto', sans-serif !important;
35+
margin: 0;
36+
text-align: center;
37+
}
38+
39+
.spinner .beat {
40+
background-color: #0364ff;
41+
height: 12px;
42+
width: 12px;
43+
margin: 24px 2px 10px;
44+
border-radius: 100%;
45+
-webkit-animation: beatStretchDelay 0.7s infinite linear;
46+
animation: beatStretchDelay 0.7s infinite linear;
47+
-webkit-animation-fill-mode: both;
48+
animation-fill-mode: both;
49+
display: inline-block;
50+
}
51+
52+
.spinner .beat-odd {
53+
animation-delay: 0s;
54+
}
55+
56+
.spinner .beat-even {
57+
animation-delay: 0.35s;
58+
}
59+
60+
@-webkit-keyframes beatStretchDelay {
61+
50% {
62+
-webkit-transform: scale(0.75);
63+
transform: scale(0.75);
64+
-webkit-opacity: 0.2;
65+
opacity: 0.2;
66+
}
67+
68+
100% {
69+
-webkit-transform: scale(1);
70+
transform: scale(1);
71+
-webkit-opacity: 1;
72+
opacity: 1;
73+
}
74+
}
75+
76+
@keyframes beatStretchDelay {
77+
50% {
78+
-webkit-transform: scale(0.75);
79+
transform: scale(0.75);
80+
-webkit-opacity: 0.2;
81+
opacity: 0.2;
82+
}
83+
84+
100% {
85+
-webkit-transform: scale(1);
86+
transform: scale(1);
87+
-webkit-opacity: 1;
88+
opacity: 1;
89+
}
90+
}
91+
92+
@media (min-width: 768px) {
93+
h1.title {
94+
font-size: 14px;
95+
}
96+
p.info {
97+
font-size: 28px;
98+
}
99+
100+
.spinner .beat {
101+
height: 12px;
102+
width: 12px;
103+
}
104+
}
105+
</style>
106+
</head>
107+
108+
<body>
109+
<div id="message" class="container">
110+
<div class="spinner content" id="spinner">
111+
<div class="beat beat-odd"></div>
112+
<div class="beat beat-even"></div>
113+
<div class="beat beat-odd"></div>
114+
</div>
115+
<h1 class="title content" id="closeText" style="display: none">You can close this window now</h1>
116+
</div>
117+
<script
118+
src="https://scripts.toruswallet.io/broadcastChannel_5_0_2.js"
119+
integrity="sha384-Bu0bRAeSlh2jpBuUxKk5ivkdotaHH37cQ2XiV20EmFJmghb41D0f8xME/M1WZxFC"
120+
crossorigin="anonymous"
121+
></script>
122+
<script>
123+
function storageAvailable(type) {
124+
var storage
125+
try {
126+
storage = window[type]
127+
var x = '__storage_test__'
128+
storage.setItem(x, x)
129+
storage.removeItem(x)
130+
return true
131+
} catch (e) {
132+
return (
133+
e &&
134+
// everything except Firefox
135+
(e.code === 22 ||
136+
// Firefox
137+
e.code === 1014 ||
138+
// test name field too, because code might not be present
139+
// everything except Firefox
140+
e.name === 'QuotaExceededError' ||
141+
// Firefox
142+
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
143+
// acknowledge QuotaExceededError only if there's something already stored
144+
storage &&
145+
storage.length !== 0
146+
)
147+
}
148+
}
149+
function showCloseText() {
150+
var closeText = document.getElementById('closeText')
151+
var spinner = document.getElementById('spinner')
152+
if (closeText) {
153+
closeText.style.display = 'block'
154+
}
155+
if (spinner) {
156+
spinner.style.display = 'none'
157+
}
158+
}
159+
var isLocalStorageAvailable = storageAvailable('localStorage')
160+
var isSessionStorageAvailable = storageAvailable('sessionStorage')
161+
// set theme
162+
let theme = 'light'
163+
if (isLocalStorageAvailable) {
164+
var torusTheme = localStorage.getItem('torus-theme')
165+
if (torusTheme) {
166+
theme = torusTheme.split('-')[0]
167+
}
168+
}
169+
170+
if (theme === 'dark') {
171+
document.querySelector('body').style.backgroundColor = '#24252A'
172+
}
173+
var bc
174+
var broadcastChannelOptions = {
175+
// type: 'localstorage', // (optional) enforce a type, oneOf['native', 'idb', 'localstorage', 'node'
176+
webWorkerSupport: false, // (optional) set this to false if you know that your channel will never be used in a WebWorker (increase performance)
177+
}
178+
var instanceParams = {}
179+
var preopenInstanceId = new URL(window.location.href).searchParams.get('preopenInstanceId')
180+
if (!preopenInstanceId) {
181+
document.getElementById('message').style.visibility = 'visible'
182+
// in general oauth redirect
183+
try {
184+
var url = new URL(location.href)
185+
var hash = url.hash.substr(1)
186+
var hashParams = {}
187+
if (hash) {
188+
hashParams = hash.split('&').reduce(function (result, item) {
189+
var parts = item.split('=')
190+
result[parts[0]] = parts[1]
191+
return result
192+
}, {})
193+
}
194+
var queryParams = {}
195+
for (var key of url.searchParams.keys()) {
196+
queryParams[key] = url.searchParams.get(key)
197+
}
198+
var error = ''
199+
try {
200+
if (Object.keys(hashParams).length > 0 && hashParams.state) {
201+
instanceParams = JSON.parse(window.atob(decodeURIComponent(decodeURIComponent(hashParams.state)))) || {}
202+
if (hashParams.error) error = hashParams.error
203+
} else if (Object.keys(queryParams).length > 0 && queryParams.state) {
204+
instanceParams = JSON.parse(window.atob(decodeURIComponent(decodeURIComponent(queryParams.state)))) || {}
205+
if (queryParams.error) error = queryParams.error
206+
}
207+
} catch (e) {
208+
console.error(e)
209+
}
210+
if (instanceParams.redirectToOpener) {
211+
// communicate to window.opener
212+
window.opener.postMessage(
213+
{
214+
channel: 'redirect_channel_' + instanceParams.instanceId,
215+
data: {
216+
instanceParams: instanceParams,
217+
hashParams: hashParams,
218+
queryParams: queryParams,
219+
},
220+
error: error,
221+
},
222+
'http://localhost:3000',
223+
)
224+
} else {
225+
// communicate via broadcast channel
226+
bc = new broadcastChannelLib.BroadcastChannel(
227+
'redirect_channel_' + instanceParams.instanceId,
228+
broadcastChannelOptions,
229+
)
230+
bc.addEventListener('message', function (ev) {
231+
if (ev.success) {
232+
bc.close()
233+
console.log('posted', {
234+
queryParams,
235+
instanceParams,
236+
hashParams,
237+
})
238+
} else {
239+
window.close()
240+
showCloseText()
241+
}
242+
})
243+
bc.postMessage({
244+
data: {
245+
instanceParams: instanceParams,
246+
hashParams: hashParams,
247+
queryParams: queryParams,
248+
},
249+
error: error,
250+
}).then(function () {
251+
setTimeout(function () {
252+
window.location.href = url.origin + location.search + location.hash
253+
}, 5000)
254+
})
255+
}
256+
} catch (err) {
257+
console.error(err, 'service worker error in redirect')
258+
bc && bc.close()
259+
window.close()
260+
showCloseText()
261+
}
262+
} else {
263+
// in preopen, awaiting redirect
264+
try {
265+
bc = new broadcastChannelLib.BroadcastChannel('preopen_channel_' + preopenInstanceId, broadcastChannelOptions)
266+
bc.onmessage = function (ev) {
267+
var { preopenInstanceId: oldId, payload, message } = ev.data
268+
if (oldId === preopenInstanceId && payload && payload.url) {
269+
window.location.href = payload.url
270+
} else if (oldId === preopenInstanceId && message === 'setup_complete') {
271+
bc.postMessage({
272+
data: {
273+
preopenInstanceId: preopenInstanceId,
274+
message: 'popup_loaded',
275+
},
276+
})
277+
}
278+
if (ev.error && ev.error !== '') {
279+
console.error(ev.error)
280+
bc.close()
281+
}
282+
}
283+
} catch (err) {
284+
console.error(err, 'service worker error in preopen')
285+
bc && bc.close()
286+
window.close()
287+
showCloseText()
288+
}
289+
}
290+
</script>
291+
</body>
292+
</html>

0 commit comments

Comments
 (0)